[CS61C FA20] Lecture 10.1 - RISC-V Procedures: Function Call Example — Transcript
Full transcript
- 0:00[Music]
- 0:10welcome back to this five assembly
- 0:12language
- 0:12we are going to continue with our
- 0:15function call example
- 0:17remember we talked about six basic steps
- 0:20that we need to make when calling a
- 0:21function
- 0:24let's go over them one more time because
- 0:26we understand much better what
- 0:27actually needs to happen and we're going
- 0:30to reinforce that with an example
- 0:32all right so the first step in the first
- 0:34step the
- 0:35main program that calls the function
- 0:37needs to put the arguments in the place
- 0:38where the function
- 0:39will find them so we know that this is
- 0:42going to be this this is going to be a
- 0:44set of designated registers for that
- 0:46then it transfers control to the
- 0:48function
- 0:49by using a single instruction which is
- 0:52jump and link
- 0:54what jumping link does saves the return
- 0:57address
- 0:58and jumps to the location of the
- 1:00function
- 1:01to the to the address of a function so
- 1:04at that point
- 1:06the function is the one that has control
- 1:08of a processor
- 1:10the sequence of execution of
- 1:12instructions
- 1:13and its local registers
- 1:17so you can just function makes itself
- 1:19comfortable and at home and
- 1:21just throw a party all over the space in
- 1:24the
- 1:25in the register no right
- 1:28there may be variables that the main
- 1:32program cares about
- 1:35they're stored in registers we cannot
- 1:38just destroy them
- 1:39and leave a mess inside the processor
- 1:42so the main program cannot continue or
- 1:45it stopped
- 1:46where it passed so
- 1:50we need to save
- 1:53variables that are in there somewhere
- 1:57in order to be able to use that space of
- 2:00course there'll be a little bit
- 2:01some amount of scratch space somewhere
- 2:04that where we can scribble
- 2:06as much as we like but we cannot
- 2:09scribble over
- 2:10all the registers that are over there
- 2:11it's like if you really want to throw a
- 2:13party you need to put away all the
- 2:15planners and
- 2:17and bases and so on and then after
- 2:19you're done with the party you put them
- 2:20back on
- 2:23then so the the processor
- 2:26when doing that so the function will
- 2:28acquire
- 2:29the local storage resources that it
- 2:32needs to
- 2:32to execute what it needs to do then it
- 2:35will do
- 2:36its desired set of tasks and
- 2:40then needs to clean up so it will put
- 2:44the return value
- 2:45in the place where it has been agreed
- 2:47upon
- 2:48and you know the register has been
- 2:50agreed upon and
- 2:52then it will restore
- 2:55all the values of the variables that
- 2:57have been already in the processor
- 2:59before the function call
- 3:01and clean up everything else and then it
- 3:03will return control to the main
- 3:05processor
- 3:06by using a single instruction which is
- 3:08returned
- 3:09return uses
- 3:13an address stored in a register to
- 3:16return turn to the place
- 3:18where it was called from because there
- 3:20there can be many locations in the main
- 3:22program
- 3:22that would be calling the same function
- 3:26all right let's take a look at
- 3:30the function call example
- 3:33in this case we're looking at the c
- 3:34function leaf and leaf usually means a
- 3:37function
- 3:37that does not call any other function it
- 3:40is being called by
- 3:41other functions but does not call other
- 3:43functions in this case
- 3:45it has four arguments g h i and j
- 3:49and it returns one value which is f
- 3:53uh it is fairly simple
- 3:57f is equal to g plus h minus the sum
- 4:01of i and j so these parameters
- 4:05g h i j j h a i and j
- 4:09will be in the argument registers a
- 4:11naught to a three
- 4:13and f will be using a temporary
- 4:18space in s0 before it is placed back in
- 4:22a knot and perhaps we need one more
- 4:24temporary
- 4:26register s1 so the
- 4:29we are going to be writing over s0 and
- 4:32s1
- 4:34so where do the old values that were in
- 4:37s
- 4:370 and s1 or any other registers go
- 4:40where are they saved when we make a
- 4:44function call
- 4:45and how do we restore them
- 4:48so we don't when you may think we can
- 4:52put them in registers but we generally
- 4:54don't have enough registers for every
- 4:56function call
- 4:57so they'll need to go to a memory and
- 4:59there is an ideal
- 5:00place in memory ldl structure in
- 5:03memory organizational memory that is
- 5:05being used for that that's a stack
- 5:07at this point you should really make a
- 5:10connection between
- 5:11the meaning of a stack in c and how it
- 5:14is physically realized in assembly
- 5:18it's the last in first out structure
- 5:21what does that mean
- 5:22well the last data that goes on stock is
- 5:25the first one that is going to go out
- 5:27um if you like it's like a stack of
- 5:30plates
- 5:31so if you have a stack of plates the
- 5:34last plate
- 5:35that you're going to put on top will be
- 5:38the first one that will remove
- 5:40from the stack of plates unless
- 5:43you are my daughter from a few years ago
- 5:46that would like to somehow
- 5:47get the middle plate on the stack
- 5:53all right um there are two operations
- 5:56that we
- 5:57perform on the stack putting a plate on
- 6:00the stack mean piece of data this is
- 6:01data we put it on a stack that's called
- 6:03a push
- 6:05when we remove a plate from the stack
- 6:08it's a pop
- 6:11these things happen frequently and in
- 6:13some instruction languages there are
- 6:15designated
- 6:16instructions that perform portion pot
- 6:20but we don't have them in risk five
- 6:25the support that we have for the stack
- 6:28is
- 6:29in having a designated place where we
- 6:31keep
- 6:32the address of a stack that is called
- 6:34the stack pointer
- 6:36or sp in by convention in risk 5 it is
- 6:41stored in the register
- 6:42x2 so x0 is a 0
- 6:45x1 is our return address x2 is the stack
- 6:49pointer
- 6:52but you may you know we'll start it's
- 6:54supposed to have the
- 6:55start and then end so how can we know
- 6:58where is this type we're just having a
- 7:00stack pointer just one
- 7:02address one 32-bit address
- 7:05convention is to that the starks that
- 7:08the stack starts
- 7:09at the very top of the memory space and
- 7:13grows downwards so as we are increasing
- 7:16the stack
- 7:17we are decreasing the address of a stack
- 7:20pointer
- 7:21so when we push onto a stack
- 7:25we decrement the stack pointer when we
- 7:27pop from a stack we increment the stack
- 7:29pointer
- 7:30all right so what goes in the stack
- 7:34usually each function has a set of data
- 7:36that we'll need to put on on stack
- 7:39and we'll call that a stack frame start
- 7:42frame will
- 7:42include the return address that may have
- 7:45already been
- 7:46saved if we if there is another function
- 7:48that is perhaps
- 7:50calling this function that has called
- 7:53this function
- 7:54then all the arguments need to go there
- 7:57and the space
- 7:58for local variables that are going to be
- 8:00used
- 8:01so if there are nested calls which we
- 8:04are going to discuss in a little bit
- 8:05more detail
- 8:06in the next segment these nested calls
- 8:09are going to be placing a set of stack
- 8:12frames
- 8:13onto the stack so
- 8:16these tag frames are contiguous blocks
- 8:19[Music]
- 8:20of memory and the stack pointer tells
- 8:23where is the last one of them
- 8:25where where is the the bottom of the
- 8:28stack
- 8:29so when we add a new stack frame we move
- 8:32the stack pointer
- 8:34from one location to another
- 8:37when the procedure ends we move the
- 8:39stack pointer back
- 8:42let's use yellow
- 8:45we move the stack pointer
- 8:49here and we free up that space
- 8:53on the stack the data
- 8:56may still be on the stack but it is now
- 8:58available for the next function call
- 9:00call to write over it
- 9:02so that's the meaning of three basically
- 9:06now let's go back to our lead it needs
- 9:08to pass four arguments
- 9:09as we have seen um for our
- 9:13four uh variables and it needs
- 9:16to to to use register as 0 and s1
- 9:21so let's take a look at what is the risk
- 9:235 code
- 9:24for leaf
- 9:29[Music]
- 9:31first since there are two variables that
- 9:34need to be
- 9:36stored we need to make room on stack for
- 9:40storing them
- 9:44so how much room do we need to how many
- 9:47bytes do we need to store two integers
- 9:49we need eight bytes so we are going to
- 9:51decrement the stack pointer
- 9:53by eight we will write
- 9:58s1 and s2 on the stack remember
- 10:01the direction here we are going from s1
- 10:05to the address that is four bytes offset
- 10:08from the stack pointer
- 10:10and s0 is going to go
- 10:14for uh at the
- 10:17location of a stack pointer the previous
- 10:19frame is
- 10:20eight bytes away
- 10:24this sequence of instructions that
- 10:28prepares the function call that is the
- 10:30first thing that we do in the function
- 10:31call
- 10:32is called the prologue
- 10:40then we do our stuff we are going to add
- 10:42the values
- 10:43we'll add f and g place it in s1
- 10:47we'll add i and j place it in
- 10:50the first one goes in the zero the
- 10:52second one in s1 and finally we put the
- 10:54return value
- 10:55in a0 then we are ready to return
- 10:59first we are going to restart restore
- 11:02the two registers that we have saved
- 11:04we are taking them from their
- 11:05appropriate locations
- 11:07back to s0 and s1
- 11:12we are going to put the stack pointer we
- 11:15are going to free up the memory we are
- 11:16putting the stack pointer where it was
- 11:18before by incrementing it by eight
- 11:21and we are ready to return this is what
- 11:24wraps up a function call
- 11:27this is called the epilogue
- 11:33and finally we jump back to the return
- 11:37address this is also what we have called
- 11:42return
- 11:45and that's that is it now we have seen
- 11:48how does a
- 11:49function call look like let's just
- 11:52one more time revisit how does the stack
- 11:56look like
- 11:57before during and after the the
- 12:00function the stack pointer is pointing
- 12:03to a memory location
- 12:05before to to some memory location before
- 12:07the call
- 12:08when we make a call we made room and
- 12:12but for two more just for saving two
- 12:14registers by decrementing the stack
- 12:16pointer
- 12:18by eight then when we are done with a
- 12:22call
- 12:24we are incrementing the stack pointer
- 12:25and pointing to the old location
- 12:28that we have where it pointed before
- 12:31the data may stay in the memory
- 12:34but it is labeled as free the next
- 12:37function
- 12:38call is going to scribble over it
- 12:43you'll find out that
- 12:47there are some security issues with that
- 12:49some people have figured out that
- 12:52one functions garbage maybe another
- 12:55function's
- 12:56treasure and they may go and mine for
- 12:58some useful stuff in their
- 13:00malicious programs for example so
- 13:03that is it i'll see you in a bit with
- 13:06the
- 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.