[CS61C FA20] Lecture 09.3 - RISC-V Decisions II: RISC-V Function Calls — Transcript
Full transcript
- 0:00[Music]
- 0:08hi welcome back to risk5 assembly one
- 0:11more time
- 0:12and this time we are going to talk about
- 0:15this five function calls we're going to
- 0:18introduce two new instructions
- 0:20that are going to be supporting that by
- 0:23the way that's
- 0:24that'll be about it for the new
- 0:26instructions in
- 0:27risk five all the other ones that we
- 0:30need to complete the base instruction
- 0:31set are
- 0:32just the variance of what we have seen
- 0:34before i will round up
- 0:36that all of that up in a bit so
- 0:39functions in c are very common
- 0:43in this case we are looking at an
- 0:45instruction that we may call for
- 0:47multiplication it's not the very best
- 0:49uh multiplication implementation or
- 0:51multiplication but
- 0:53it does the multiplication by adding the
- 0:56multiplicand
- 0:57to the product
- 1:01multiplier times
- 1:04remember c calls functions by value
- 1:08so what it does it needs to pass the
- 1:10value
- 1:11to the function then so when we
- 1:14run into when we execute an instruction
- 1:21we'll get to the to the instruction call
- 1:24to the function call
- 1:26and that will make us jump
- 1:29to that function then we execute the
- 1:33function
- 1:34and when we hit the return we go back
- 1:37to the next instruction in the main if
- 1:39that
- 1:40next instruction is again a function
- 1:42call
- 1:44we're gonna go and jump again to the
- 1:48function
- 1:49execute that call again and return
- 1:53to the next instruction in the code
- 1:56and keep doing that as many as function
- 1:59calls are there
- 2:01so what kind of risk 5 assembly code
- 2:03should be used for this well i already
- 2:05said it
- 2:06there should be different kinds of jumps
- 2:10so let's get to that there are six
- 2:13fundamental steps
- 2:14in calling a function the first step put
- 2:17arguments in place where function can
- 2:19access them
- 2:21two transfer control to function three
- 2:24acquire local storage resources needed
- 2:27for function
- 2:29four perform the function or the task
- 2:33associated with it
- 2:35then put the return value in place where
- 2:38calling code
- 2:40can find it and restore any registers
- 2:43to their previous state release local
- 2:46storage
- 2:47six return control to the point of
- 2:50origin
- 2:51since a function can be called from
- 2:54several points in a program
- 2:55and program is back in control
- 2:59i'll give you an example that is not
- 3:00exactly the same but
- 3:02resembles this well so you're away
- 3:06you're away to college in normal
- 3:08circumstances
- 3:09and you go back to visit your parents
- 3:12assuming that your parents maybe are not
- 3:14that
- 3:15far away you're in berkeley and they're
- 3:17in silicon valley so
- 3:19they're not that far away you go back
- 3:21and you ring the doorbell
- 3:23and they are not there so what do you do
- 3:25you go straight for the planter where
- 3:27you know the spare
- 3:27key is or perhaps the fake croc from
- 3:30home depot
- 3:31where the spare key is you take the
- 3:34spare key
- 3:35and enter the house
- 3:39there you're in control you
- 3:45take you acquire the local storage
- 3:47resources perhaps the refrigerator and
- 3:49perform the function after that
- 3:51you rate the refrigerator but after
- 3:54you're done with the refrigerator raid
- 3:56you clean up after yourself because
- 3:59things would not be good if you don't do
- 4:01that
- 4:02and then you restore everything to the
- 4:05original state
- 4:06minus the food in the refrigerator put
- 4:09the key back in the planter
- 4:11such that when your
- 4:14sibling comes back from school can
- 4:17perform
- 4:17exactly the same function operating the
- 4:19refrigerator
- 4:21and cleaning up after themselves before
- 4:23they go perhaps to a soccer practice
- 4:29anyway it's not exactly the same but
- 4:31it's quite
- 4:32similar to what functions need to do
- 4:34including
- 4:35the cleanup procedure all right
- 4:39let's take a look at what are the risk
- 4:41five calling conventions
- 4:43they're fairly straightforward first
- 4:47we would like to use registers because
- 4:49they're faster than memory we don't want
- 4:51to always have to go
- 4:53to the memory to pass the values so we
- 4:55are going to be using registers
- 4:58extend to x17 as eight argument
- 5:01registers they'll be labeled a
- 5:03naught to a7 and the two
- 5:06of them are going to be holding the
- 5:08return values a naught
- 5:10and a1
- 5:13return address is going to be stored in
- 5:15register x1 and we are going to call
- 5:17that
- 5:18ra also registers
- 5:22s 0 s 1 and s 2 to s 11 are going to
- 5:26have a special meaning of so-called
- 5:28saved registers
- 5:30let's take a look at how does
- 5:33how does a function look like in
- 5:36assembly
- 5:37so we have a simple function up here
- 5:40in c it is a sum of two numbers and then
- 5:44when we
- 5:45call that function um the function will
- 5:49take the arguments the values of the
- 5:50arguments x and y
- 5:52and return the sum
- 5:55like everything else in risk 5
- 6:00instructions are 4 bytes 32 bits and
- 6:03each one of them has its own address
- 6:05addresses are shown here in decimal
- 6:06values one thousand thousand
- 6:08four eight twelve sixteen
- 6:11that's where the main program will be
- 6:13stored and
- 6:14the function may be at a different
- 6:17address
- 6:18two thousand two thousand 2004
- 6:21so what happens here when the main
- 6:24is calling the function what it needs to
- 6:28do is to prepare
- 6:29the things so the function can
- 6:33find them so first what
- 6:36we see here the values a and b are going
- 6:39to be stored in s naught in s1
- 6:42so we are going to move s naught in a
- 6:44naught
- 6:45and s1 in 2 to a1
- 6:48those are the ones that are going to be
- 6:51passed to the function
- 6:53and then we need to prepare for the
- 6:55return we are going to take the next
- 6:57instruction here 10 16
- 7:0210 16 and put it
- 7:06a as a return address in the array
- 7:09register
- 7:11then we are ready to jump to the
- 7:14function
- 7:15so what we do we simply jump to the sum
- 7:19sum does its stuff adds a not
- 7:23in a1 saves the result in a
- 7:27return argument a naught and it's done
- 7:30then it's ready
- 7:31to jump back to the main we use a new
- 7:34instruction here
- 7:36jrra to return
- 7:39to the value to the address stored in
- 7:42the array
- 7:43which is 10 16. important thing here
- 7:47we don't use a jump for returning
- 7:50because
- 7:50jump requires us to code up the address
- 7:54value
- 7:55function is going to be called from many
- 7:57places
- 7:58in the code so we better use a variable
- 8:02for that
- 8:02that's why we use jr
- 8:06all right
- 8:09what is instruction support for
- 8:11functions we actually
- 8:13have a new instruction here a new
- 8:16instruction very useful
- 8:18is called jump and link
- 8:21so instead of using two instructions
- 8:25to jump to the function which is saving
- 8:29a value of the next instruction in our a
- 8:32and then jumping to the sum we can just
- 8:35write
- 8:36jowl sum and that automatically saves
- 8:38the return address the next
- 8:40address in the code in the return as a
- 8:43return address
- 8:44in next one so that is very very
- 8:48useful very convenient because
- 8:52first we don't have to think about this
- 8:54next address
- 8:55and we are supporting the common case so
- 8:58since this is
- 8:59something function calls are really
- 9:01really frequently in
- 9:03in assembly we are going to support a
- 9:06common case
- 9:07and execute it fast with a more compact
- 9:11code
- 9:14jump in link should probably have been
- 9:17called
- 9:18link and jump because we first link and
- 9:20then jump
- 9:22link is basically forming an address or
- 9:24a link
- 9:25that points to the calling site to allow
- 9:27allow function to return
- 9:28to the proper address to the next
- 9:30address so we can just
- 9:33use it as jowl function label of course
- 9:36it's called a jowl because it's going to
- 9:37be
- 9:38hanging out in the green sheet with all
- 9:40the other jumps
- 9:41not with loads
- 9:45and then the other one that we have seen
- 9:46is jr there is actually a
- 9:50a pseudo instruction that we use
- 9:54as return return
- 9:57is jr to return address so at the end of
- 10:01a function call we'll generally just
- 10:03find
- 10:03a return or rat
- 10:07that's basically it for function calls
- 10:10we
- 10:11have learned how to call a function from
- 10:14c
- 10:14and how does that translate to
- 10:17risk 5 assembly just a few notes
- 10:21there are actually not that many
- 10:23instructions
- 10:25several instructions that i mentioned
- 10:27are pseudo instructions
- 10:29because they're special cases of gel or
- 10:32yet another instruction here that you're
- 10:34going to see
- 10:35gel r you can look at this
- 10:39we don't really need a special
- 10:40instruction for a jump
- 10:42if we don't save the return address we
- 10:44put it in x0
- 10:46that's our jump same thing with gel r
- 10:51we haven't learned that we're going to
- 10:52see it a little bit later but
- 10:54jr is essentially a special case
- 10:57of java
- 11:01that's it for function calls we are
- 11:03going to see a few examples after this
- 11:06so see you in just a bit
- 11:29you
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 09.3 - RISC-V Decisions II: RISC-V Function Calls by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 1,480 words across 276 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.