[CS61C FA20] Lecture 10.2 - RISC-V Procedures: Register Conventions — Transcript
Full transcript
- 0:00[Music]
- 0:10welcome back one more time to the risk 5
- 0:12assembly language
- 0:13this time we'll talk about nested
- 0:14procedures
- 0:16and register conventions that support
- 0:18them
- 0:20remember how we do a function call in
- 0:22risk five
- 0:24main program places arguments in
- 0:27known locations registers say not to a7
- 0:32and does a jumping link
- 0:35in that process saves the return
- 0:39address in x1 and then transfers
- 0:42control to the function by jumping to
- 0:45its location
- 0:47then the function will have to save
- 0:51those registers that have some value
- 0:55that the the main
- 0:58relies on well by putting them on a
- 1:00stack then
- 1:02the function will do its own thing we'll
- 1:04put the
- 1:05return value in a naught in a1
- 1:09and will return those registers
- 1:12restore the registers from the stack and
- 1:14then
- 1:15we'll do the return back to the main
- 1:18now what happens if
- 1:22there is another function say function2
- 1:25that this function that has been called
- 1:27by the main
- 1:29needs to call so there is basically a
- 1:31nested function
- 1:32the main calls a function 1 and function
- 1:351 needs to call
- 1:36function 2. what will happen here
- 1:40is that we have only one place where we
- 1:43can put the return address where we know
- 1:45how to where to put the return address
- 1:48which is the register x1 that's the one
- 1:50that holds the return address
- 1:52so it will be holding the return address
- 1:54from the main when we go to the function
- 1:56one
- 1:56when function one calls function two it
- 1:59will clogger
- 2:00that value and functional will not know
- 2:02where to go back
- 2:04similar holds for the argument registers
- 2:08so what
- 2:11what are we going to do well let's take
- 2:13a look at that if this was not clear
- 2:15let's look
- 2:16a little bit more into an example
- 2:19so here is a function sum square that
- 2:22works on
- 2:23integers x and y and it
- 2:26in turn calls another function mult
- 2:30of x and then adds a y to that
- 2:34so if something called some square you
- 2:37know some
- 2:38some program maybe it's another function
- 2:41something called sum square
- 2:42and now some square calls mult so there
- 2:44is a value in r a
- 2:46that some square wants to jump back to
- 2:48to be able to return but this will be
- 2:51overwritten by the by the that call to
- 2:54uh malt so what are we going to do well
- 2:58we have a standard solution right we
- 3:01shall use stack actually you know there
- 3:05is a straightforward way of doing things
- 3:08we take all the registers all 31 of them
- 3:11and we just put them on stack every time
- 3:14there is a
- 3:15nested call but that is inefficient
- 3:18we will rarely be using all 31 registers
- 3:21and
- 3:22memory operations are expensive they
- 3:24take time
- 3:26and sometimes we may get away without at
- 3:29all
- 3:30having to put put things on a stack we
- 3:33may be able to reuse local
- 3:34register for something
- 3:38so let's take a look
- 3:41at what we actually do
- 3:46we rely on putting
- 3:49some of the registers on the stack
- 3:52during a nested call
- 3:53and having some of them being
- 3:57clobbered in order to understand how
- 4:00this works we have we have to
- 4:04um always understand the relationship
- 4:06between the nested functions
- 4:09so the one that calls the function we'll
- 4:12call a
- 4:12caller the one the function that is
- 4:16being called will call
- 4:17call e so caller one once
- 4:20again caller is the calling function
- 4:24call le is the function that is being
- 4:26called
- 4:28so when a callee returns from executing
- 4:32the caller needs to know which registers
- 4:35have been clobbered and which ones
- 4:39are guaranteed to be safe to to store
- 4:41the value that was
- 4:42there before the function call so the
- 4:45register convention
- 4:47essentially splits the registers into
- 4:49these the ones that are saved
- 4:51and then the other ones that are
- 4:54volatile
- 4:55or called temporary
- 5:00so every time there is a jowl we need to
- 5:03split the registers into this
- 5:05into these two groups and that is done
- 5:07by convention in risk five
- 5:10so let's take a look at what is
- 5:13happening in these conventions and again
- 5:15this is happening in order to reduce the
- 5:18number of unnecessary
- 5:20um rights to the memory because
- 5:25i'll try it tries to optimize some up
- 5:27some average number of registers that
- 5:29need to go to the stack
- 5:31assuming that sometimes all of them will
- 5:32have to go to the stack or
- 5:34many of them will have to go to the
- 5:35stack sometimes none or
- 5:38almost none so there are two categories
- 5:41of registers those
- 5:44that are preserved across function calls
- 5:48and caller can rely on these values
- 5:51being unchanged so the function the
- 5:54caller function the calls
- 5:56the callee doesn't have to worry about
- 5:58those whatever is left in those
- 6:01registers stays
- 6:02that way so the things that
- 6:04automatically saved
- 6:06you guessed one of them is clearly
- 6:09the stack pointer stack pointer is is
- 6:13the one that
- 6:14is preserved across because of the
- 6:16mechanism how the stack works
- 6:20there are a few other pointers that
- 6:24we are not going to touch here there is
- 6:26a global pointer a thread pointer and a
- 6:28frame pointer
- 6:29those are generally touched by the
- 6:34by the compiler in the operating system
- 6:38and then there are save registers there
- 6:41is a dozen registers s naught to s11
- 6:46that are saved whose values
- 6:50which should not be that that are going
- 6:53to be untouched
- 6:54so they're not going to be scribbled
- 6:56over and if we need to use them
- 6:58like we did in the previous example of a
- 7:01function call
- 7:02well the callee will need
- 7:05to restore their value and then
- 7:09there is the second set of registers
- 7:11that are not preserved across function
- 7:13calls
- 7:14so the caller cannot rely on
- 7:17these staying the same we'll call those
- 7:20volatile or temporary registers
- 7:24so we already know that the return
- 7:26address
- 7:27and the argument return registers a
- 7:30naught to a7
- 7:31are not going to be preserved they will
- 7:34need to go
- 7:35to a stack across the
- 7:38nested calls and there are also
- 7:41so-called temporary registers
- 7:42t naught to t t six if there is
- 7:45something
- 7:45that the caller has that
- 7:48does not want to be overwritten in the
- 7:50temporary registers
- 7:52the caller will need to save them before
- 7:55calling a function got that
- 8:03so
- 8:06here is a summary of you know which
- 8:10registers are
- 8:14saved and which ones are temporary
- 8:17they have their numbers that hardware
- 8:23understands and then they have something
- 8:24that is called the application binary
- 8:26interface
- 8:27these are their symbolic names these are
- 8:29human-friendly symbolic names in
- 8:31assembly code that's how you can call
- 8:34them uh
- 8:34by in assembly and then here are there
- 8:38um descriptions
- 8:41um some of these are familiar and you
- 8:45have seen
- 8:46the other ones that you haven't seen you
- 8:47probably don't need to get into a deep
- 8:49you know deep into detail with them but
- 8:52the important thing is
- 8:53here who is the one that needs to save
- 8:56the contents of that register
- 8:58if there is a nested call
- 9:02so well nobody needs to
- 9:06save x0 return address
- 9:09needs to be saved by the caller
- 9:13stack pointer is saved by the callee
- 9:18taken care of we don't care about these
- 9:22and then the temporary registers if
- 9:25there is something in the temporary
- 9:26registers
- 9:27that the caller would like to have
- 9:30saved during the during the function
- 9:33call
- 9:34the caller needs to save them so the
- 9:36caller will need to put them
- 9:38in the locations where they are going to
- 9:40be preserved
- 9:42stack or
- 9:46or the the saved registers
- 9:50and then if after a function call
- 9:57the the callee wants to use or needs to
- 10:00use the save registers
- 10:03they need to save those values
- 10:06and restore them because the basic logic
- 10:10here
- 10:10is caller assumes that the values that
- 10:13are going to be
- 10:14in the saved registers are not going to
- 10:16be touched
- 10:18if they have something that matters to
- 10:20them in the temporary registers in
- 10:23like a scratch space they better take
- 10:25care of that
- 10:26and put it somewhere where they can
- 10:28restore it
- 10:29from after the function call
- 10:32and that is basically it we are going to
- 10:35take a look a little bit more of the
- 10:38the operational memory after this break
- 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.