YouTube2Text

[CS61C FA20] Lecture 10.2 - RISC-V Procedures: Register Conventions — Transcript

by CS 61C Departmental · 1,383 words · 248 segments · language en · Watch on YouTube

Full transcript

  1. 0:00[Music]
  2. 0:10welcome back one more time to the risk 5
  3. 0:12assembly language
  4. 0:13this time we'll talk about nested
  5. 0:14procedures
  6. 0:16and register conventions that support
  7. 0:18them
  8. 0:20remember how we do a function call in
  9. 0:22risk five
  10. 0:24main program places arguments in
  11. 0:27known locations registers say not to a7
  12. 0:32and does a jumping link
  13. 0:35in that process saves the return
  14. 0:39address in x1 and then transfers
  15. 0:42control to the function by jumping to
  16. 0:45its location
  17. 0:47then the function will have to save
  18. 0:51those registers that have some value
  19. 0:55that the the main
  20. 0:58relies on well by putting them on a
  21. 1:00stack then
  22. 1:02the function will do its own thing we'll
  23. 1:04put the
  24. 1:05return value in a naught in a1
  25. 1:09and will return those registers
  26. 1:12restore the registers from the stack and
  27. 1:14then
  28. 1:15we'll do the return back to the main
  29. 1:18now what happens if
  30. 1:22there is another function say function2
  31. 1:25that this function that has been called
  32. 1:27by the main
  33. 1:29needs to call so there is basically a
  34. 1:31nested function
  35. 1:32the main calls a function 1 and function
  36. 1:351 needs to call
  37. 1:36function 2. what will happen here
  38. 1:40is that we have only one place where we
  39. 1:43can put the return address where we know
  40. 1:45how to where to put the return address
  41. 1:48which is the register x1 that's the one
  42. 1:50that holds the return address
  43. 1:52so it will be holding the return address
  44. 1:54from the main when we go to the function
  45. 1:56one
  46. 1:56when function one calls function two it
  47. 1:59will clogger
  48. 2:00that value and functional will not know
  49. 2:02where to go back
  50. 2:04similar holds for the argument registers
  51. 2:08so what
  52. 2:11what are we going to do well let's take
  53. 2:13a look at that if this was not clear
  54. 2:15let's look
  55. 2:16a little bit more into an example
  56. 2:19so here is a function sum square that
  57. 2:22works on
  58. 2:23integers x and y and it
  59. 2:26in turn calls another function mult
  60. 2:30of x and then adds a y to that
  61. 2:34so if something called some square you
  62. 2:37know some
  63. 2:38some program maybe it's another function
  64. 2:41something called sum square
  65. 2:42and now some square calls mult so there
  66. 2:44is a value in r a
  67. 2:46that some square wants to jump back to
  68. 2:48to be able to return but this will be
  69. 2:51overwritten by the by the that call to
  70. 2:54uh malt so what are we going to do well
  71. 2:58we have a standard solution right we
  72. 3:01shall use stack actually you know there
  73. 3:05is a straightforward way of doing things
  74. 3:08we take all the registers all 31 of them
  75. 3:11and we just put them on stack every time
  76. 3:14there is a
  77. 3:15nested call but that is inefficient
  78. 3:18we will rarely be using all 31 registers
  79. 3:21and
  80. 3:22memory operations are expensive they
  81. 3:24take time
  82. 3:26and sometimes we may get away without at
  83. 3:29all
  84. 3:30having to put put things on a stack we
  85. 3:33may be able to reuse local
  86. 3:34register for something
  87. 3:38so let's take a look
  88. 3:41at what we actually do
  89. 3:46we rely on putting
  90. 3:49some of the registers on the stack
  91. 3:52during a nested call
  92. 3:53and having some of them being
  93. 3:57clobbered in order to understand how
  94. 4:00this works we have we have to
  95. 4:04um always understand the relationship
  96. 4:06between the nested functions
  97. 4:09so the one that calls the function we'll
  98. 4:12call a
  99. 4:12caller the one the function that is
  100. 4:16being called will call
  101. 4:17call e so caller one once
  102. 4:20again caller is the calling function
  103. 4:24call le is the function that is being
  104. 4:26called
  105. 4:28so when a callee returns from executing
  106. 4:32the caller needs to know which registers
  107. 4:35have been clobbered and which ones
  108. 4:39are guaranteed to be safe to to store
  109. 4:41the value that was
  110. 4:42there before the function call so the
  111. 4:45register convention
  112. 4:47essentially splits the registers into
  113. 4:49these the ones that are saved
  114. 4:51and then the other ones that are
  115. 4:54volatile
  116. 4:55or called temporary
  117. 5:00so every time there is a jowl we need to
  118. 5:03split the registers into this
  119. 5:05into these two groups and that is done
  120. 5:07by convention in risk five
  121. 5:10so let's take a look at what is
  122. 5:13happening in these conventions and again
  123. 5:15this is happening in order to reduce the
  124. 5:18number of unnecessary
  125. 5:20um rights to the memory because
  126. 5:25i'll try it tries to optimize some up
  127. 5:27some average number of registers that
  128. 5:29need to go to the stack
  129. 5:31assuming that sometimes all of them will
  130. 5:32have to go to the stack or
  131. 5:34many of them will have to go to the
  132. 5:35stack sometimes none or
  133. 5:38almost none so there are two categories
  134. 5:41of registers those
  135. 5:44that are preserved across function calls
  136. 5:48and caller can rely on these values
  137. 5:51being unchanged so the function the
  138. 5:54caller function the calls
  139. 5:56the callee doesn't have to worry about
  140. 5:58those whatever is left in those
  141. 6:01registers stays
  142. 6:02that way so the things that
  143. 6:04automatically saved
  144. 6:06you guessed one of them is clearly
  145. 6:09the stack pointer stack pointer is is
  146. 6:13the one that
  147. 6:14is preserved across because of the
  148. 6:16mechanism how the stack works
  149. 6:20there are a few other pointers that
  150. 6:24we are not going to touch here there is
  151. 6:26a global pointer a thread pointer and a
  152. 6:28frame pointer
  153. 6:29those are generally touched by the
  154. 6:34by the compiler in the operating system
  155. 6:38and then there are save registers there
  156. 6:41is a dozen registers s naught to s11
  157. 6:46that are saved whose values
  158. 6:50which should not be that that are going
  159. 6:53to be untouched
  160. 6:54so they're not going to be scribbled
  161. 6:56over and if we need to use them
  162. 6:58like we did in the previous example of a
  163. 7:01function call
  164. 7:02well the callee will need
  165. 7:05to restore their value and then
  166. 7:09there is the second set of registers
  167. 7:11that are not preserved across function
  168. 7:13calls
  169. 7:14so the caller cannot rely on
  170. 7:17these staying the same we'll call those
  171. 7:20volatile or temporary registers
  172. 7:24so we already know that the return
  173. 7:26address
  174. 7:27and the argument return registers a
  175. 7:30naught to a7
  176. 7:31are not going to be preserved they will
  177. 7:34need to go
  178. 7:35to a stack across the
  179. 7:38nested calls and there are also
  180. 7:41so-called temporary registers
  181. 7:42t naught to t t six if there is
  182. 7:45something
  183. 7:45that the caller has that
  184. 7:48does not want to be overwritten in the
  185. 7:50temporary registers
  186. 7:52the caller will need to save them before
  187. 7:55calling a function got that
  188. 8:03so
  189. 8:06here is a summary of you know which
  190. 8:10registers are
  191. 8:14saved and which ones are temporary
  192. 8:17they have their numbers that hardware
  193. 8:23understands and then they have something
  194. 8:24that is called the application binary
  195. 8:26interface
  196. 8:27these are their symbolic names these are
  197. 8:29human-friendly symbolic names in
  198. 8:31assembly code that's how you can call
  199. 8:34them uh
  200. 8:34by in assembly and then here are there
  201. 8:38um descriptions
  202. 8:41um some of these are familiar and you
  203. 8:45have seen
  204. 8:46the other ones that you haven't seen you
  205. 8:47probably don't need to get into a deep
  206. 8:49you know deep into detail with them but
  207. 8:52the important thing is
  208. 8:53here who is the one that needs to save
  209. 8:56the contents of that register
  210. 8:58if there is a nested call
  211. 9:02so well nobody needs to
  212. 9:06save x0 return address
  213. 9:09needs to be saved by the caller
  214. 9:13stack pointer is saved by the callee
  215. 9:18taken care of we don't care about these
  216. 9:22and then the temporary registers if
  217. 9:25there is something in the temporary
  218. 9:26registers
  219. 9:27that the caller would like to have
  220. 9:30saved during the during the function
  221. 9:33call
  222. 9:34the caller needs to save them so the
  223. 9:36caller will need to put them
  224. 9:38in the locations where they are going to
  225. 9:40be preserved
  226. 9:42stack or
  227. 9:46or the the saved registers
  228. 9:50and then if after a function call
  229. 9:57the the callee wants to use or needs to
  230. 10:00use the save registers
  231. 10:03they need to save those values
  232. 10:06and restore them because the basic logic
  233. 10:10here
  234. 10:10is caller assumes that the values that
  235. 10:13are going to be
  236. 10:14in the saved registers are not going to
  237. 10:16be touched
  238. 10:18if they have something that matters to
  239. 10:20them in the temporary registers in
  240. 10:23like a scratch space they better take
  241. 10:25care of that
  242. 10:26and put it somewhere where they can
  243. 10:28restore it
  244. 10:29from after the function call
  245. 10:32and that is basically it we are going to
  246. 10:35take a look a little bit more of the
  247. 10:38the operational memory after this break
  248. 10:50you

About this transcript

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