YouTube2Text

[CS61C FA20] Lecture 12.2 - RISC-V Instruction Formats II: Upper Immediates — Transcript

by CS 61C Departmental · 1,901 words · 339 segments · language en · Watch on YouTube

Full transcript

  1. 0:00[Music]
  2. 0:10hi welcome back to a new episode
  3. 0:12of risk 5 instruction instruction
  4. 0:14encodings we have seen
  5. 0:16four race five instruction formats so
  6. 0:19far so we're going to add one more
  7. 0:21that's the one that is going to help us
  8. 0:23work with long immediates what we have
  9. 0:25seen so far that our immediates have
  10. 0:27been limited
  11. 0:28to being 12 bits but occasionally we
  12. 0:30need them to be longer
  13. 0:31so let's try to see a mechanism how we
  14. 0:34can
  15. 0:34use all 32 bits as an immediate value
  16. 0:39but before then let's recap how our
  17. 0:41branches work because they'll help us
  18. 0:43develop a bit of an intuition what we
  19. 0:45are trying to do
  20. 0:47our branches have that
  21. 0:51immediate field and what we have done so
  22. 0:53far we have
  23. 0:55hand compiled what replaces the value
  24. 0:58that replaces the label
  25. 0:59in the branch that label corresponds to
  26. 1:02the offset
  27. 1:03how many instructions away from our
  28. 1:06current
  29. 1:06program counter is our target branch
  30. 1:13now what happens if we move that
  31. 1:15instruction in the code
  32. 1:17well if you just move it branch
  33. 1:19instruction ahead of another instruction
  34. 1:21that we had in a code
  35. 1:22we have to clearly
  36. 1:26change the the the value that sits in
  37. 1:29the label
  38. 1:30that replaces the label because we have
  39. 1:32changed the distance
  40. 1:33relative distance between the two
  41. 1:35instructions
  42. 1:36so the relative offset from the program
  43. 1:39counter is not the same
  44. 1:41but often what we are going to see will
  45. 1:44be dealing with a movable code
  46. 1:46so most of these compilations are done
  47. 1:48by the assembly
  48. 1:50uh process and we often will link
  49. 1:53different procedures and different
  50. 1:55functions
  51. 1:56with external libraries so the code may
  52. 1:58be moving
  53. 2:00in in the memory or most generally will
  54. 2:04be moving in the memory so when we move
  55. 2:06a piece of a code the entire piece of a
  56. 2:08code in memory that includes
  57. 2:10loops and branches inside of it does it
  58. 2:14and do we need to replace retarget
  59. 2:17all of our labels and the answer is no
  60. 2:20and that's an advantage
  61. 2:22of pc relative addressing and what
  62. 2:25um there is also a term used for that
  63. 2:28that this
  64. 2:29is now position independent code so if
  65. 2:31you move a whole procedure
  66. 2:34to a new location all
  67. 2:38relative addressing is preserved there
  68. 2:40is another
  69. 2:41important thing to keep in mind is that
  70. 2:43the range of our branches since we are
  71. 2:46really limited to 13 bit offsets where
  72. 2:49we
  73. 2:50drop the last bit we keep the 12 bits in
  74. 2:52our instruction encoding
  75. 2:54the range is limited to plus minus 1024
  76. 2:58instructions away from the branch
  77. 3:01instruction
  78. 3:02so what happens if we need to branch
  79. 3:04really but it may happen that we have to
  80. 3:06branch
  81. 3:07to a location that is outside plus minus
  82. 3:091024 instructions
  83. 3:12um we need help we can do that with a
  84. 3:14single branch instruction
  85. 3:16um and here is an example how this will
  86. 3:19happen
  87. 3:20so in this case we have an instruction
  88. 3:23branch on equal
  89. 3:24extend x0 to a far away location that is
  90. 3:27outside of
  91. 3:28our 20 024 range so this
  92. 3:31compares the value in the register x10
  93. 3:33with a 0
  94. 3:34and then branches to a far location
  95. 3:38if x10 is equal to 0 if it is not equal
  96. 3:41to 0 that goes
  97. 3:43and executes the next instruction in
  98. 3:45sequence
  99. 3:47now the way how we would do this we are
  100. 3:49going to go flip this
  101. 3:50branch condition so we are going to
  102. 3:52branch on if not
  103. 3:53equal when x10 is not 0
  104. 3:57to the next and next here is our next
  105. 4:00instruction that was supposed to be
  106. 4:02executed but in between if
  107. 4:05x10 is actually equal to zero we are
  108. 4:07going to put a jump
  109. 4:08and the idea here is that the jump is
  110. 4:11going to have
  111. 4:12a much longer reach than our
  112. 4:15branches so that's what you're going to
  113. 4:18see
  114. 4:19as a consequence of the next little
  115. 4:22section
  116. 4:23about long immediates so
  117. 4:26there are there is a format that helps
  118. 4:29support
  119. 4:30long immediates this is a u format
  120. 4:33that stands for upper immediate
  121. 4:36instructions or some people
  122. 4:37say unusually long immediates
  123. 4:41in the instruction um
  124. 4:45remember our immediates were limited so
  125. 4:47far to 12 bits
  126. 4:49in the instructions in the i format
  127. 4:52also in the branches and and in in
  128. 4:55stores
  129. 4:57and we got that by reusing the
  130. 5:01one of the register fields and the
  131. 5:04the funct7 field now 12 bits
  132. 5:08is good but if you want to put
  133. 5:12load the entire 32-bit operand
  134. 5:15into a register uh all the entire 32-bit
  135. 5:19value into a register
  136. 5:22then we are missing 20 bits so
  137. 5:25the u format provides a way
  138. 5:28for getting these missing 20 bits into
  139. 5:32our register
  140. 5:33so here is how the instruction looks
  141. 5:35like it's fairly straightforward
  142. 5:37it has an opcode in the lowest seven
  143. 5:39bits there
  144. 5:40there should be no surprise this is the
  145. 5:42same field what we have seen before
  146. 5:44and there are two different top codes
  147. 5:46for two instructions that we have there
  148. 5:48the instructions are lui and now epc
  149. 5:52louis stands for load upper immediate
  150. 5:56essentially loads an upper immediate
  151. 5:59into a register destination register rd
  152. 6:03and leaves the bottom 12 bits resets the
  153. 6:06the bottom 12 bits to zeros and then
  154. 6:10au pc adds upper immediate
  155. 6:13to pc to the program counter
  156. 6:17and stores the result in the destination
  157. 6:19register rd
  158. 6:21now there are two separate top codes
  159. 6:23that we are using for these two
  160. 6:25instructions and that's expensive
  161. 6:27there is an interesting trick that is
  162. 6:29happening here it's outside of this
  163. 6:30class
  164. 6:31the top code is used across different
  165. 6:33variants
  166. 6:34of the risk 5 instruction set
  167. 6:36architecture
  168. 6:37rv 32 64 128
  169. 6:41now so
  170. 6:44what we need for this instruction is
  171. 6:45essentially 12 bits to say
  172. 6:48what kind of instruction it is and what
  173. 6:50is the destination register
  174. 6:52we have miraculously freed up the top 20
  175. 6:54bits
  176. 6:55and that's where we can put our 20-bit
  177. 6:57immediate
  178. 6:58very well let's see how we use this so
  179. 7:01louis writes
  180. 7:02the upper 20 bits in the destination
  181. 7:05with the immediate value and clears the
  182. 7:06lower 20 bits
  183. 7:08so when we would like to put in a 32-bit
  184. 7:12immediate into a destination register we
  185. 7:15need to chop
  186. 7:16that immediate into two halves
  187. 7:19this has happened to some other louis
  188. 7:21which was a french king
  189. 7:23any similarities are accidental here so
  190. 7:27we are going to cut this
  191. 7:30immediate into two hops first we are
  192. 7:32going to louie the upper 20 bits
  193. 7:35into the destination register that is
  194. 7:36shown in here
  195. 7:38in x10 that is going to put
  196. 7:41the upper five nibbles into that
  197. 7:45destination register it is going to
  198. 7:47leave the lower
  199. 7:49three nibbles as zeros then we can
  200. 7:52simply do
  201. 7:53add immediate of the value that contains
  202. 7:56the lower three nibbles so in this case
  203. 7:58we would like to put the final value
  204. 7:59eight seven six five four three two one
  205. 8:02into a register x10 and we did it in two
  206. 8:05steps in the first step
  207. 8:06lui put eight seven six five four
  208. 8:10into the upper 20 bits or upper
  209. 8:13five nibbles and then add immediate
  210. 8:16finish that by adding three to one
  211. 8:20to the bottom this worked fine
  212. 8:23but there is a catch it does not always
  213. 8:27work
  214. 8:28out of the box here is an issue dead
  215. 8:31beef is a completely legit
  216. 8:34hexadecimal number so how do we
  217. 8:37load that beef into the register x7 x10
  218. 8:41so let's try to follow the same
  219. 8:42procedure
  220. 8:44first we go and load the
  221. 8:48the louis the first part that b
  222. 8:51all right that's good so now x10
  223. 8:53contains the value
  224. 8:55that b000 and then if we add the
  225. 8:58immediate
  226. 9:00if there we end up with dead
  227. 9:03a if oh what happened here we were
  228. 9:06supposed to get dead beef
  229. 9:07we got that eighth
  230. 9:10and the problem there is remember
  231. 9:15add immediate looks at this
  232. 9:18the most significant bit of e and
  233. 9:22sine extends it so since it found a
  234. 9:25one in the most significant bit position
  235. 9:27of hexadecimal value e
  236. 9:29it sign extended it all the way to the
  237. 9:32upper 32 bits
  238. 9:34so when adding these operands in the
  239. 9:36upper five nibbles
  240. 9:38it placed all once which is equivalent
  241. 9:39to a minus one
  242. 9:41that's why this bit b
  243. 9:44the the the nibble that contain b got
  244. 9:48decremented
  245. 9:50so that's a problem this doesn't sound
  246. 9:52right so what do we do well
  247. 9:54we know that that this is not right so
  248. 9:57but can't we just use add immediate
  249. 10:00unsigned isn't there an instruction like
  250. 10:02that
  251. 10:02nope there is no
  252. 10:05add immediate unsigned in risk five
  253. 10:09remember we basically ran out of fund
  254. 10:11three um
  255. 10:13space and there was no room for another
  256. 10:16instruction in there so ad immediate was
  257. 10:20voted off the island
  258. 10:21uh at immediate unsigned was voted off
  259. 10:23the island um
  260. 10:25so we have to live with what we got here
  261. 10:27we would you know in order to add more
  262. 10:29instruction
  263. 10:30we had we would have had to spend
  264. 10:33another
  265. 10:34um up field
  266. 10:37up code field so what do we do well we
  267. 10:39know that what is going to happen we are
  268. 10:41going to have
  269. 10:42one less one bit lower
  270. 10:46value in the fifth
  271. 10:49nibble from the top so we are going to
  272. 10:54on purpose make it a little bit
  273. 10:57bigger bigger by a value of one so we
  274. 11:00have added
  275. 11:02plus one here
  276. 11:05this is supposed to be plus one
  277. 11:09so how do we set that beef in register
  278. 11:12extent we first write
  279. 11:14dead c by using a lui and then we add
  280. 11:18immediate
  281. 11:20if value to that and that is going to
  282. 11:22set
  283. 11:23the correct value in register x 10.
  284. 11:27now there is good news this is a known
  285. 11:31thing known procedure so every time
  286. 11:34we have that a compiler can take care of
  287. 11:37things
  288. 11:37so there is a pseudo instruction so the
  289. 11:41operation
  290. 11:42that is load immediate and whenever we
  291. 11:45try
  292. 11:46to load a long
  293. 11:49immediate into register any but in this
  294. 11:52case x then
  295. 11:53if we just write li x10 and the value
  296. 11:57is going to do the proper thing it is
  297. 12:00going to
  298. 12:00break it up into two proper instructions
  299. 12:02that will finish the job for us
  300. 12:04so just don't do this louis addy just
  301. 12:07use
  302. 12:08li in assembly code and you get yourself
  303. 12:10set with the appropriate
  304. 12:13long immediate okay
  305. 12:16that's it for louis for now we are going
  306. 12:18to see some
  307. 12:20few more uses of blue in the next
  308. 12:22segment but for now
  309. 12:25let's see one missing instruction that
  310. 12:27we have
  311. 12:28that we mentioned earlier on which is
  312. 12:31aui pc that
  313. 12:32adds upper immediate value to the
  314. 12:34current content of the program counter
  315. 12:36and stores the result
  316. 12:37in the destination register rd so this
  317. 12:40is great for
  318. 12:41pc relative addressing because we can
  319. 12:44add offsets to the current content of
  320. 12:48the program counter
  321. 12:49that sounds great in the simplest case
  322. 12:52something that we
  323. 12:53really wanted to have early on
  324. 12:56if we add a zero
  325. 13:00we use zero as the immediate value
  326. 13:04in our pc we are simply storing
  327. 13:07about the current value of the program
  328. 13:10counter in the destination register
  329. 13:12it's incredibly convenient that's
  330. 13:14essentially our return address
  331. 13:16so uh well by doing this we can put an
  332. 13:19address
  333. 13:21of a label in um
  334. 13:25in our epc by or in
  335. 13:28register x10 by saying iepc x10
  336. 13:32comma 0. that's it that wraps up upper
  337. 13:35immediates
  338. 13:36we are going to see one final format
  339. 13:39after the break

About this transcript

This page contains the full transcript of [CS61C FA20] Lecture 12.2 - RISC-V Instruction Formats II: Upper Immediates by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 1,901 words across 339 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.