YouTube2Text

[CS61C FA20] Lecture 10.1 - RISC-V Procedures: Function Call Example — Transcript

by CS 61C Departmental · 1,809 words · 321 segments · language en · Watch on YouTube

Full transcript

  1. 0:00[Music]
  2. 0:10welcome back to this five assembly
  3. 0:12language
  4. 0:12we are going to continue with our
  5. 0:15function call example
  6. 0:17remember we talked about six basic steps
  7. 0:20that we need to make when calling a
  8. 0:21function
  9. 0:24let's go over them one more time because
  10. 0:26we understand much better what
  11. 0:27actually needs to happen and we're going
  12. 0:30to reinforce that with an example
  13. 0:32all right so the first step in the first
  14. 0:34step the
  15. 0:35main program that calls the function
  16. 0:37needs to put the arguments in the place
  17. 0:38where the function
  18. 0:39will find them so we know that this is
  19. 0:42going to be this this is going to be a
  20. 0:44set of designated registers for that
  21. 0:46then it transfers control to the
  22. 0:48function
  23. 0:49by using a single instruction which is
  24. 0:52jump and link
  25. 0:54what jumping link does saves the return
  26. 0:57address
  27. 0:58and jumps to the location of the
  28. 1:00function
  29. 1:01to the to the address of a function so
  30. 1:04at that point
  31. 1:06the function is the one that has control
  32. 1:08of a processor
  33. 1:10the sequence of execution of
  34. 1:12instructions
  35. 1:13and its local registers
  36. 1:17so you can just function makes itself
  37. 1:19comfortable and at home and
  38. 1:21just throw a party all over the space in
  39. 1:24the
  40. 1:25in the register no right
  41. 1:28there may be variables that the main
  42. 1:32program cares about
  43. 1:35they're stored in registers we cannot
  44. 1:38just destroy them
  45. 1:39and leave a mess inside the processor
  46. 1:42so the main program cannot continue or
  47. 1:45it stopped
  48. 1:46where it passed so
  49. 1:50we need to save
  50. 1:53variables that are in there somewhere
  51. 1:57in order to be able to use that space of
  52. 2:00course there'll be a little bit
  53. 2:01some amount of scratch space somewhere
  54. 2:04that where we can scribble
  55. 2:06as much as we like but we cannot
  56. 2:09scribble over
  57. 2:10all the registers that are over there
  58. 2:11it's like if you really want to throw a
  59. 2:13party you need to put away all the
  60. 2:15planners and
  61. 2:17and bases and so on and then after
  62. 2:19you're done with the party you put them
  63. 2:20back on
  64. 2:23then so the the processor
  65. 2:26when doing that so the function will
  66. 2:28acquire
  67. 2:29the local storage resources that it
  68. 2:32needs to
  69. 2:32to execute what it needs to do then it
  70. 2:35will do
  71. 2:36its desired set of tasks and
  72. 2:40then needs to clean up so it will put
  73. 2:44the return value
  74. 2:45in the place where it has been agreed
  75. 2:47upon
  76. 2:48and you know the register has been
  77. 2:50agreed upon and
  78. 2:52then it will restore
  79. 2:55all the values of the variables that
  80. 2:57have been already in the processor
  81. 2:59before the function call
  82. 3:01and clean up everything else and then it
  83. 3:03will return control to the main
  84. 3:05processor
  85. 3:06by using a single instruction which is
  86. 3:08returned
  87. 3:09return uses
  88. 3:13an address stored in a register to
  89. 3:16return turn to the place
  90. 3:18where it was called from because there
  91. 3:20there can be many locations in the main
  92. 3:22program
  93. 3:22that would be calling the same function
  94. 3:26all right let's take a look at
  95. 3:30the function call example
  96. 3:33in this case we're looking at the c
  97. 3:34function leaf and leaf usually means a
  98. 3:37function
  99. 3:37that does not call any other function it
  100. 3:40is being called by
  101. 3:41other functions but does not call other
  102. 3:43functions in this case
  103. 3:45it has four arguments g h i and j
  104. 3:49and it returns one value which is f
  105. 3:53uh it is fairly simple
  106. 3:57f is equal to g plus h minus the sum
  107. 4:01of i and j so these parameters
  108. 4:05g h i j j h a i and j
  109. 4:09will be in the argument registers a
  110. 4:11naught to a three
  111. 4:13and f will be using a temporary
  112. 4:18space in s0 before it is placed back in
  113. 4:22a knot and perhaps we need one more
  114. 4:24temporary
  115. 4:26register s1 so the
  116. 4:29we are going to be writing over s0 and
  117. 4:32s1
  118. 4:34so where do the old values that were in
  119. 4:37s
  120. 4:370 and s1 or any other registers go
  121. 4:40where are they saved when we make a
  122. 4:44function call
  123. 4:45and how do we restore them
  124. 4:48so we don't when you may think we can
  125. 4:52put them in registers but we generally
  126. 4:54don't have enough registers for every
  127. 4:56function call
  128. 4:57so they'll need to go to a memory and
  129. 4:59there is an ideal
  130. 5:00place in memory ldl structure in
  131. 5:03memory organizational memory that is
  132. 5:05being used for that that's a stack
  133. 5:07at this point you should really make a
  134. 5:10connection between
  135. 5:11the meaning of a stack in c and how it
  136. 5:14is physically realized in assembly
  137. 5:18it's the last in first out structure
  138. 5:21what does that mean
  139. 5:22well the last data that goes on stock is
  140. 5:25the first one that is going to go out
  141. 5:27um if you like it's like a stack of
  142. 5:30plates
  143. 5:31so if you have a stack of plates the
  144. 5:34last plate
  145. 5:35that you're going to put on top will be
  146. 5:38the first one that will remove
  147. 5:40from the stack of plates unless
  148. 5:43you are my daughter from a few years ago
  149. 5:46that would like to somehow
  150. 5:47get the middle plate on the stack
  151. 5:53all right um there are two operations
  152. 5:56that we
  153. 5:57perform on the stack putting a plate on
  154. 6:00the stack mean piece of data this is
  155. 6:01data we put it on a stack that's called
  156. 6:03a push
  157. 6:05when we remove a plate from the stack
  158. 6:08it's a pop
  159. 6:11these things happen frequently and in
  160. 6:13some instruction languages there are
  161. 6:15designated
  162. 6:16instructions that perform portion pot
  163. 6:20but we don't have them in risk five
  164. 6:25the support that we have for the stack
  165. 6:28is
  166. 6:29in having a designated place where we
  167. 6:31keep
  168. 6:32the address of a stack that is called
  169. 6:34the stack pointer
  170. 6:36or sp in by convention in risk 5 it is
  171. 6:41stored in the register
  172. 6:42x2 so x0 is a 0
  173. 6:45x1 is our return address x2 is the stack
  174. 6:49pointer
  175. 6:52but you may you know we'll start it's
  176. 6:54supposed to have the
  177. 6:55start and then end so how can we know
  178. 6:58where is this type we're just having a
  179. 7:00stack pointer just one
  180. 7:02address one 32-bit address
  181. 7:05convention is to that the starks that
  182. 7:08the stack starts
  183. 7:09at the very top of the memory space and
  184. 7:13grows downwards so as we are increasing
  185. 7:16the stack
  186. 7:17we are decreasing the address of a stack
  187. 7:20pointer
  188. 7:21so when we push onto a stack
  189. 7:25we decrement the stack pointer when we
  190. 7:27pop from a stack we increment the stack
  191. 7:29pointer
  192. 7:30all right so what goes in the stack
  193. 7:34usually each function has a set of data
  194. 7:36that we'll need to put on on stack
  195. 7:39and we'll call that a stack frame start
  196. 7:42frame will
  197. 7:42include the return address that may have
  198. 7:45already been
  199. 7:46saved if we if there is another function
  200. 7:48that is perhaps
  201. 7:50calling this function that has called
  202. 7:53this function
  203. 7:54then all the arguments need to go there
  204. 7:57and the space
  205. 7:58for local variables that are going to be
  206. 8:00used
  207. 8:01so if there are nested calls which we
  208. 8:04are going to discuss in a little bit
  209. 8:05more detail
  210. 8:06in the next segment these nested calls
  211. 8:09are going to be placing a set of stack
  212. 8:12frames
  213. 8:13onto the stack so
  214. 8:16these tag frames are contiguous blocks
  215. 8:19[Music]
  216. 8:20of memory and the stack pointer tells
  217. 8:23where is the last one of them
  218. 8:25where where is the the bottom of the
  219. 8:28stack
  220. 8:29so when we add a new stack frame we move
  221. 8:32the stack pointer
  222. 8:34from one location to another
  223. 8:37when the procedure ends we move the
  224. 8:39stack pointer back
  225. 8:42let's use yellow
  226. 8:45we move the stack pointer
  227. 8:49here and we free up that space
  228. 8:53on the stack the data
  229. 8:56may still be on the stack but it is now
  230. 8:58available for the next function call
  231. 9:00call to write over it
  232. 9:02so that's the meaning of three basically
  233. 9:06now let's go back to our lead it needs
  234. 9:08to pass four arguments
  235. 9:09as we have seen um for our
  236. 9:13four uh variables and it needs
  237. 9:16to to to use register as 0 and s1
  238. 9:21so let's take a look at what is the risk
  239. 9:235 code
  240. 9:24for leaf
  241. 9:29[Music]
  242. 9:31first since there are two variables that
  243. 9:34need to be
  244. 9:36stored we need to make room on stack for
  245. 9:40storing them
  246. 9:44so how much room do we need to how many
  247. 9:47bytes do we need to store two integers
  248. 9:49we need eight bytes so we are going to
  249. 9:51decrement the stack pointer
  250. 9:53by eight we will write
  251. 9:58s1 and s2 on the stack remember
  252. 10:01the direction here we are going from s1
  253. 10:05to the address that is four bytes offset
  254. 10:08from the stack pointer
  255. 10:10and s0 is going to go
  256. 10:14for uh at the
  257. 10:17location of a stack pointer the previous
  258. 10:19frame is
  259. 10:20eight bytes away
  260. 10:24this sequence of instructions that
  261. 10:28prepares the function call that is the
  262. 10:30first thing that we do in the function
  263. 10:31call
  264. 10:32is called the prologue
  265. 10:40then we do our stuff we are going to add
  266. 10:42the values
  267. 10:43we'll add f and g place it in s1
  268. 10:47we'll add i and j place it in
  269. 10:50the first one goes in the zero the
  270. 10:52second one in s1 and finally we put the
  271. 10:54return value
  272. 10:55in a0 then we are ready to return
  273. 10:59first we are going to restart restore
  274. 11:02the two registers that we have saved
  275. 11:04we are taking them from their
  276. 11:05appropriate locations
  277. 11:07back to s0 and s1
  278. 11:12we are going to put the stack pointer we
  279. 11:15are going to free up the memory we are
  280. 11:16putting the stack pointer where it was
  281. 11:18before by incrementing it by eight
  282. 11:21and we are ready to return this is what
  283. 11:24wraps up a function call
  284. 11:27this is called the epilogue
  285. 11:33and finally we jump back to the return
  286. 11:37address this is also what we have called
  287. 11:42return
  288. 11:45and that's that is it now we have seen
  289. 11:48how does a
  290. 11:49function call look like let's just
  291. 11:52one more time revisit how does the stack
  292. 11:56look like
  293. 11:57before during and after the the
  294. 12:00function the stack pointer is pointing
  295. 12:03to a memory location
  296. 12:05before to to some memory location before
  297. 12:07the call
  298. 12:08when we make a call we made room and
  299. 12:12but for two more just for saving two
  300. 12:14registers by decrementing the stack
  301. 12:16pointer
  302. 12:18by eight then when we are done with a
  303. 12:22call
  304. 12:24we are incrementing the stack pointer
  305. 12:25and pointing to the old location
  306. 12:28that we have where it pointed before
  307. 12:31the data may stay in the memory
  308. 12:34but it is labeled as free the next
  309. 12:37function
  310. 12:38call is going to scribble over it
  311. 12:43you'll find out that
  312. 12:47there are some security issues with that
  313. 12:49some people have figured out that
  314. 12:52one functions garbage maybe another
  315. 12:55function's
  316. 12:56treasure and they may go and mine for
  317. 12:58some useful stuff in their
  318. 13:00malicious programs for example so
  319. 13:03that is it i'll see you in a bit with
  320. 13:06the
  321. 13:06nested function calls

About this transcript

This page contains the full transcript of [CS61C FA20] Lecture 10.1 - RISC-V Procedures: Function Call Example by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 1,809 words across 321 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.