YouTube2Text

[CS61C FA20] Lecture 09.3 - RISC-V Decisions II: RISC-V Function Calls — Transcript

by CS 61C Departmental · 1,480 words · 276 segments · language en · Watch on YouTube

Full transcript

  1. 0:00[Music]
  2. 0:08hi welcome back to risk5 assembly one
  3. 0:11more time
  4. 0:12and this time we are going to talk about
  5. 0:15this five function calls we're going to
  6. 0:18introduce two new instructions
  7. 0:20that are going to be supporting that by
  8. 0:23the way that's
  9. 0:24that'll be about it for the new
  10. 0:26instructions in
  11. 0:27risk five all the other ones that we
  12. 0:30need to complete the base instruction
  13. 0:31set are
  14. 0:32just the variance of what we have seen
  15. 0:34before i will round up
  16. 0:36that all of that up in a bit so
  17. 0:39functions in c are very common
  18. 0:43in this case we are looking at an
  19. 0:45instruction that we may call for
  20. 0:47multiplication it's not the very best
  21. 0:49uh multiplication implementation or
  22. 0:51multiplication but
  23. 0:53it does the multiplication by adding the
  24. 0:56multiplicand
  25. 0:57to the product
  26. 1:01multiplier times
  27. 1:04remember c calls functions by value
  28. 1:08so what it does it needs to pass the
  29. 1:10value
  30. 1:11to the function then so when we
  31. 1:14run into when we execute an instruction
  32. 1:21we'll get to the to the instruction call
  33. 1:24to the function call
  34. 1:26and that will make us jump
  35. 1:29to that function then we execute the
  36. 1:33function
  37. 1:34and when we hit the return we go back
  38. 1:37to the next instruction in the main if
  39. 1:39that
  40. 1:40next instruction is again a function
  41. 1:42call
  42. 1:44we're gonna go and jump again to the
  43. 1:48function
  44. 1:49execute that call again and return
  45. 1:53to the next instruction in the code
  46. 1:56and keep doing that as many as function
  47. 1:59calls are there
  48. 2:01so what kind of risk 5 assembly code
  49. 2:03should be used for this well i already
  50. 2:05said it
  51. 2:06there should be different kinds of jumps
  52. 2:10so let's get to that there are six
  53. 2:13fundamental steps
  54. 2:14in calling a function the first step put
  55. 2:17arguments in place where function can
  56. 2:19access them
  57. 2:21two transfer control to function three
  58. 2:24acquire local storage resources needed
  59. 2:27for function
  60. 2:29four perform the function or the task
  61. 2:33associated with it
  62. 2:35then put the return value in place where
  63. 2:38calling code
  64. 2:40can find it and restore any registers
  65. 2:43to their previous state release local
  66. 2:46storage
  67. 2:47six return control to the point of
  68. 2:50origin
  69. 2:51since a function can be called from
  70. 2:54several points in a program
  71. 2:55and program is back in control
  72. 2:59i'll give you an example that is not
  73. 3:00exactly the same but
  74. 3:02resembles this well so you're away
  75. 3:06you're away to college in normal
  76. 3:08circumstances
  77. 3:09and you go back to visit your parents
  78. 3:12assuming that your parents maybe are not
  79. 3:14that
  80. 3:15far away you're in berkeley and they're
  81. 3:17in silicon valley so
  82. 3:19they're not that far away you go back
  83. 3:21and you ring the doorbell
  84. 3:23and they are not there so what do you do
  85. 3:25you go straight for the planter where
  86. 3:27you know the spare
  87. 3:27key is or perhaps the fake croc from
  88. 3:30home depot
  89. 3:31where the spare key is you take the
  90. 3:34spare key
  91. 3:35and enter the house
  92. 3:39there you're in control you
  93. 3:45take you acquire the local storage
  94. 3:47resources perhaps the refrigerator and
  95. 3:49perform the function after that
  96. 3:51you rate the refrigerator but after
  97. 3:54you're done with the refrigerator raid
  98. 3:56you clean up after yourself because
  99. 3:59things would not be good if you don't do
  100. 4:01that
  101. 4:02and then you restore everything to the
  102. 4:05original state
  103. 4:06minus the food in the refrigerator put
  104. 4:09the key back in the planter
  105. 4:11such that when your
  106. 4:14sibling comes back from school can
  107. 4:17perform
  108. 4:17exactly the same function operating the
  109. 4:19refrigerator
  110. 4:21and cleaning up after themselves before
  111. 4:23they go perhaps to a soccer practice
  112. 4:29anyway it's not exactly the same but
  113. 4:31it's quite
  114. 4:32similar to what functions need to do
  115. 4:34including
  116. 4:35the cleanup procedure all right
  117. 4:39let's take a look at what are the risk
  118. 4:41five calling conventions
  119. 4:43they're fairly straightforward first
  120. 4:47we would like to use registers because
  121. 4:49they're faster than memory we don't want
  122. 4:51to always have to go
  123. 4:53to the memory to pass the values so we
  124. 4:55are going to be using registers
  125. 4:58extend to x17 as eight argument
  126. 5:01registers they'll be labeled a
  127. 5:03naught to a7 and the two
  128. 5:06of them are going to be holding the
  129. 5:08return values a naught
  130. 5:10and a1
  131. 5:13return address is going to be stored in
  132. 5:15register x1 and we are going to call
  133. 5:17that
  134. 5:18ra also registers
  135. 5:22s 0 s 1 and s 2 to s 11 are going to
  136. 5:26have a special meaning of so-called
  137. 5:28saved registers
  138. 5:30let's take a look at how does
  139. 5:33how does a function look like in
  140. 5:36assembly
  141. 5:37so we have a simple function up here
  142. 5:40in c it is a sum of two numbers and then
  143. 5:44when we
  144. 5:45call that function um the function will
  145. 5:49take the arguments the values of the
  146. 5:50arguments x and y
  147. 5:52and return the sum
  148. 5:55like everything else in risk 5
  149. 6:00instructions are 4 bytes 32 bits and
  150. 6:03each one of them has its own address
  151. 6:05addresses are shown here in decimal
  152. 6:06values one thousand thousand
  153. 6:08four eight twelve sixteen
  154. 6:11that's where the main program will be
  155. 6:13stored and
  156. 6:14the function may be at a different
  157. 6:17address
  158. 6:18two thousand two thousand 2004
  159. 6:21so what happens here when the main
  160. 6:24is calling the function what it needs to
  161. 6:28do is to prepare
  162. 6:29the things so the function can
  163. 6:33find them so first what
  164. 6:36we see here the values a and b are going
  165. 6:39to be stored in s naught in s1
  166. 6:42so we are going to move s naught in a
  167. 6:44naught
  168. 6:45and s1 in 2 to a1
  169. 6:48those are the ones that are going to be
  170. 6:51passed to the function
  171. 6:53and then we need to prepare for the
  172. 6:55return we are going to take the next
  173. 6:57instruction here 10 16
  174. 7:0210 16 and put it
  175. 7:06a as a return address in the array
  176. 7:09register
  177. 7:11then we are ready to jump to the
  178. 7:14function
  179. 7:15so what we do we simply jump to the sum
  180. 7:19sum does its stuff adds a not
  181. 7:23in a1 saves the result in a
  182. 7:27return argument a naught and it's done
  183. 7:30then it's ready
  184. 7:31to jump back to the main we use a new
  185. 7:34instruction here
  186. 7:36jrra to return
  187. 7:39to the value to the address stored in
  188. 7:42the array
  189. 7:43which is 10 16. important thing here
  190. 7:47we don't use a jump for returning
  191. 7:50because
  192. 7:50jump requires us to code up the address
  193. 7:54value
  194. 7:55function is going to be called from many
  195. 7:57places
  196. 7:58in the code so we better use a variable
  197. 8:02for that
  198. 8:02that's why we use jr
  199. 8:06all right
  200. 8:09what is instruction support for
  201. 8:11functions we actually
  202. 8:13have a new instruction here a new
  203. 8:16instruction very useful
  204. 8:18is called jump and link
  205. 8:21so instead of using two instructions
  206. 8:25to jump to the function which is saving
  207. 8:29a value of the next instruction in our a
  208. 8:32and then jumping to the sum we can just
  209. 8:35write
  210. 8:36jowl sum and that automatically saves
  211. 8:38the return address the next
  212. 8:40address in the code in the return as a
  213. 8:43return address
  214. 8:44in next one so that is very very
  215. 8:48useful very convenient because
  216. 8:52first we don't have to think about this
  217. 8:54next address
  218. 8:55and we are supporting the common case so
  219. 8:58since this is
  220. 8:59something function calls are really
  221. 9:01really frequently in
  222. 9:03in assembly we are going to support a
  223. 9:06common case
  224. 9:07and execute it fast with a more compact
  225. 9:11code
  226. 9:14jump in link should probably have been
  227. 9:17called
  228. 9:18link and jump because we first link and
  229. 9:20then jump
  230. 9:22link is basically forming an address or
  231. 9:24a link
  232. 9:25that points to the calling site to allow
  233. 9:27allow function to return
  234. 9:28to the proper address to the next
  235. 9:30address so we can just
  236. 9:33use it as jowl function label of course
  237. 9:36it's called a jowl because it's going to
  238. 9:37be
  239. 9:38hanging out in the green sheet with all
  240. 9:40the other jumps
  241. 9:41not with loads
  242. 9:45and then the other one that we have seen
  243. 9:46is jr there is actually a
  244. 9:50a pseudo instruction that we use
  245. 9:54as return return
  246. 9:57is jr to return address so at the end of
  247. 10:01a function call we'll generally just
  248. 10:03find
  249. 10:03a return or rat
  250. 10:07that's basically it for function calls
  251. 10:10we
  252. 10:11have learned how to call a function from
  253. 10:14c
  254. 10:14and how does that translate to
  255. 10:17risk 5 assembly just a few notes
  256. 10:21there are actually not that many
  257. 10:23instructions
  258. 10:25several instructions that i mentioned
  259. 10:27are pseudo instructions
  260. 10:29because they're special cases of gel or
  261. 10:32yet another instruction here that you're
  262. 10:34going to see
  263. 10:35gel r you can look at this
  264. 10:39we don't really need a special
  265. 10:40instruction for a jump
  266. 10:42if we don't save the return address we
  267. 10:44put it in x0
  268. 10:46that's our jump same thing with gel r
  269. 10:51we haven't learned that we're going to
  270. 10:52see it a little bit later but
  271. 10:54jr is essentially a special case
  272. 10:57of java
  273. 11:01that's it for function calls we are
  274. 11:03going to see a few examples after this
  275. 11:06so see you in just a bit
  276. 11:29you

About this transcript

This page contains the full transcript of [CS61C FA20] Lecture 09.3 - RISC-V Decisions II: RISC-V Function Calls by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 1,480 words across 276 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.