YouTube2Text

[CS61C FA20] Lecture 10.3 - RISC-V Procedures: Memory Allocation — Transcript

by CS 61C Departmental · 1,589 words · 280 segments · language en · Watch on YouTube

Full transcript

  1. 0:00[Music]
  2. 0:09hi welcome back
  3. 0:10can you believe that we are almost done
  4. 0:12with the risk 5
  5. 0:14isa and the assembly language
  6. 0:18i'm getting tired too so let's get
  7. 0:22[Applause]
  8. 0:24going with wrapping it up the one last
  9. 0:26thing that we need to do is quite
  10. 0:27exciting
  11. 0:28it is going to tie the memory allocation
  12. 0:31that you have seen in c
  13. 0:33with what is happening with memory
  14. 0:35allocation in risk 5.
  15. 0:38so that will hopefully really
  16. 0:42paint a clear picture of what is
  17. 0:43happening with these mallocs
  18. 0:45and things like those and see
  19. 0:48so let's get to it there are two types
  20. 0:50of variables that we have seen in c
  21. 0:52there are automatic variables that are
  22. 0:56local to a function
  23. 0:57and appear when the function is called
  24. 1:00and then
  25. 1:01are discarded when the function exits
  26. 1:04and then there are
  27. 1:05statics variables that that exist all
  28. 1:08the time across
  29. 1:09entries and exits into the procedures
  30. 1:14so when we need these local variables
  31. 1:18that don't fit in the registers
  32. 1:20we will need to store them on the stack
  33. 1:25um so the this
  34. 1:29stack frame that we have mentioned
  35. 1:30before not only has
  36. 1:32the the return address and
  37. 1:36some of the registers that need to be
  38. 1:38saved
  39. 1:39but also contains all these variables
  40. 1:42that
  41. 1:43cannot fit into the registers so that's
  42. 1:45why it's called the procedure frame or
  43. 1:47procedure activation record
  44. 1:50it's a segment of a stack that has all
  45. 1:52the saved registers and local variables
  46. 1:55that are needed to run that procedure
  47. 1:59let's take a look at how does it look
  48. 2:01like we have seen this before
  49. 2:03before we call a function stack stack
  50. 2:06pointer
  51. 2:06is sitting at some address
  52. 2:11when we call a function we allocate the
  53. 2:14space
  54. 2:15on the stack to save the return address
  55. 2:19to save the arguments because they may
  56. 2:22get overrun
  57. 2:22by a nested call and then
  58. 2:26saved registers need to to be on the
  59. 2:28stack as well
  60. 2:29and in addition to that any of the
  61. 2:32the variables that don't fit in the
  62. 2:34registers will be there
  63. 2:36and that piece can be fairly large if
  64. 2:38you're working with matrices or
  65. 2:40some large chunks of data and then when
  66. 2:42you're done
  67. 2:43with the call we clean up by
  68. 2:46decrementing the stack
  69. 2:47by incrementing the stack back to the
  70. 2:49starting position
  71. 2:50here remember stack decrements
  72. 2:54when we add a frame to it
  73. 2:57and increments when we remove the frame
  74. 3:02don't mix this up
  75. 3:05so how do we actually do
  76. 3:09this um how do we implement what happens
  77. 3:13with the stack when we implement this
  78. 3:15nested function that we have seen before
  79. 3:17remember we have
  80. 3:19a function sum square that works on
  81. 3:22integers x and y
  82. 3:23and calls within
  83. 3:26itself another function that multiplies
  84. 3:29x with x
  85. 3:30and then adds y before it exits
  86. 3:34so that it's a nested function call
  87. 3:38let's take a look at it take a look at
  88. 3:40what do we need to put on the stack
  89. 3:42when we make this call what is going to
  90. 3:44be really helpful is to understand
  91. 3:46what is where and what is going to get
  92. 3:49over it and
  93. 3:50what is going to be clobbered
  94. 3:54um so what are the arguments
  95. 3:57of sum square x is going to be living
  96. 4:01in
  97. 4:06a0 and y will live
  98. 4:09in a1 now when we
  99. 4:13call malt will have also two arguments
  100. 4:18both times it will be x sitting in
  101. 4:22a naught and a one so we can leave one
  102. 4:26value of x
  103. 4:26in a zero but y
  104. 4:30the value of y that was in a1
  105. 4:33will get overwritten what else is going
  106. 4:36to get overwritten during a nested call
  107. 4:38the return address remember when
  108. 4:41some square got called by the main
  109. 4:47we put the return address where the sum
  110. 4:50square will be
  111. 4:52going back or where will it jump back
  112. 4:55where will it
  113. 4:56return but now when we have a nested
  114. 4:58call
  115. 5:02some square becomes a caller and malt is
  116. 5:04a callee
  117. 5:05we need to save the return address for
  118. 5:08malt
  119. 5:09where will mult go back and that's the
  120. 5:12same
  121. 5:12spot it's the register x1 so
  122. 5:16the old return address is going to get
  123. 5:17overwritten
  124. 5:19some square will have no idea where to
  125. 5:21go back when it's done
  126. 5:25so the two things that we need to say
  127. 5:27therefore are
  128. 5:28the return address and
  129. 5:31the y that lives in a1 so we need to
  130. 5:34make room for two registers on the stack
  131. 5:38therefore we decrement the stack by
  132. 5:40eight
  133. 5:41never make that mistake and then we're
  134. 5:44going to write
  135. 5:45the return address and a1 on the stack
  136. 5:51in order to set up the
  137. 5:54multicall in order to call mount we need
  138. 5:57to put its arguments in the right place
  139. 5:59um so we need to copy value
  140. 6:02of x from a naught
  141. 6:06into a1 and remember a move
  142. 6:09instruction is a pseudo instruction that
  143. 6:12essentially
  144. 6:13just copies the value like the
  145. 6:16the value from one register to another
  146. 6:19there is no move
  147. 6:20the the original value still stays in a
  148. 6:23not
  149. 6:24we just get a copy of it in a1 um
  150. 6:27it's an ad immediate with a zero so um
  151. 6:33you know why is it called it's a tough
  152. 6:36question that i think
  153. 6:37belongs to that originates from
  154. 6:40motorola's
  155. 6:41microprocessor since 70s why they called
  156. 6:43it move we don't know
  157. 6:45but everybody now calls it move although
  158. 6:48it's just a copy
  159. 6:50so we already we have x in
  160. 6:53in both a naught and a one register so
  161. 6:56we can call
  162. 6:57a malt with those arguments so we are
  163. 7:00going to call
  164. 7:01mult here and multi is going to
  165. 7:05return the result back in a naught
  166. 7:08now what do we need in order to complete
  167. 7:11our
  168. 7:12sum square instruction we need to get y
  169. 7:15back from the stack because
  170. 7:16y is gone to the stack we don't know um
  171. 7:23we saved it to a stack so that we can
  172. 7:25call it back
  173. 7:26so we're going to put it back in a1
  174. 7:29we are gonna finish our addition we'll
  175. 7:32add the result of malt
  176. 7:34uh to y or y to the result of malt
  177. 7:37and then we need to clean up we
  178. 7:40put back we recall from the stack
  179. 7:45the return address we
  180. 7:49clean up the stack pointer we increment
  181. 7:51the stack pointer by 8
  182. 7:52to put it to the previous value and then
  183. 7:55we are ready
  184. 7:56to jump back to the return address
  185. 8:03these three instructions at the top
  186. 8:06is what corresponds to to
  187. 8:10a push to the stack and the three
  188. 8:13instructions three red instructions on
  189. 8:16the bottom correspond to the pop
  190. 8:18in some sisk architectures you'll find
  191. 8:20out that one
  192. 8:21[Applause]
  193. 8:23push instructions pushes multiple
  194. 8:25registers to the stack
  195. 8:27and similarly pop instruction recalls
  196. 8:30from the stack
  197. 8:33what we have here is basically a
  198. 8:35procedure of saving these
  199. 8:38registers and the
  200. 8:41arguments of the function on the stack
  201. 8:44and recalling them back
  202. 8:45so they can be used to complete
  203. 8:49the our original function remember here
  204. 8:53some square is a color malt is a colby
  205. 9:01a few more things to tie this together
  206. 9:04with c
  207. 9:04when we're on c there are three
  208. 9:08basic memory regions that that get
  209. 9:11allocated
  210. 9:12we have static variables that are
  211. 9:14declared once per program
  212. 9:16um there is a heap that is
  213. 9:19dynamically allocated by a malloc and
  214. 9:22there is stack
  215. 9:23that is used during procedure calls
  216. 9:29where does this live in memory in risk 5
  217. 9:33this is set by the convention it is
  218. 9:36specific to rb32 it's a little bit
  219. 9:39different for
  220. 9:40rb64 and rv128
  221. 9:43one thing to keep in mind all these
  222. 9:45memory regions need to be
  223. 9:47aligned with a 16 byte boundary such
  224. 9:50that
  225. 9:51everything is compatible also with rv128
  226. 9:53but you don't have to worry too much
  227. 9:55about that and i was not paying
  228. 9:56attention to that
  229. 9:58so stack starts with the top of the
  230. 10:01memory
  231. 10:02which is bff ffff0 aligned with
  232. 10:0516 um in rv32
  233. 10:11rb 32 programs
  234. 10:14are on the bottom of the memory very
  235. 10:17close to the bottom of the number and
  236. 10:18not at the very bottom
  237. 10:19the very bottom is reserved for special
  238. 10:22stuff
  239. 10:23i'll mention that in a second programs
  240. 10:27go from the address one
  241. 10:30and four zeros and then static
  242. 10:34data segment is right above the
  243. 10:37uh about the text what we call about the
  244. 10:40program
  245. 10:41um and
  246. 10:44this global pointer points to the static
  247. 10:47so
  248. 10:48it is one zero zero zero
  249. 10:51zero zero zero hex and then finally
  250. 10:54there is heap
  251. 10:55that is above static um
  252. 10:58and that's where we place our data
  253. 11:00structure that we malloc
  254. 11:03so heap will go up towards the stack
  255. 11:07stack will go downwards if they meet we
  256. 11:09are out of memory
  257. 11:11for now let's see this pictorially
  258. 11:14um on the top we have the stack
  259. 11:18going downwards and dynamic data on the
  260. 11:21heap goes up
  261. 11:23static data is allocated in the middle
  262. 11:26and hex is from above from the
  263. 11:29from the address 10 000 hex
  264. 11:32what is on the bottom well that's where
  265. 11:37some of these system uh calls are
  266. 11:40that's where you would write to write to
  267. 11:43a printer or a display and things like
  268. 11:45those io devices
  269. 11:47live down there things that we'll see
  270. 11:49later interrupts
  271. 11:50and so on generally stuff that you
  272. 11:52should not be scribbling over
  273. 11:54otherwise some strange things may happen
  274. 11:57and that's it i've just wrapped up the
  275. 12:00entire
  276. 12:02isa that we need to know for risk five
  277. 12:06we have learned most of the instructions
  278. 12:09and we're going to
  279. 12:10go and summarize that in just a bit see
  280. 12:13you there

About this transcript

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