YouTube2Text

[CS61C FA20] Lecture 08.2 - RISC-V lw, sw, Decisions I: Data Transfer Instructions — Transcript

by CS 61C Departmental · 3,245 words · 569 segments · language en · Watch on YouTube

Full transcript

  1. 0:00[Music]
  2. 0:08hi
  3. 0:09welcome back we are continuing with race
  4. 0:125 assembly
  5. 0:13so let's see those instructions that do
  6. 0:16data transfers to
  7. 0:17and from the memory remember the picture
  8. 0:20from early in the course when we
  9. 0:22introduced the principal
  10. 0:23memory hierarchy the processor core was
  11. 0:27at the very top with its registers
  12. 0:31registers are extremely fast they share
  13. 0:34that precious real estate with a
  14. 0:35processor core and therefore
  15. 0:37are extremely expensive so
  16. 0:40we have a small number of them to be to
  17. 0:43be precise
  18. 0:4432 total now
  19. 0:47on a separate chip typically there is
  20. 0:50the main memory it is implementing a
  21. 0:51different technology it is called
  22. 0:53dram which stands for dynamic random
  23. 0:56access memory
  24. 0:58and it comes in different flavors you
  25. 1:01might have heard of double data rate
  26. 1:04which is ddr and comes in different
  27. 1:07generations
  28. 1:08three four five or maybe high bandwidth
  29. 1:11memory
  30. 1:12which is hbm and that also has different
  31. 1:15generations
  32. 1:16too there is hbm hbm2 and then
  33. 1:20now we are getting to hbm3 generation
  34. 1:24dram is also fast but not nearly as fast
  35. 1:27as the registers
  36. 1:29at this price reasonably you get a lot
  37. 1:31of gigabytes for a few tens of dollars
  38. 1:34and has medium capacity compared to
  39. 1:39manage storage that we have in disks and
  40. 1:41solid-state drives
  41. 1:45but the question here is you know what
  42. 1:46how big is this gap in the in the
  43. 1:48pyramid how much a register is really
  44. 1:50faster than the memory
  45. 1:51so given that there are 32 registers um
  46. 1:5432 bits each total of 1024 bits
  47. 1:58meaning 128 bytes total there
  48. 2:02on the other hand in dram we may have in
  49. 2:06a low end
  50. 2:06laptop about two gigabytes in a high-end
  51. 2:08laptop up to 64 gigabytes of memory
  52. 2:11and the server may have a terabyte of
  53. 2:13memory and the physics dictates that
  54. 2:15smaller is faster
  55. 2:17main claim to fame or bruce lee for
  56. 2:20example or
  57. 2:21uh typically point guards are a lot
  58. 2:24faster than the centers in basketball
  59. 2:26how much faster are the registers the ra
  60. 2:30and then dram really so when we put that
  61. 2:32on a piece of paper
  62. 2:34we will find out that the numbers work
  63. 2:36out that they're about
  64. 2:38you know 50 to 500 500 times faster
  65. 2:42so in terms that's in terms of latency
  66. 2:46of one axis or the first access to zero
  67. 2:49for to get one individual data item
  68. 2:53you know that taxes takes you know a few
  69. 2:55tens of nanoseconds and you compare that
  70. 2:57with the fraction of a nanosecond that
  71. 2:59it takes us to
  72. 3:00access registers keep in mind
  73. 3:04that this is for one isolated access you
  74. 3:06know many succeed
  75. 3:08subsequent accesses to nearby memory
  76. 3:10locations
  77. 3:11will go faster
  78. 3:15to put this in perspective a great
  79. 3:17picture is
  80. 3:19to keep in mind is this picture of
  81. 3:22jim gray's storage latency analogy
  82. 3:26what we have on the left hand side
  83. 3:30are our registers and they take about a
  84. 3:34nanosecond to access
  85. 3:35memory takes about two or so magnitude
  86. 3:38more
  87. 3:38so 100 nanoseconds
  88. 3:43let's try to make an equivalent to tasks
  89. 3:46that we do as humans so for example if
  90. 3:48we would like
  91. 3:49if we would like to retrieve data from
  92. 3:51our heads um
  93. 3:53let's say that the retrieving data from
  94. 3:55a register that is in our head would
  95. 3:57take about a minute
  96. 3:58so what is that takes 100 times longer
  97. 4:03where would that data be well when we
  98. 4:05are all in berkeley and live and
  99. 4:07all that um you know it would be
  100. 4:11normal to make an analogy to you know
  101. 4:13driving to a town
  102. 4:14for example we we forgot a piece of
  103. 4:17paper somewhere and we are going to go
  104. 4:18and retrieve it
  105. 4:19let's say that town is sacramento which
  106. 4:22is
  107. 4:23you know roughly 100 minutes away around
  108. 4:26half away that's
  109. 4:29big penalty on time we can do a lot of
  110. 4:32stuff in an hour and a half
  111. 4:34or if that gap is 500 x that it may be
  112. 4:37the case in some cases in 500 minutes we
  113. 4:40can make it to los angeles and back
  114. 4:43it would not be convenient to leave that
  115. 4:46piece of paper with our data in l.a and
  116. 4:49having to retreat they have to go to
  117. 4:50retrieve it
  118. 4:51or sacramento so this
  119. 4:55is there to try to illustrate how
  120. 4:57expensive is it
  121. 4:58to have to go get the data
  122. 5:01from the memory let's
  123. 5:04try to see how do these instructions
  124. 5:07look like
  125. 5:08on a simple example so the first one
  126. 5:10that we're going to look at
  127. 5:12involves loading from memory to register
  128. 5:16so this is a simple code that we is
  129. 5:19that's in c
  130. 5:19and we'll try to translate it into risk
  131. 5:22five assembly
  132. 5:24so let's go back line by line so what we
  133. 5:26have
  134. 5:27here
  135. 5:32we have a declaration of an integer
  136. 5:35array of 100 elements
  137. 5:36and then the second line very simply
  138. 5:39takes element a3 of that array
  139. 5:42and adds it to the variable h and stores
  140. 5:45the result
  141. 5:46in a variable g i'm using kind of
  142. 5:49assembly techno terminology again but
  143. 5:52you guys know what what i mean here
  144. 5:56so we know how to add things in
  145. 5:58registers so we need to figure out a way
  146. 6:00how to get
  147. 6:01the value of this third element in the
  148. 6:04array that
  149. 6:04resides in memory to our register
  150. 6:09we need a new instruction there so the
  151. 6:11new instruction
  152. 6:12is showing up right here it is load word
  153. 6:16so load word and risk 5 goes to the
  154. 6:20memory
  155. 6:21and loads from the memory to a register
  156. 6:24so the syntax is fairly straightforward
  157. 6:26to understand here take a look
  158. 6:28this is my destination register so
  159. 6:31mnemonic is
  160. 6:33lw destination register and then
  161. 6:36we have this what does that mean we have
  162. 6:39to specify the base register
  163. 6:42which is the pointer to the element a0
  164. 6:44of the array
  165. 6:45and then specify the offset to the
  166. 6:49the element of the array that we would
  167. 6:51like to retrieve
  168. 6:52so the base pointer points with an
  169. 6:55offset
  170. 6:56of 0 to the element a0 would like to get
  171. 6:59the third
  172. 7:00element in the array but remember we are
  173. 7:03storing
  174. 7:04integers in this array they are 32 bits
  175. 7:06wide
  176. 7:0832 bits is 4 bytes and
  177. 7:12risk 5 addresses each byte in memory
  178. 7:15so we need to specify this offset in
  179. 7:17bytes
  180. 7:19three words is equal
  181. 7:22to 12 bytes
  182. 7:26so that's why this offset is specified
  183. 7:29by as 12
  184. 7:30that's why we have 12 here so the actual
  185. 7:34address of the datum in the memory is
  186. 7:36calculated
  187. 7:37as the address of the base pointer which
  188. 7:41is
  189. 7:42the the content of a register x15
  190. 7:46plus 12 very well
  191. 7:51so keep in mind that this offset must be
  192. 7:52a constant
  193. 7:54known at the assembly time so if you
  194. 7:57picture it another thing to to remember
  195. 8:00this syntax wall
  196. 8:01is the direction of the data flow
  197. 8:05within the instruction that kind of
  198. 8:07mimics what we see
  199. 8:09in in reality so data flow
  200. 8:12goes from right to left
  201. 8:15like what we have seen in other
  202. 8:18instructions so we add instruction also
  203. 8:20moves the data
  204. 8:21from right to left we add contents of
  205. 8:25registers x12
  206. 8:26and x10 and store the result
  207. 8:30in x11
  208. 8:34so that kind of should resemble a
  209. 8:37picture that we have had in the
  210. 8:38previous segment we take data
  211. 8:42from the right in the memory and move it
  212. 8:45to the left to the registers so we load
  213. 8:48from memory to registers
  214. 8:51we are going from right to left
  215. 8:54so to recap what we have seen here we
  216. 8:57have
  217. 8:58essentially translated this c code one
  218. 9:01note to
  219. 9:02to make here there is no really assembly
  220. 9:04instruction
  221. 9:05for now for
  222. 9:09the translates the declaration of um
  223. 9:12allocating this integer array all what
  224. 9:14we are translating is this
  225. 9:16one c instruction that as
  226. 9:20h plus a3 and saves it as g
  227. 9:24so what we have there we first load the
  228. 9:26contents
  229. 9:27of the of this memory location
  230. 9:31specified that by the base pointer plus
  231. 9:33the offset
  232. 9:34to temporary register x10 and then we
  233. 9:37add the contents of x10
  234. 9:39with x12 and s4 result in x11 notice
  235. 9:42that we could have been a little bit
  236. 9:44more um
  237. 9:46optimized here and could have saved
  238. 9:50this could have reused register x10
  239. 9:54for the variable g okay
  240. 9:58hopefully this made sense let's see the
  241. 10:00other instruction
  242. 10:01the other instruction that we need is to
  243. 10:03store from the register
  244. 10:05to the memory so it goes the other way
  245. 10:09we have something in the register and we
  246. 10:11want to put it in a memory and here is
  247. 10:12the example code
  248. 10:13exact same code except that we have
  249. 10:15changed a bit in it
  250. 10:17instead of saving it in a variable g we
  251. 10:20are we will need to save it in another
  252. 10:24element in the array which in this case
  253. 10:26is a10 it is
  254. 10:28offset by 10 words from
  255. 10:31the base pointer so the first two lines
  256. 10:34are identical
  257. 10:36as what we have had before we did get a
  258. 10:39little bit better here we are
  259. 10:40we have learned we are reusing x10
  260. 10:44as a temporary variable and now here
  261. 10:46comes a new instruction
  262. 10:48this new instruction is store oops
  263. 10:52store word and risk 5.
  264. 10:56that store worked in risk 5
  265. 10:59does the following it takes the contents
  266. 11:02of a register
  267. 11:03x10 where we have saved our temporary
  268. 11:06result
  269. 11:08and moves it to a memory location
  270. 11:11that is addressed by the base pointer
  271. 11:14and the offset
  272. 11:15and in this case since we want to write
  273. 11:17in the 10th element of the array
  274. 11:20we have to specify the offset as
  275. 11:2310 times 4 bytes away
  276. 11:26so that is 40
  277. 11:29is the opposite here so that's why we
  278. 11:32have 40 there
  279. 11:38the data movement or data flow here is
  280. 11:40the other way round
  281. 11:42we are storing from the register in our
  282. 11:45picture
  283. 11:46to the memory
  284. 11:50and the data flow is going from
  285. 11:53left to right
  286. 11:56okay very well so that should hopefully
  287. 12:00keep confusion out of the way
  288. 12:03we are always in this syntax um
  289. 12:07the load operations are moving data from
  290. 12:10right to left in
  291. 12:14store operations are storing from
  292. 12:15register to memory and it is moving
  293. 12:18from left to right
  294. 12:22one thing to keep in mind
  295. 12:26these offsets
  296. 12:29for loading and storing should be
  297. 12:33multiples of four it's written here as
  298. 12:37must because that's a really good
  299. 12:38practice
  300. 12:39one side slight disclaimer risk file sa
  301. 12:43allows for misaligned
  302. 12:47memory uh accesses meaning that it
  303. 12:50allows you to scribble to start writing
  304. 12:53in the middle of one word and then write
  305. 12:55a half a word in one memory location and
  306. 12:57another half in another memory location
  307. 13:00but that's not advisable it is just
  308. 13:02there to support some
  309. 13:03legacy code we should not do that
  310. 13:06because that would be something that is
  311. 13:08very very slow and it's really messy we
  312. 13:10don't want to be just scribbling all
  313. 13:11over the memory so
  314. 13:12you can you should take this should
  315. 13:15really is a must
  316. 13:21now keep in mind that these load board
  317. 13:23and store board
  318. 13:25instructions support essentially taking
  319. 13:28the entire 32 bits from a memory
  320. 13:30location
  321. 13:31a word from memory and putting it into a
  322. 13:33register so they they fit really nicely
  323. 13:35it's 32 bits to 32 bits
  324. 13:37or when we are storing to the memory we
  325. 13:40take 32 bits from a register
  326. 13:42and place it into a memory but often
  327. 13:45we operate with different kinds of data
  328. 13:48types that are not necessarily 32 bits
  329. 13:50wide so it will be wasteful to
  330. 13:52use 32-bit memory locations to store
  331. 13:56just 8 bits worth of data
  332. 13:59like when we are storing characters when
  333. 14:02we are storing
  334. 14:04you know color channels
  335. 14:08that are typically all eight bits wide
  336. 14:11so
  337. 14:11risk five is conscious of that and
  338. 14:14supports
  339. 14:16shorter operands so it allows for
  340. 14:20load byte and store byte the format is
  341. 14:23exactly the same
  342. 14:24same as what we have seen in load word
  343. 14:27store word
  344. 14:30um as we have as we see here
  345. 14:33so we have load byte
  346. 14:36it loads a content of a memory location
  347. 14:38specified by the base pointer
  348. 14:40x11 with an offset of three bytes
  349. 14:44to a memory uh to a register x10
  350. 14:47so um it is the same as what we have
  351. 14:51seen before
  352. 14:52with one difference is that this offset
  353. 14:54now really doesn't have to be
  354. 14:56and shouldn't be four because we should
  355. 14:59be able to pluck
  356. 15:00any byte from any part
  357. 15:03of a word and write it
  358. 15:06in our destination register
  359. 15:10now by convention
  360. 15:13when we take that byte no matter where
  361. 15:15it is stored in the memory because
  362. 15:17all characters can be packed right next
  363. 15:19to each other as
  364. 15:20bytes in the world so we will be
  365. 15:22indexing that
  366. 15:23by that those memory locations by one
  367. 15:27it will copy it to the low byte
  368. 15:30position of the register extent so if
  369. 15:33this is our register extend
  370. 15:35here no matter where we picks up fix the
  371. 15:38python in this case
  372. 15:39the offset is three so i'll take the the
  373. 15:42highest byte the most significant byte
  374. 15:44uh from the that memory location that
  375. 15:47x11 points to
  376. 15:49it would write it into a
  377. 15:52low byte position of register x10
  378. 15:55there is another important thing to to
  379. 15:57keep in mind here
  380. 15:59we don't know what kind of
  381. 16:02data types we are operating but in many
  382. 16:05cases
  383. 16:06we will be operating with signed numbers
  384. 16:09so remembering signed numbers if it is a
  385. 16:12two's complement number
  386. 16:14this first bit of a byte of an
  387. 16:17of a byte determines what is
  388. 16:21the sign whether that number is positive
  389. 16:24or negative by
  390. 16:25looking at it if it is a zero the number
  391. 16:27is positive if it is a one
  392. 16:29the number is negative so what should we
  393. 16:32do
  394. 16:34when we copy this byte to our
  395. 16:37register to preserve its sign so
  396. 16:41if we just write zeros um in the upper
  397. 16:44three bytes well
  398. 16:48then no matter what their number was we
  399. 16:50are going to make it look like
  400. 16:52like it's a positive number we do
  401. 16:55have to preserve the sign so the way how
  402. 16:58we preserve the sign
  403. 16:59we take a note of that top
  404. 17:02bit the most significant bit in the byte
  405. 17:05and copy it over
  406. 17:07that copy over is called the sign
  407. 17:10extension
  408. 17:12and you know the way how it looks in
  409. 17:14this picture we actually take that
  410. 17:17bit and smear it all over
  411. 17:20the the upper bites in the in the world
  412. 17:25so we take the most significant bit out
  413. 17:28of
  414. 17:29that low bite and smear it over like
  415. 17:32avocado on a toast
  416. 17:34make sense
  417. 17:39so that's sign extension we don't
  418. 17:43always want to do sign extension so if
  419. 17:46we don't want if
  420. 17:47if you're certain this is not a sign um
  421. 17:51number that we are transferring from
  422. 17:53memory it is
  423. 17:54uh a character or or
  424. 17:57something it is always a positive number
  425. 18:00like
  426. 18:01color intensity
  427. 18:04risk 5 supports load byte
  428. 18:08unsigned which is another instruction
  429. 18:12that copies a byte from a memory
  430. 18:16loads a byte from memory to a register
  431. 18:19but does not do sign extension so
  432. 18:23it just fills the upper bytes with zeros
  433. 18:26a question for you why there is no
  434. 18:29equivalent of that
  435. 18:30no unsigned store by it so no sbu
  436. 18:38well it doesn't make sense we're just
  437. 18:40taking
  438. 18:41a byte and plucking it into a memory
  439. 18:44location there is no
  440. 18:45sign extension that is happening we
  441. 18:47don't have to fill out anything there
  442. 18:49now lbu has exactly the same
  443. 18:52syntax as lb all right
  444. 18:57so let's do a little more than exercise
  445. 18:59here
  446. 19:00um to see how this all this clicks
  447. 19:04together
  448. 19:05so the question here in this example is
  449. 19:08what
  450. 19:08is in x12 after these three instructions
  451. 19:11are executed and
  452. 19:12feel free to pause here work it out
  453. 19:14yourself or
  454. 19:16you can just follow me as i do it
  455. 19:19so these are the three instructions that
  456. 19:21are fairly straightforward the first one
  457. 19:25essentially stores 3f5
  458. 19:28in register x11 the second one stores a
  459. 19:31word
  460. 19:33stores the content of x11 into a memory
  461. 19:35location that is pointed by the
  462. 19:38pointed to by the base pointer x5 with
  463. 19:41no offset so it just
  464. 19:42takes that address that is stored in x5
  465. 19:45and writes
  466. 19:46right there and then finally
  467. 19:49the the last instruction
  468. 19:52lb takes bite with opposite of one
  469. 19:56from that same word and writes it in x12
  470. 20:00i think you got a picture if you haven't
  471. 20:02let me do the let me walk you through
  472. 20:04that
  473. 20:05so what we have in x11 after the
  474. 20:08first instruction is this
  475. 20:12we have f 5
  476. 20:160 3 0 0 0 0
  477. 20:19let's say that x5 points to this
  478. 20:22location
  479. 20:24so then this content of a register x11
  480. 20:28will be
  481. 20:29written exactly there so we are going to
  482. 20:31get f5
  483. 20:330 3 0 0
  484. 20:370 0. now when we say load byte
  485. 20:42with an offset of 1 which byte are we
  486. 20:44going to pick
  487. 20:46so this is a word with an offset of zero
  488. 20:50the next word in memory has
  489. 20:53offset of four because we're bite
  490. 20:55addressing the memory
  491. 20:57so the byte that has an offset of one
  492. 21:01is this one so load byte is going to
  493. 21:04take the contents
  494. 21:06of this byte of that word
  495. 21:09second byte in that word or the second
  496. 21:13to the least significant byte in the
  497. 21:15word
  498. 21:16and write it here so it'll be zero three
  499. 21:19i'll take a look at the most significant
  500. 21:21bit
  501. 21:21in that word and sign extend it
  502. 21:25to fill the rest of the register with
  503. 21:27zeros hopefully this is a good
  504. 21:29illustration
  505. 21:30and you get to practice a few of these
  506. 21:33afterwards
  507. 21:36one little final note
  508. 21:39we have only introduced about eight
  509. 21:42risk five assembly instructions and one
  510. 21:45um
  511. 21:46who could who is paying good enough
  512. 21:49attention here
  513. 21:50might notice that at least one of them
  514. 21:53is
  515. 21:54redundant it's ad immediate we really
  516. 21:57don't need
  517. 21:58an immediate we can replace an immediate
  518. 21:59with the sequence of two instructions
  519. 22:01that we have here
  520. 22:02we can store a constant in the memory
  521. 22:05load it from the memory into a register
  522. 22:08and then
  523. 22:09add that new immediate
  524. 22:13to a content of another register and
  525. 22:16voila that is
  526. 22:17ad immediate
  527. 22:21so what's up with that why do we have
  528. 22:23redundancy and respiration
  529. 22:25reduced instruction set computer
  530. 22:26supposed to be really lean and mean
  531. 22:31yeah this does not change the fact that
  532. 22:33this
  533. 22:34reduced instruction set computer and
  534. 22:37immediate
  535. 22:39is necessary because
  536. 22:42these two instructions if if
  537. 22:46we don't have added media those two
  538. 22:47instructions would be very slow we would
  539. 22:49have to go for every immediate to
  540. 22:51sacramento to get it and then
  541. 22:54do the addition that would be a really
  542. 22:57really
  543. 22:58low performance machine because ad
  544. 23:00immediates
  545. 23:01are very frequent in assembly code
  546. 23:06so this is
  547. 23:09here the instruction there is necessary
  548. 23:11to support the common case and the
  549. 23:14common case
  550. 23:15is adding immediates there is
  551. 23:18one big difference between these two
  552. 23:21instructions
  553. 23:22we'll touch on that in just a little bit
  554. 23:24in a couple of segments
  555. 23:26but the point here is immediate
  556. 23:29in the instruction field are limited
  557. 23:32there are they have to be less
  558. 23:33than 32 bits because we have to store
  559. 23:36other information so immediates
  560. 23:38are a short part of the instruction
  561. 23:41field so the range
  562. 23:42of immediates in addi is
  563. 23:46relatively short if you need a longer
  564. 23:50immediate
  565. 23:50we would have to load it from a memory
  566. 23:52or there may be other mechanisms
  567. 23:54to generate it but we'll pause here
  568. 23:58i'll see you in a bit with more risk 5
  569. 24:01instructions

About this transcript

This page contains the full transcript of [CS61C FA20] Lecture 08.2 - RISC-V lw, sw, Decisions I: Data Transfer Instructions by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 3,245 words across 569 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.