YouTube2Text

[CS61C FA20] Lecture 11.2 - RISC-V Instruction Formats I: R-Format Layout — Transcript

by CS 61C Departmental · 1,838 words · 331 segments · language en · Watch on YouTube

Full transcript

  1. 0:00[Music]
  2. 0:09welcome back to wrist 5
  3. 0:10machine language which is our way of
  4. 0:13representing assembly instructions in a
  5. 0:14binary format such that the processor
  6. 0:16can understand them
  7. 0:18keep in mind that we don't study this
  8. 0:21because we have a particular joy of
  9. 0:23staring at binary representations of
  10. 0:25instructions
  11. 0:26but we do that because it will be
  12. 0:28tremendously helpful later on when we
  13. 0:31try to
  14. 0:31build a processor rs5 processor that can
  15. 0:35execute these instructions
  16. 0:36we need to be able to speak at a
  17. 0:38language that
  18. 0:40processor understands because we don't
  19. 0:43have another design method we don't
  20. 0:45still have a still don't have a system
  21. 0:47that where we can do
  22. 0:48this computer here is there is five
  23. 0:52specification
  24. 0:53build a processor for me such that it
  25. 0:55meets that spec
  26. 0:56may happen at some point but not
  27. 1:00quite right now so let's get into these
  28. 1:02instruction formats
  29. 1:04we have seen that all instructions in
  30. 1:07risk 5
  31. 1:08fit 32-bit words so each one of them is
  32. 1:12going to be play encoded as a 32-bit
  33. 1:14word
  34. 1:15it is going to follow a very rigid
  35. 1:17format which is going to be
  36. 1:20somewhat different between six different
  37. 1:22instruction types
  38. 1:23the first type that we are going to look
  39. 1:25at are register to register
  40. 1:28instructions for arithmetic and logic
  41. 1:31operations
  42. 1:32so let's take a look at the r format
  43. 1:35layout
  44. 1:36in the right our format layout we said
  45. 1:39each instructions
  46. 1:40is 32 bits wide so it
  47. 1:44covers bits 0 to 31
  48. 1:47and it has particular fields it is
  49. 1:50divided into variable with fields
  50. 1:53that sum up to a total of 32 bits
  51. 1:57on the bottom on the lowest least
  52. 2:00significant bits
  53. 2:01we have the op code off code is
  54. 2:04seven bits wide and occupies positions 0
  55. 2:08to 6 then it's followed by
  56. 2:11the binary code for the destination
  57. 2:14register notice it's 5 bits wide which
  58. 2:16is enough to represent any of the 32
  59. 2:19registers that we have in the risc-5
  60. 2:22architecture
  61. 2:23and occupies with positions seven to
  62. 2:26eleven
  63. 2:26then it's followed by another field here
  64. 2:28this is called func
  65. 2:29three three bits wide
  66. 2:33twelve to fourteen bit positions then
  67. 2:36go follows rs1 and rs2
  68. 2:39register fields each are five bits wide
  69. 2:42and finally
  70. 2:43there is a funct seven field that is
  71. 2:46seven bits wide
  72. 2:47from 25 to 31. all
  73. 2:51register based arithmetic and logic
  74. 2:54instructions
  75. 2:55look alike we said that before
  76. 2:58it always has an instruction the name of
  77. 3:01the instruction
  78. 3:03then fields one two three where one
  79. 3:05corresponds to the destination register
  80. 3:07two corresponds to the first source
  81. 3:11register
  82. 3:11three corresponds to the second source
  83. 3:13register and we see that here
  84. 3:15we have the the place to
  85. 3:19put the which one of the of the
  86. 3:21registers is
  87. 3:22our destination register and we have
  88. 3:24spaces for our
  89. 3:26two source registers let's take a look
  90. 3:30a bit more what are these op codes and
  91. 3:33funct 3 and func 7 fields those are the
  92. 3:36ones
  93. 3:37that specify what kind of an instruction
  94. 3:40do you want to execute
  95. 3:41right so op code functs 3 and func 7
  96. 3:45are going to tell us whether we want to
  97. 3:47do add subtract
  98. 3:49shift left or xor and so on
  99. 3:53and these are going to be the registers
  100. 3:57that we are targeting
  101. 3:58notice one important thing all opcodes
  102. 4:02all instructions that are of our type
  103. 4:04are going to have the same op code
  104. 4:07that is going to be 0 1 1 0 0 1 1
  105. 4:10every single one of them and and or are
  106. 4:13all are going to
  107. 4:14to have 0 1 1 0 0 1
  108. 4:171 in the op code field that means that
  109. 4:20different instructions are going to be
  110. 4:22encoded in funct 3 and func 7 fields
  111. 4:27but then you you you may scratch your
  112. 4:29head oh
  113. 4:30um why do i need
  114. 4:3410 bits to encode
  115. 4:37about 10 instructions i mean with 10
  116. 4:39bits i can encode
  117. 4:411024 different instructions are we going
  118. 4:44to encode them such that
  119. 4:45each one of them is just has one bit
  120. 4:48equal to one
  121. 4:49no but it is true that we don't need
  122. 4:531024 we don't need to utilize all funct
  123. 4:553 and func 7 fields
  124. 4:58bits in function and function fields to
  125. 5:00represent all arithmetic and logic
  126. 5:02instructions
  127. 5:03so we have some redundancy there we have
  128. 5:05some options how we want to choose that
  129. 5:07encoding
  130. 5:08and we'll find out that we're going to
  131. 5:10use that to make the job
  132. 5:12of a processor in figuring out which
  133. 5:14extra instruction is it going to
  134. 5:16execute easier all right
  135. 5:20the other question is why are these you
  136. 5:22may ask why are these op code and
  137. 5:24functioning and
  138. 5:25seven fields spread all over the place
  139. 5:29we'll see that later it is again to make
  140. 5:31processor job
  141. 5:33easier
  142. 5:36now we have mentioned that each
  143. 5:37instruction has register specifiers
  144. 5:40and they're placed at rigid spots we
  145. 5:43have a spot for the
  146. 5:44destination register for the source
  147. 5:46register
  148. 5:47and the other source register
  149. 5:50each uh register holds a five bit
  150. 5:54unsigned
  151. 5:54integer which is just enough that we
  152. 5:58need
  153. 5:58to identify one of the 32 registers that
  154. 6:02we have in the
  155. 6:02s5 ic so there is no redundancy there
  156. 6:05and no need to have any redundancy there
  157. 6:07so let's take a look at a practical
  158. 6:09example how we do that
  159. 6:12let's encode the first instruction that
  160. 6:14we have learned for register based
  161. 6:15operations that's the add
  162. 6:18and the format of the ad is add rd rs1
  163. 6:21rs2 so in this case add will operate
  164. 6:25on the contents of the register 6 19 and
  165. 6:27x10
  166. 6:29take them and them together and place
  167. 6:30the result in the register destination
  168. 6:33register x18
  169. 6:35so what we have here is
  170. 6:40relatively straightforward when you look
  171. 6:42into it first we are going to take
  172. 6:44write down the op code all r type
  173. 6:47instructions
  174. 6:48have the same opcode that opcode
  175. 6:51is 0 1 1 0 0 1 1
  176. 6:54then we're going to look up in the green
  177. 6:56sheet and find out
  178. 6:58that add instruction corresponds to all
  179. 7:01zeroes
  180. 7:02in funct seven and func three fields
  181. 7:06and then finally we need to fill out the
  182. 7:08numbers that correspond to the registers
  183. 7:10that we are using
  184. 7:11so in this case we look up where does
  185. 7:14the destination register x18 go
  186. 7:17oh it's supposed to go into the rd field
  187. 7:19so we
  188. 7:20write the binary value for 18 which is
  189. 7:23one zero zero one zero sixteen plus two
  190. 7:26all right we got that one and we do the
  191. 7:29same
  192. 7:30with rs1 and rs2 registers rs1 is 19
  193. 7:33which is 16 plus 2 plus 1
  194. 7:36and then finally rs2 right here
  195. 7:40is 0 1
  196. 7:430 1 0 which is 10.
  197. 7:46all right we figure that out hopefully
  198. 7:48that made sense
  199. 7:50how about you do this one and
  200. 7:53what is the correct encoding of add x4
  201. 7:55x3 x2
  202. 7:57and ignore that i might be using some
  203. 8:01reserve registers for some things this
  204. 8:03is just an example
  205. 8:04of register based edition
  206. 8:08i'll pause here or you can pause me here
  207. 8:12do it yourself and then i'm going to
  208. 8:15continue
  209. 8:16and work it out in this case we actually
  210. 8:19don't need to
  211. 8:21write all 32 bits let's try to guess
  212. 8:24which one
  213. 8:25is is right the correct representation
  214. 8:29so let's take a look first
  215. 8:32all register based instructions
  216. 8:35end with 0
  217. 8:390 1 1 which is a hexadecimal
  218. 8:42representation
  219. 8:43of 3 and notice that all our answers
  220. 8:46are offered here as hexadecimal numbers
  221. 8:50so i should just go ahead and eliminate
  222. 8:53the one that does not end with three
  223. 8:58the other ones we have no options the
  224. 9:00next
  225. 9:01significant digit to be either three or
  226. 9:04b
  227. 9:04which one is that so let's take a look
  228. 9:06at our drd is x4
  229. 9:08the last bit of x4 is zero
  230. 9:14x4 is zero one
  231. 9:18zero zero so then it has to be
  232. 9:21if i take these bottom three bits and
  233. 9:24add a zero that is that has to be
  234. 9:26here then that's going to be zero zero
  235. 9:30one one
  236. 9:31therefore the next significant bit needs
  237. 9:34to be
  238. 9:34three as well so you can cross these two
  239. 9:37off
  240. 9:38so which one is it four zero two one
  241. 9:42or zero zero two one well we
  242. 9:45know that add
  243. 9:50has all zeros in the leading position so
  244. 9:52we can eliminate this one
  245. 9:55and four is our correct answer but if
  246. 9:57you would like to
  247. 9:58to verify that you can go ahead and
  248. 10:01write down
  249. 10:02all 32 binary digits
  250. 10:05and convert them to hexadecimal it's a
  251. 10:07short exercise
  252. 10:10well this is a summary of all
  253. 10:14of our r type instructions we have seen
  254. 10:17them all
  255. 10:20we have the the encoding for all of them
  256. 10:23in one table so what we find out they
  257. 10:27all have the same
  258. 10:29op code in the end so zero one one
  259. 10:33zero zero one one and they have they
  260. 10:35have
  261. 10:36the same spots for the destination and
  262. 10:38the two source registers
  263. 10:40and they have different funct seven and
  264. 10:43func three fields
  265. 10:46so the instructions that we have here
  266. 10:48are
  267. 10:49add sub sll slt
  268. 10:53sll stands for shift left logical what
  269. 10:55is slt
  270. 10:58it's a new instruction it is set
  271. 11:02on less than it compares the values of
  272. 11:06registers
  273. 11:08source registers rs1 and rs2 and if
  274. 11:10contents of rs1
  275. 11:12is less than rs2 it sets the destination
  276. 11:15register to 1.
  277. 11:17it is useful for comparisons there and
  278. 11:19then there is the unsigned version of
  279. 11:21that
  280. 11:21then we have xor shift write logical
  281. 11:25shift right arithmetic or an end so we
  282. 11:28just added two more
  283. 11:30instructions over here that are very
  284. 11:31useful
  285. 11:33let's take a look a little bit at the
  286. 11:35encoding here
  287. 11:37um one thing that you will spot is that
  288. 11:42add and sub are very similar to each
  289. 11:45other
  290. 11:46and that's done on purpose that way
  291. 11:47because we are going to be using
  292. 11:50the same hardware to perform subtraction
  293. 11:53as we use for addition because
  294. 11:56subtraction is just
  295. 11:58using two's complements number addition
  296. 12:00of two's complements number there is no
  297. 12:02different hardware needed
  298. 12:04except that we need to perform sign
  299. 12:07extension
  300. 12:09so go ahead and circle this
  301. 12:13this bit basically says that i need to
  302. 12:17sign extend
  303. 12:19an operand before i perform subtraction
  304. 12:22whichever instruction
  305. 12:23has that bit equal to one
  306. 12:26this one what's that instruction oh
  307. 12:29that's shift right arithmetic that's the
  308. 12:30another one that does the sign extension
  309. 12:32so this
  310. 12:33bit here signifies that instruction
  311. 12:38is going to do sign extension
  312. 12:46it basically tells a processor
  313. 12:49you're doing register type instruction
  314. 12:53and you're supposed to
  315. 12:56start with sign extension here so very
  316. 12:59early on
  317. 13:00it can start extending the sign of the
  318. 13:02operand
  319. 13:04very useful hint over there and
  320. 13:07then you're going to see some other
  321. 13:09similarities through this table
  322. 13:10you don't have to pay attention to that
  323. 13:12now but there is some cleverness that is
  324. 13:14put
  325. 13:15in the way how they're encoded i'm going
  326. 13:17to pause here and
  327. 13:20we back in just a bit to talk about
  328. 13:23the next type of the next for
  329. 13:27instruction format which is our
  330. 13:29immediates
  331. 13:31see you there

About this transcript

This page contains the full transcript of [CS61C FA20] Lecture 11.2 - RISC-V Instruction Formats I: R-Format Layout by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 1,838 words across 331 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.