[CS61C FA20] Lecture 10.3 - RISC-V Procedures: Memory Allocation — Transcript
Full transcript
- 0:00[Music]
- 0:09hi welcome back
- 0:10can you believe that we are almost done
- 0:12with the risk 5
- 0:14isa and the assembly language
- 0:18i'm getting tired too so let's get
- 0:22[Applause]
- 0:24going with wrapping it up the one last
- 0:26thing that we need to do is quite
- 0:27exciting
- 0:28it is going to tie the memory allocation
- 0:31that you have seen in c
- 0:33with what is happening with memory
- 0:35allocation in risk 5.
- 0:38so that will hopefully really
- 0:42paint a clear picture of what is
- 0:43happening with these mallocs
- 0:45and things like those and see
- 0:48so let's get to it there are two types
- 0:50of variables that we have seen in c
- 0:52there are automatic variables that are
- 0:56local to a function
- 0:57and appear when the function is called
- 1:00and then
- 1:01are discarded when the function exits
- 1:04and then there are
- 1:05statics variables that that exist all
- 1:08the time across
- 1:09entries and exits into the procedures
- 1:14so when we need these local variables
- 1:18that don't fit in the registers
- 1:20we will need to store them on the stack
- 1:25um so the this
- 1:29stack frame that we have mentioned
- 1:30before not only has
- 1:32the the return address and
- 1:36some of the registers that need to be
- 1:38saved
- 1:39but also contains all these variables
- 1:42that
- 1:43cannot fit into the registers so that's
- 1:45why it's called the procedure frame or
- 1:47procedure activation record
- 1:50it's a segment of a stack that has all
- 1:52the saved registers and local variables
- 1:55that are needed to run that procedure
- 1:59let's take a look at how does it look
- 2:01like we have seen this before
- 2:03before we call a function stack stack
- 2:06pointer
- 2:06is sitting at some address
- 2:11when we call a function we allocate the
- 2:14space
- 2:15on the stack to save the return address
- 2:19to save the arguments because they may
- 2:22get overrun
- 2:22by a nested call and then
- 2:26saved registers need to to be on the
- 2:28stack as well
- 2:29and in addition to that any of the
- 2:32the variables that don't fit in the
- 2:34registers will be there
- 2:36and that piece can be fairly large if
- 2:38you're working with matrices or
- 2:40some large chunks of data and then when
- 2:42you're done
- 2:43with the call we clean up by
- 2:46decrementing the stack
- 2:47by incrementing the stack back to the
- 2:49starting position
- 2:50here remember stack decrements
- 2:54when we add a frame to it
- 2:57and increments when we remove the frame
- 3:02don't mix this up
- 3:05so how do we actually do
- 3:09this um how do we implement what happens
- 3:13with the stack when we implement this
- 3:15nested function that we have seen before
- 3:17remember we have
- 3:19a function sum square that works on
- 3:22integers x and y
- 3:23and calls within
- 3:26itself another function that multiplies
- 3:29x with x
- 3:30and then adds y before it exits
- 3:34so that it's a nested function call
- 3:38let's take a look at it take a look at
- 3:40what do we need to put on the stack
- 3:42when we make this call what is going to
- 3:44be really helpful is to understand
- 3:46what is where and what is going to get
- 3:49over it and
- 3:50what is going to be clobbered
- 3:54um so what are the arguments
- 3:57of sum square x is going to be living
- 4:01in
- 4:06a0 and y will live
- 4:09in a1 now when we
- 4:13call malt will have also two arguments
- 4:18both times it will be x sitting in
- 4:22a naught and a one so we can leave one
- 4:26value of x
- 4:26in a zero but y
- 4:30the value of y that was in a1
- 4:33will get overwritten what else is going
- 4:36to get overwritten during a nested call
- 4:38the return address remember when
- 4:41some square got called by the main
- 4:47we put the return address where the sum
- 4:50square will be
- 4:52going back or where will it jump back
- 4:55where will it
- 4:56return but now when we have a nested
- 4:58call
- 5:02some square becomes a caller and malt is
- 5:04a callee
- 5:05we need to save the return address for
- 5:08malt
- 5:09where will mult go back and that's the
- 5:12same
- 5:12spot it's the register x1 so
- 5:16the old return address is going to get
- 5:17overwritten
- 5:19some square will have no idea where to
- 5:21go back when it's done
- 5:25so the two things that we need to say
- 5:27therefore are
- 5:28the return address and
- 5:31the y that lives in a1 so we need to
- 5:34make room for two registers on the stack
- 5:38therefore we decrement the stack by
- 5:40eight
- 5:41never make that mistake and then we're
- 5:44going to write
- 5:45the return address and a1 on the stack
- 5:51in order to set up the
- 5:54multicall in order to call mount we need
- 5:57to put its arguments in the right place
- 5:59um so we need to copy value
- 6:02of x from a naught
- 6:06into a1 and remember a move
- 6:09instruction is a pseudo instruction that
- 6:12essentially
- 6:13just copies the value like the
- 6:16the value from one register to another
- 6:19there is no move
- 6:20the the original value still stays in a
- 6:23not
- 6:24we just get a copy of it in a1 um
- 6:27it's an ad immediate with a zero so um
- 6:33you know why is it called it's a tough
- 6:36question that i think
- 6:37belongs to that originates from
- 6:40motorola's
- 6:41microprocessor since 70s why they called
- 6:43it move we don't know
- 6:45but everybody now calls it move although
- 6:48it's just a copy
- 6:50so we already we have x in
- 6:53in both a naught and a one register so
- 6:56we can call
- 6:57a malt with those arguments so we are
- 7:00going to call
- 7:01mult here and multi is going to
- 7:05return the result back in a naught
- 7:08now what do we need in order to complete
- 7:11our
- 7:12sum square instruction we need to get y
- 7:15back from the stack because
- 7:16y is gone to the stack we don't know um
- 7:23we saved it to a stack so that we can
- 7:25call it back
- 7:26so we're going to put it back in a1
- 7:29we are gonna finish our addition we'll
- 7:32add the result of malt
- 7:34uh to y or y to the result of malt
- 7:37and then we need to clean up we
- 7:40put back we recall from the stack
- 7:45the return address we
- 7:49clean up the stack pointer we increment
- 7:51the stack pointer by 8
- 7:52to put it to the previous value and then
- 7:55we are ready
- 7:56to jump back to the return address
- 8:03these three instructions at the top
- 8:06is what corresponds to to
- 8:10a push to the stack and the three
- 8:13instructions three red instructions on
- 8:16the bottom correspond to the pop
- 8:18in some sisk architectures you'll find
- 8:20out that one
- 8:21[Applause]
- 8:23push instructions pushes multiple
- 8:25registers to the stack
- 8:27and similarly pop instruction recalls
- 8:30from the stack
- 8:33what we have here is basically a
- 8:35procedure of saving these
- 8:38registers and the
- 8:41arguments of the function on the stack
- 8:44and recalling them back
- 8:45so they can be used to complete
- 8:49the our original function remember here
- 8:53some square is a color malt is a colby
- 9:01a few more things to tie this together
- 9:04with c
- 9:04when we're on c there are three
- 9:08basic memory regions that that get
- 9:11allocated
- 9:12we have static variables that are
- 9:14declared once per program
- 9:16um there is a heap that is
- 9:19dynamically allocated by a malloc and
- 9:22there is stack
- 9:23that is used during procedure calls
- 9:29where does this live in memory in risk 5
- 9:33this is set by the convention it is
- 9:36specific to rb32 it's a little bit
- 9:39different for
- 9:40rb64 and rv128
- 9:43one thing to keep in mind all these
- 9:45memory regions need to be
- 9:47aligned with a 16 byte boundary such
- 9:50that
- 9:51everything is compatible also with rv128
- 9:53but you don't have to worry too much
- 9:55about that and i was not paying
- 9:56attention to that
- 9:58so stack starts with the top of the
- 10:01memory
- 10:02which is bff ffff0 aligned with
- 10:0516 um in rv32
- 10:11rb 32 programs
- 10:14are on the bottom of the memory very
- 10:17close to the bottom of the number and
- 10:18not at the very bottom
- 10:19the very bottom is reserved for special
- 10:22stuff
- 10:23i'll mention that in a second programs
- 10:27go from the address one
- 10:30and four zeros and then static
- 10:34data segment is right above the
- 10:37uh about the text what we call about the
- 10:40program
- 10:41um and
- 10:44this global pointer points to the static
- 10:47so
- 10:48it is one zero zero zero
- 10:51zero zero zero hex and then finally
- 10:54there is heap
- 10:55that is above static um
- 10:58and that's where we place our data
- 11:00structure that we malloc
- 11:03so heap will go up towards the stack
- 11:07stack will go downwards if they meet we
- 11:09are out of memory
- 11:11for now let's see this pictorially
- 11:14um on the top we have the stack
- 11:18going downwards and dynamic data on the
- 11:21heap goes up
- 11:23static data is allocated in the middle
- 11:26and hex is from above from the
- 11:29from the address 10 000 hex
- 11:32what is on the bottom well that's where
- 11:37some of these system uh calls are
- 11:40that's where you would write to write to
- 11:43a printer or a display and things like
- 11:45those io devices
- 11:47live down there things that we'll see
- 11:49later interrupts
- 11:50and so on generally stuff that you
- 11:52should not be scribbling over
- 11:54otherwise some strange things may happen
- 11:57and that's it i've just wrapped up the
- 12:00entire
- 12:02isa that we need to know for risk five
- 12:06we have learned most of the instructions
- 12:09and we're going to
- 12:10go and summarize that in just a bit see
- 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.