YouTube2Text

[CS61C FA20] Lecture 12.1 - RISC-V Instruction Formats II: B-Format — Transcript

by CS 61C Departmental · 3,054 words · 554 segments · language en · Watch on YouTube

Full transcript

  1. 0:00[Music]
  2. 0:10hey
  3. 0:11hello welcome back to risk 5 instruction
  4. 0:14encoding
  5. 0:15we have covered three types of
  6. 0:17instruction formats so far
  7. 0:20those the the first format that we
  8. 0:22covered was
  9. 0:23the r format that covered r type
  10. 0:25instructions which are those
  11. 0:26instructions that operate
  12. 0:28between the registers in the
  13. 0:30microprocessor for arithmetic and logic
  14. 0:33instructions then we covered the i
  15. 0:35format that covered
  16. 0:37immediate operations with the immediates
  17. 0:40and then we covered the s format
  18. 0:41that covered stores let's take a look at
  19. 0:45the
  20. 0:46b format which will be covering branches
  21. 0:49and let's see what is a subtle detail
  22. 0:53why we needed yet another
  23. 0:56instruction format for branches first
  24. 0:59let's take a look at what let's recall
  25. 1:01what the branches do
  26. 1:04conditional branches evaluate
  27. 1:07a condition between the registers
  28. 1:10in this case we will need two source
  29. 1:12registers x1 and x2
  30. 1:14and this branch if equal will compare
  31. 1:17the values that are in registers x1 and
  32. 1:19x2
  33. 1:20and branch to a label which is another
  34. 1:24address in the code if that condition is
  35. 1:27met
  36. 1:28if the condition is not met if the value
  37. 1:30in x1 is not equal to the value in x2
  38. 1:33it will execute the next extraction in
  39. 1:36sequence
  40. 1:38so one thing that we notice branch
  41. 1:40instructions
  42. 1:42could use the same or similar format
  43. 1:45as the stores like the stores branches
  44. 1:49need two source registers they don't
  45. 1:51need the destination register
  46. 1:52and they need some immediate value to
  47. 1:55represent
  48. 1:56where is the label or some
  49. 1:58representation of the label is needed
  50. 2:01so the main question here is how do we
  51. 2:03encode that value
  52. 2:05of a label where do we branch to
  53. 2:09in order to answer that let's take a
  54. 2:11look at what
  55. 2:12do branches do and what are they usually
  56. 2:15being used to
  57. 2:16so branches in our assembly code are a
  58. 2:19product
  59. 2:20often of loops we have seen that there
  60. 2:22are three types of loops if
  61. 2:24else while in four in c that tend to get
  62. 2:27translated into branches
  63. 2:32these loops that when translated to the
  64. 2:35assembly code are
  65. 2:36relatively of a short length you know
  66. 2:38the number of instructions that are
  67. 2:39inside
  68. 2:40the loop is typically less than 50
  69. 2:44maybe a few hundred at the maximum um
  70. 2:47and if we need to go to some
  71. 2:50farther away part of a code like a
  72. 2:52procedure
  73. 2:53we would use a different type of an
  74. 2:55instruction which would be a jump
  75. 2:57which we'll see is of a j format
  76. 3:00the other important observation is about
  77. 3:03where the code is
  78. 3:05generally we know that the code resides
  79. 3:09in the memory and it is in a separate
  80. 3:11location
  81. 3:12from the data so it is in the
  82. 3:16limited part of a memory and it does not
  83. 3:18mix with the data
  84. 3:20in general that code or sometimes
  85. 3:23traditionally has been called
  86. 3:25text for some old reasons a
  87. 3:28code is a subset of
  88. 3:32our memory addresses and the maximum
  89. 3:35reach of a branch
  90. 3:36should not exceed the size of the code
  91. 3:38otherwise we'll be
  92. 3:39branching into a part of the memory
  93. 3:41where there is no program so
  94. 3:43things would not make sense the other
  95. 3:45thing to keep in mind
  96. 3:47as we are executing these loops the
  97. 3:49current address
  98. 3:50current address that we are
  99. 3:53where the instruction is that we are
  100. 3:54executing points to the program counter
  101. 3:57and as we
  102. 3:57execute them in sequence the program
  103. 3:59counter increments
  104. 4:01and then when we hit the branch it goes
  105. 4:04back
  106. 4:04to the original position and then we
  107. 4:06keep executing
  108. 4:07and that's our loop so
  109. 4:11the address of a current of the
  110. 4:13instruction that is currently being
  111. 4:14executed
  112. 4:15is in the program counter so it makes
  113. 4:18sense
  114. 4:19to use so-called pc relative addressing
  115. 4:23for the branches what does that mean pc
  116. 4:26relative means
  117. 4:27that we are going to use immediate
  118. 4:31value to point to the offset
  119. 4:35to calculate the offset from the current
  120. 4:38instruction that is in the program
  121. 4:40counter
  122. 4:44so in which units should that
  123. 4:48offset be expressed first if we are
  124. 4:51using
  125. 4:52very similar format to what we have used
  126. 4:54for stores
  127. 4:56as types we know that we can use up to
  128. 4:5912 bits
  129. 5:00to encode our offsets
  130. 5:03these 12 bits come from repurposing the
  131. 5:06destination register that we don't need
  132. 5:08there are five bits there and seven bits
  133. 5:11for the func seven field
  134. 5:13so that gives us 12 bits or a total
  135. 5:17range of
  136. 5:17two to the twelve addresses or plus
  137. 5:21minus two to the eleventh addresses when
  138. 5:24we need to go
  139. 5:25both forwards and backwards
  140. 5:28so what should we use as the unit of
  141. 5:30addressing should we use bytes as we
  142. 5:31used in stores
  143. 5:33no because we don't want to branch into
  144. 5:36a middle of an
  145. 5:36instruction somewhere in the code
  146. 5:39remember our code
  147. 5:40in the the the rb32 base instruction set
  148. 5:45consists of instructions that are always
  149. 5:4732 bits wide
  150. 5:49so if we branch into an address that is
  151. 5:52not aligned with the start of an
  152. 5:54instruction we would be
  153. 5:57branching into what looks like a garbage
  154. 6:00to us
  155. 6:01so we don't want to branch into the
  156. 6:02middle of an instruction
  157. 6:04so i think the other option is just to
  158. 6:06branch
  159. 6:07to the beginning of the instruction
  160. 6:09always that branch should be divisible
  161. 6:12branch value should be divisible left by
  162. 6:14four
  163. 6:15and it has also another benefit if we
  164. 6:18always branch the start of the word
  165. 6:22is that we have now increased the reach
  166. 6:24of our branches
  167. 6:26it is 2 to the plus minus 2 to the 11
  168. 6:30times the number of words and each word
  169. 6:32is 4 bytes are 32
  170. 6:34bits so that gives us you know tens of
  171. 6:36kilobytes of range for our branch
  172. 6:39which is nice
  173. 6:42so here is how the branch calculation
  174. 6:45would work
  175. 6:46if we don't take the branch our program
  176. 6:49counter
  177. 6:50would point to the next instruction in
  178. 6:53the code
  179. 6:54where is the next instruction in the
  180. 6:55code
  181. 6:57that is you know follows in in our code
  182. 7:00well it is in the next word which is
  183. 7:03exactly four bytes away
  184. 7:05we have seen that before if we do take
  185. 7:07the branch
  186. 7:08we need to calculate the offset with
  187. 7:10respect to the program counter
  188. 7:13and that offset is going to be
  189. 7:17the immediate value or 12 bits
  190. 7:2012 bit wide long immediate times
  191. 7:244 because in this scenario
  192. 7:27we would be calculating that
  193. 7:31offset in the unit of words
  194. 7:35and keep in mind keep in mind that this
  195. 7:37immediate value
  196. 7:39can be positive or negative
  197. 7:42making the branch go forwards or
  198. 7:45backwards
  199. 7:46but this is not really how we do it
  200. 7:50in risk five keep in mind this is not
  201. 7:53true
  202. 7:53there is a quirk a network
  203. 7:57is in a feature of the isa that supports
  204. 8:02um so-called compressed instruction set
  205. 8:05so what is that now we haven't
  206. 8:07talked about that before compressed
  207. 8:09instruction set
  208. 8:10is a subset of the isa
  209. 8:13that has 16-bit instructions now 32
  210. 8:17we have talked about 32 6428
  211. 8:20rb 32 rv 64 rv
  212. 8:24128 but all the instructions were 32
  213. 8:27bits except
  214. 8:27there is a subset that uses 16-bit
  215. 8:31instructions
  216. 8:33why why is that because in some
  217. 8:38applications
  218. 8:40this code size really matters
  219. 8:43so you want to have your code have the
  220. 8:46smallest possible
  221. 8:47footprint so that is usually for really
  222. 8:49inexpensive
  223. 8:50consumer devices a good example of those
  224. 8:54are like flash memory keys
  225. 8:57and things like those over there if you
  226. 9:01need to use more of a memory to store
  227. 9:04the your code you have to pay more for
  228. 9:07that
  229. 9:08and people don't want to do that
  230. 9:11so that code compactness really matters
  231. 9:14um so that's why risk 5 supports
  232. 9:16so-called
  233. 9:17a compressed instruction format and
  234. 9:19those instructions are
  235. 9:2016 bits wide and don't confuse that with
  236. 9:24rvs other rv
  237. 9:27types all those are 32-bit instructions
  238. 9:30now
  239. 9:32here is a catch
  240. 9:36we have we may have a code that has
  241. 9:39both 32-bit instructions and 16-bit
  242. 9:42instructions
  243. 9:43so what do we do we need to be able to
  244. 9:46branch into 16-bit instructions
  245. 9:50that may be offset by two bytes from the
  246. 9:53start of the word
  247. 9:55so that's why risk five does not this
  248. 9:58made a
  249. 9:59decision not to have two different types
  250. 10:02of branches
  251. 10:03to just have one type of a branch that
  252. 10:05is
  253. 10:06able to index in you know by
  254. 10:10two bytes two that is able to calculate
  255. 10:13the offset by two bytes
  256. 10:15so by default all risk 5 branches
  257. 10:19use this unit that multiplies the
  258. 10:22immediate
  259. 10:23by 2.
  260. 10:26so that is less of a reach than we would
  261. 10:30have had
  262. 10:31if we were calculating the offset
  263. 10:34with respect to words but it is still
  264. 10:37good enough
  265. 10:40keep in mind though that
  266. 10:44in this class we are only using 32-bit
  267. 10:46instructions we are not
  268. 10:47studying a compressed instruction set
  269. 10:49that means that
  270. 10:51if we don't correctly specify that
  271. 10:53offset
  272. 10:54we could branch into a middle of an
  273. 10:56instruction and that would not be good
  274. 10:57that would
  275. 10:58in our case produce garbage would read a
  276. 11:01garbage
  277. 11:02instruction so as a result
  278. 11:05the range of our
  279. 11:08branches is plus
  280. 11:11minus 1 kilowatt or
  281. 11:154 kilobytes so let's take a look at now
  282. 11:18how does the
  283. 11:19actual branch calculation look like
  284. 11:23if we don't take the branch
  285. 11:26we are going to execute the next
  286. 11:28extraction in the code which is four
  287. 11:29bytes away
  288. 11:31if we do take the branch we
  289. 11:34are taking we are loading a new value in
  290. 11:38the program counter
  291. 11:39that equals to the value of the
  292. 11:41immediate times two
  293. 11:43remember that it is always times two so
  294. 11:47those offsets are always going to be
  295. 11:50even numbers i'm gonna see an example
  296. 11:53just in a second
  297. 11:54but we have arrived to so we can see
  298. 11:58how does the b format actually look like
  299. 12:01so our b format looks really complicated
  300. 12:04the most complicated format that we have
  301. 12:05so
  302. 12:06seen so far but it actually is not it is
  303. 12:09pretty much the same
  304. 12:10as the s format because we'll find the
  305. 12:14op
  306. 12:14code in the familiar place then we'll
  307. 12:16find func 3 in the familiar place which
  308. 12:19will specify the type of a branch that
  309. 12:21we
  310. 12:21are going to execute and has rs1 and rs2
  311. 12:25in the familiar places the rest the
  312. 12:29former space that we use for the
  313. 12:31destination register
  314. 12:33and the funct7 fields are basically
  315. 12:37taken by
  316. 12:38the bits that specify the immediate
  317. 12:40values
  318. 12:41except that these immediate bits are
  319. 12:44shuffled over all
  320. 12:45over the place and initially that does
  321. 12:48not quite
  322. 12:49make that much sense
  323. 12:53but we'll see in the next slide how does
  324. 12:55it make sense
  325. 12:56now keep in mind that these 12 bits
  326. 12:59represent
  327. 13:00a 13 bit value because that 13 bit value
  328. 13:04always has the least significant bit
  329. 13:06equal to zero
  330. 13:08as you specified before so
  331. 13:12we don't need to store that last bit the
  332. 13:14least significant bit because we know
  333. 13:16that it is always equal to zero
  334. 13:18we just need to store 12 bits for a
  335. 13:2113-bit
  336. 13:22representational offset
  337. 13:25let's take a look at an example here
  338. 13:28so here is our risk-five code that
  339. 13:30executes a loop
  340. 13:32in in the beginning of that loop there
  341. 13:34is an
  342. 13:36an instruction prime chip equal
  343. 13:39contents of two registers to the end
  344. 13:42so it would branch if the the contents
  345. 13:45if the values in x19 and x10
  346. 13:48are equal to each other it would branch
  347. 13:51to the instruction that is specified by
  348. 13:53the label end
  349. 13:54what is the end right there
  350. 13:58so this instruction is at
  351. 14:01the value of pc
  352. 14:04program counter this instruction is four
  353. 14:07bytes away ppc plus four
  354. 14:10this one is at pc plus eight pc
  355. 14:13plus twelve what is the instruction that
  356. 14:15end points to
  357. 14:17this target instruction what should be
  358. 14:19inside the end
  359. 14:21plus 16 bytes
  360. 14:24so the branch offset is we
  361. 14:28calculated by calculating the number of
  362. 14:29instructions that we have from the
  363. 14:31branch
  364. 14:32and multiplying that by
  365. 14:36the number of bits or the number of
  366. 14:37bytes that we need to
  367. 14:39branch forward so that is 16 bytes
  368. 14:43one note is here if
  369. 14:47we accidentally put a zero here
  370. 14:51as the value for the offset we are
  371. 14:53always
  372. 14:54going to be branching to ourselves
  373. 14:58and that is an unfortunate situation of
  374. 15:00so-called infinite loop
  375. 15:02that we don't have a way to exit out of
  376. 15:06and also keep in mind that the infinite
  377. 15:09loop
  378. 15:09was the address of all the address of
  379. 15:12apple computer
  380. 15:13one infinite loop cupertino california
  381. 15:17plenty of funny names like this one in
  382. 15:20the silicon valley
  383. 15:24okay so offset here is 16 bytes
  384. 15:28and let's see how we should represent
  385. 15:32that
  386. 15:33so let's take a look at make a first
  387. 15:35pass at this instruction encoding
  388. 15:38we have our instruction
  389. 15:41and our it is supposed to
  390. 15:45branch forward by four bytes and we're
  391. 15:48going to determine this number four at
  392. 15:49the compilation time
  393. 15:52um branch does have a new opcode
  394. 15:55um one one zero zero zero
  395. 15:58one one and then we'll find out that
  396. 16:01the func three fields specify the type
  397. 16:04of a branch
  398. 16:05branch if um equal
  399. 16:08and then we need to put the val the
  400. 16:11the numbers of the registers that are in
  401. 16:14rs1 and rs2
  402. 16:15rs1 is x
  403. 16:1819 so we are going to have a binary
  404. 16:20value of 19 in the field for our that
  405. 16:23specifies rs1
  406. 16:24and then we are going to have final
  407. 16:26revalue for
  408. 16:2710 in this field that specifies rs2
  409. 16:32so the last thing that we need to do is
  410. 16:34to figure out how does this immediate
  411. 16:37um get placed in there so we do want to
  412. 16:40specify
  413. 16:41immediate of 16 bytes or actually what
  414. 16:44will turn out to be
  415. 16:46eight byte pairs right remember i
  416. 16:50don't need to keep that least
  417. 16:52significant bit so 16
  418. 16:54bytes is the same as 12 as eight byte
  419. 16:57pairs right
  420. 17:02before we go and figure out exactly what
  421. 17:05were
  422. 17:06what would be the value of the immediate
  423. 17:08let's
  424. 17:09figure out the reasoning for this kind
  425. 17:11of
  426. 17:13strange immediate encoding so here are
  427. 17:16the four types of instructions that we
  428. 17:18have seen so far
  429. 17:20and three of them use you have to encode
  430. 17:23immediates
  431. 17:25so i type encoded immediates
  432. 17:28in the upper bits from
  433. 17:31a bit position 20 to 31
  434. 17:34and that would translate into the
  435. 17:37immediate value that would be placed
  436. 17:39in the bottom 12 bits and the top bit
  437. 17:42would always
  438. 17:43end up being sign extended remember we
  439. 17:46always have to extend the
  440. 17:48value of the of the immediate
  441. 17:51to fill all 32 bits now the next type
  442. 17:54was the s-type
  443. 17:56and here immediate was spread into
  444. 18:00two places into two fields
  445. 18:03the former rd field had lower five bits
  446. 18:07and former funk seven field had the
  447. 18:10upper seven bits so we would put
  448. 18:14together the
  449. 18:15s type of intermediate um by taking bits
  450. 18:197 through 11 the lower 5
  451. 18:23and the upper 7 over here and we would
  452. 18:25extend the one that was in uh
  453. 18:27bit position 31.
  454. 18:32now b type when you take a look at it
  455. 18:35here
  456. 18:38is almost identical to
  457. 18:41the the encoding that we have used
  458. 18:44for the s immediate so all the bits are
  459. 18:48exactly in the same places
  460. 18:50and most importantly the 31st bit is in
  461. 18:53the topmost position the
  462. 18:55most valuable position position so
  463. 18:58that's the one that is going to be
  464. 19:00sign extended we always will find that
  465. 19:01one that needs to be signed extended
  466. 19:03in the in the top most bit position
  467. 19:08and all what we see here what has
  468. 19:11happened is
  469. 19:12we have moved a bit in 7-bit
  470. 19:15from in 7-bit from this bit position
  471. 19:20over there in our encoding the reason
  472. 19:23why this
  473. 19:23looks kind of strange is because we are
  474. 19:27humans it looks strange to us
  475. 19:28this is totally normal to a processor it
  476. 19:31allah it is a lot easier
  477. 19:33because we are helping processor figure
  478. 19:36out
  479. 19:37where the immediates are by keeping them
  480. 19:39mostly in the same place
  481. 19:42so our registers are going to be in the
  482. 19:44same place and most of our immediates
  483. 19:46are going to be found
  484. 19:47in the same place as well
  485. 19:50we'll see there is a benefit to that
  486. 19:54let's finish our example that we started
  487. 19:57uh some time ago
  488. 19:58so we're going to finish uh encoding
  489. 20:00this branch if equal
  490. 20:03instruction with an offset that should
  491. 20:05specify
  492. 20:0616 bytes or eight
  493. 20:09pairs of bytes so
  494. 20:13we are going to encode this value of the
  495. 20:16immediate
  496. 20:17with an actual position
  497. 20:21actual value of 16 will be placed in
  498. 20:24that immediate inappropriate bit
  499. 20:25positions
  500. 20:27so first thing that will notice since
  501. 20:29we're always
  502. 20:30branching by pairs of bytes we don't
  503. 20:34need to store the least significant bit
  504. 20:35it is always zero there is no reason to
  505. 20:38to waste our precious instruction space
  506. 20:42to encode zeros so we'll throw that one
  507. 20:44away
  508. 20:45then we'll take the next
  509. 20:48least significant four bits and place
  510. 20:51them
  511. 20:52in this bit position that was previously
  512. 20:55partially occupied by rd
  513. 20:57then we'll take the next ones place them
  514. 21:00in the
  515. 21:01lower 6 bits of the func 7 field
  516. 21:04add this immediate 11 in the least
  517. 21:08significant bit position where rd was
  518. 21:11and finally put the most significant
  519. 21:15immediate bit into the very first bit of
  520. 21:18the instruction
  521. 21:19and that's it so then we can find out
  522. 21:21exactly what is our value
  523. 21:24well um this bit
  524. 21:27is discarded we need to take these four
  525. 21:30and place them down there and that is
  526. 21:33our immediate
  527. 21:36this wraps up a somewhat lengthy
  528. 21:38discussion on how
  529. 21:40do branch instructions look like here is
  530. 21:42a summary of all six branch instructions
  531. 21:44that we have had
  532. 21:46they have an opcode here it is the same
  533. 21:49for all six of them
  534. 21:51and the funct3 field
  535. 21:54specified which branch type it is
  536. 21:59one thing that they wanted to you to
  537. 22:01notice
  538. 22:03you might have seen that the last two
  539. 22:05bits in the upcode are always 11
  540. 22:08and so far the next two bits are zero
  541. 22:11zero
  542. 22:18this is a feature we are only really
  543. 22:20using the top three bits
  544. 22:22in the func field to specify the type
  545. 22:25of the instruction that we are working
  546. 22:27with the bottom
  547. 22:30four bits are essentially used
  548. 22:34to encode the other
  549. 22:37parts of the isa and to add the
  550. 22:40extensions
  551. 22:43we don't need to pay attention to that
  552. 22:45now this is just for your information
  553. 22:47but i'll pause here see you after a
  554. 22:50break

About this transcript

This page contains the full transcript of [CS61C FA20] Lecture 12.1 - RISC-V Instruction Formats II: B-Format by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 3,054 words across 554 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.