YouTube2Text

[CS61C FA20] Lecture 08.3 - RISC-V lw, sw, Decisions I: Decision Making — Transcript

by CS 61C Departmental · 1,945 words · 343 segments · language en · Watch on YouTube

Full transcript

  1. 0:00[Music]
  2. 0:09welcome back
  3. 0:10to risk five assembly language we have
  4. 0:12learned
  5. 0:13eight risk five assembly instructions so
  6. 0:15far in the next 15 minutes we are
  7. 0:17almost going to double that number the
  8. 0:19instructions that we have learned so far
  9. 0:21deal with addition and subtraction
  10. 0:23operations with the memory
  11. 0:24we can actually build a computer with
  12. 0:26that that would be more like a
  13. 0:28calculator or
  14. 0:30a stack machine we can we could build a
  15. 0:32stack machine those kind of
  16. 0:33things were popular say in the 60s or
  17. 0:3570s
  18. 0:36and all the action there happens on the
  19. 0:39stack
  20. 0:40but in order to support modern
  21. 0:43programming languages
  22. 0:44we need to support computer decision
  23. 0:46making example of that in c
  24. 0:48is the if statement and
  25. 0:51in risk 5 there is no x if statement but
  26. 0:54there is its counterpart i wouldn't say
  27. 0:56it's an equivalent it's a counterpart
  28. 0:58um that that is a set of branch
  29. 1:02instructions
  30. 1:03branch instructions are those that
  31. 1:05support decision making they
  32. 1:06change the the order of executions of
  33. 1:10instructions in the code so
  34. 1:14here is an example of one of those
  35. 1:16instructions
  36. 1:17branch if equal mnemonic is
  37. 1:20beq and it compares
  38. 1:24the contents of the registers reg 1 and
  39. 1:26reg 2
  40. 1:27and if they are the same if they are
  41. 1:30equal
  42. 1:30then it will jump to a label l1
  43. 1:34label l1 is just a symbolic address for
  44. 1:36another part of the code
  45. 1:39otherwise if the contents are not the
  46. 1:41same
  47. 1:42it will execute the next statement
  48. 1:45next instruction that is in in our
  49. 1:48program
  50. 1:50next line that is in our program
  51. 1:53um beq has its counterpart
  52. 1:57b and e which stands for branch
  53. 2:01if not equal it will take a look at the
  54. 2:04contents of two registers and if they're
  55. 2:05not equal
  56. 2:06it will branch there are two types of
  57. 2:10branches in
  58. 2:10assembly and risk five is no different
  59. 2:12than that there are conditional branches
  60. 2:14and unconditional branches
  61. 2:16conditional branches basically evaluate
  62. 2:18something that we
  63. 2:20asked them too um examples what we have
  64. 2:23seen are already branch of equal
  65. 2:25and branch if not equal but we can also
  66. 2:28compare magnitude so we have two
  67. 2:30instructions
  68. 2:31branch if less than and which is blt
  69. 2:35and branch if greater or greater than or
  70. 2:38equal
  71. 2:39which is bge
  72. 2:43and then we have unsigned versions of
  73. 2:45those bltu
  74. 2:46and bgeu
  75. 2:50there is an unconditional branch which
  76. 2:53in risk five is a jump
  77. 2:54it is just encode is j and then a label
  78. 2:58whenever we run into that
  79. 3:02instruction we basically jump to another
  80. 3:05location in a program
  81. 3:07it's like a go to um
  82. 3:11it is always as executed it corresponds
  83. 3:14to an
  84. 3:14always statement now
  85. 3:19when somebody may ask
  86. 3:22oh can we just make an unconditional
  87. 3:25branch out of
  88. 3:26a conditional one for example we say
  89. 3:29branch if equal x0
  90. 3:32x0 label yeah and that will definitely
  91. 3:35always
  92. 3:36jump to a label but there is one
  93. 3:40subtlety
  94. 3:40there the range of that branch the range
  95. 3:43of branches
  96. 3:44is shorter because we are limited
  97. 3:48to 32-bit instructions in risk five
  98. 3:51so these 32-bit instructions you know
  99. 3:54you have to
  100. 3:55encode somehow the type of instruction
  101. 3:58which kind of registers are we comparing
  102. 4:00which one ones of the 32 registers are
  103. 4:02we comparing
  104. 4:03and then there is a little bit to place
  105. 4:05the label
  106. 4:06which is essentially an immediate value
  107. 4:10in jump we just say jump and then have
  108. 4:14some kind of an immediate value so the
  109. 4:16range of an unconditional
  110. 4:17jump is is is longer it's
  111. 4:20reaches farther let's take a look at a
  112. 4:23couple of examples of how
  113. 4:25does an if statement translate to risk
  114. 4:28five
  115. 4:28so here is an a piece of c code
  116. 4:32if i equal equal j compares the
  117. 4:36values of i and j then we should
  118. 4:40say that f equals to g plus h
  119. 4:44let's see how does that translate into
  120. 4:47risk five assembly
  121. 4:49the first thing we need to figure out if
  122. 4:52i and j
  123. 4:53are the same and their contents are in
  124. 4:55the registers
  125. 4:56x13 and x14 and look at this here is
  126. 4:59something that might surprise you
  127. 5:00but totally makes sense instead of
  128. 5:03saying
  129. 5:04branch if equal x13 x14
  130. 5:08we are using b and e we are inverting
  131. 5:12this this
  132. 5:13this uh equal equal
  133. 5:16condition why are we doing that it
  134. 5:19it makes sense because if you think
  135. 5:22about how does the flow
  136. 5:24of execution go here this is what
  137. 5:27happens
  138. 5:29if b and e
  139. 5:34branch if not equal if these two the
  140. 5:36contents of the registers are
  141. 5:38not equal we'll go this way
  142. 5:42if it is not true we'll go down
  143. 5:45so in this case in the
  144. 5:49c in instruction
  145. 5:53we would like to add the two numbers if
  146. 5:56i
  147. 5:56and j are equal which is essentially
  148. 5:59what
  149. 6:00happens automatically on the next
  150. 6:02instruction
  151. 6:03if the branch is not taken so it does
  152. 6:06make sense in this case
  153. 6:08to flip this around and ask oh if
  154. 6:11they're not equal
  155. 6:12then jump around this instruction that
  156. 6:15you would like to do
  157. 6:16if they are equal so if they're not
  158. 6:19equal we'll jump around
  159. 6:21or branch around to be more precise go
  160. 6:24to the exit
  161. 6:25if they are equal then
  162. 6:29the ad will be executed
  163. 6:32hopefully this made sense this is a very
  164. 6:35common thing that we'll do
  165. 6:37and it is very helpful if you think
  166. 6:39about it this way
  167. 6:40the direction of true and direction of
  168. 6:46false here is another example
  169. 6:49it's an if else statement and it's
  170. 6:52exactly the same that except that we
  171. 6:54added the x
  172. 6:57condition here that will take
  173. 7:00f equals to g minus h
  174. 7:03if i is not equal to j now this one
  175. 7:07you can do in different ways and look
  176. 7:09similar
  177. 7:10but let's stick to the idea that we
  178. 7:13always flip this original condition so
  179. 7:15we'll have b
  180. 7:16and e and then if b and e is true
  181. 7:20b and if x 13 and x 14 are not equal
  182. 7:24we will skip to else in else
  183. 7:28while sub sub will subtract
  184. 7:32the two registers
  185. 7:35and then we'll exit if they are
  186. 7:41if b and e does not
  187. 7:45branch if this instruction doesn't
  188. 7:47branch we'll execute the next one so
  189. 7:49we'll add
  190. 7:50the f and j j and h and store it in f
  191. 7:54now there is a common mistake and i've
  192. 7:57made it gazillion times
  193. 7:59and that mistake is by omitting
  194. 8:02this j instruction to exit what happens
  195. 8:05if we don't have it there so then
  196. 8:08if we don't branch
  197. 8:11here we will add the two the contents of
  198. 8:15the two registers and put them in x10
  199. 8:18then if there is no jump we will execute
  200. 8:21the next instruction that will subtract
  201. 8:23the contents of two registers and put
  202. 8:26them in put the result in extend
  203. 8:28and it'll always do that and you'll be
  204. 8:30looking at there debugging this code and
  205. 8:32scratching your head what
  206. 8:33you know why why do i never get this
  207. 8:36ad to to to happen even though i
  208. 8:40wrote it in there well because it gets
  209. 8:41over it and the content or register gets
  210. 8:43overwritten
  211. 8:44so this j this jump is a must
  212. 8:51just a quick words about the magnitude
  213. 8:53comparison risk five
  214. 8:54we said we already have them we have
  215. 8:56branch on less than
  216. 8:58we also have branch on greater or equal
  217. 9:00but we do not
  218. 9:02have a branch on less
  219. 9:06or less than or equal and we do not have
  220. 9:09um branch on greater than y
  221. 9:13because we are trying to save to reduce
  222. 9:16the number of these instructions
  223. 9:18and those are absolutely not necessary
  224. 9:20we get them by flipping
  225. 9:22the order of two operands the contents
  226. 9:26of the two registers
  227. 9:27that we would like to compare so they're
  228. 9:30absolutely not necessary
  229. 9:32but what is necessary is to have an
  230. 9:34unsigned version
  231. 9:35of a branch so there is a bltu
  232. 9:40and bg eu
  233. 9:43that compare the unsigned versions of
  234. 9:46the integers it's really
  235. 9:50important to to remember which one are
  236. 9:52we using when so whenever we are
  237. 9:54comparing
  238. 9:55um signed numbers into this complement
  239. 9:58we are using
  240. 9:59blt and uh if we would like to compare
  241. 10:02unsigned
  242. 10:03it is a bltu now one thing that
  243. 10:07is kind of convenient to remember here
  244. 10:09which instruction do we have
  245. 10:11do we have blt in our instruction set or
  246. 10:14bgt
  247. 10:16well the way how i remember it is by
  248. 10:18remembering the
  249. 10:19ring that there does exist a type
  250. 10:22of a blt sandwich but i have not yet
  251. 10:25seen
  252. 10:26a bgt sandwich blt stands for bacon
  253. 10:29lettuce tomato
  254. 10:31all right let's move on and take a look
  255. 10:33at a little bit more
  256. 10:35complex control so you'll find different
  257. 10:38types of loops and see
  258. 10:39for example while do while and four
  259. 10:43they are mapped on exactly the same
  260. 10:47set of branching eq ranging
  261. 10:50branching instructions that we have seen
  262. 10:52so far no difference there
  263. 10:54let's take a look at a little bit more
  264. 10:55complex example here so here is a
  265. 10:58part here is a piece of c code
  266. 11:01that looks at an array of 20 integers
  267. 11:06and adds them all up so
  268. 11:09color coding here is used to easily
  269. 11:12identify what goes where which registers
  270. 11:15correspond to which variables so first
  271. 11:18we'll take the pointer
  272. 11:19to the first element in the array a0 and
  273. 11:22copy to register eight
  274. 11:24because we're going to be changing it
  275. 11:26and we do not want to destroy it that's
  276. 11:28another good practice that we would like
  277. 11:32to use in the
  278. 11:32in the assembly we don't want to destroy
  279. 11:35the content the starting content of that
  280. 11:37you know of that register because
  281. 11:40we may need it later then we need to
  282. 11:43initialize everything will initialize
  283. 11:44sum to zero
  284. 11:45index i to zero and
  285. 11:48we do need to to store the
  286. 11:52final value 20 in
  287. 11:55the register x13 because we'll need to
  288. 11:58compare
  289. 11:59um the i to that value
  290. 12:02and find out if we uh we have completed
  291. 12:05our summation
  292. 12:06um remember we don't have branching with
  293. 12:09immediates in risk five
  294. 12:11contents of both variables that are
  295. 12:13going to be
  296. 12:14compared need to be in the register so
  297. 12:16there is no
  298. 12:17comparison to uh to an immediate
  299. 12:21so let's start our loop here is the
  300. 12:23start of the loop and there is one
  301. 12:25really important thing that i want
  302. 12:26everybody to remember
  303. 12:28test the condition if you should exit
  304. 12:31right away
  305. 12:32because it's the same logic that we've
  306. 12:34been following following so far
  307. 12:37if the condition to exit the loop is
  308. 12:39true we just
  309. 12:40go to the end to done
  310. 12:43so in this case if x11
  311. 12:46if i has reached 20 we just
  312. 12:50go to done that's yes if not we stay
  313. 12:54inside the loop so when we are in the
  314. 12:57loop
  315. 12:58we load the next value of the next
  316. 13:01element of
  317. 13:03a into register x12 we add it
  318. 13:06to the running value of the sum which is
  319. 13:09in x10
  320. 13:10then we increment the pointer
  321. 13:14remember we are we would like to load
  322. 13:15the next integer integers are 4 bytes
  323. 13:18wide
  324. 13:19risk five addresses every byte in the
  325. 13:21memory so we need to increment this
  326. 13:23pointer by four this is a
  327. 13:25very common mistake always keep that in
  328. 13:28mind
  329. 13:28because you're going to be having a fun
  330. 13:31time
  331. 13:31debugging these if you are not
  332. 13:33incrementing if you're working with
  333. 13:35integers and not incrementing memory
  334. 13:37pointers very four
  335. 13:38we increment i by one and that's
  336. 13:42basically it
  337. 13:43we jump back to the beginning of the
  338. 13:45loop when we we are just missing one
  339. 13:48more
  340. 13:49label which is done to say that we have
  341. 13:52exited this loop
  342. 13:54and that's it that wraps up our
  343. 13:57discussion of branches see you in a bit

About this transcript

This page contains the full transcript of [CS61C FA20] Lecture 08.3 - RISC-V lw, sw, Decisions I: Decision Making by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 1,945 words across 343 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.