[CS61C FA20] Lecture 08.1 - RISC-V lw, sw, Decisions I: Storing in Memory — Transcript
Full transcript
- 0:00[Music]
- 0:09hey welcome back
- 0:11i have a question for you how do you use
- 0:14patterson and hennessy textbook to
- 0:16figure out if an egg is hard boiled or
- 0:18not
- 0:21you take the book use it as a flat
- 0:24surface
- 0:25and spin an egg on it when you stop the
- 0:28spinning whether it
- 0:31wiggles or not determines whether the
- 0:34egg
- 0:36is hard-boiled in this case it's most
- 0:38likely
- 0:39hard-boiled
- 0:42but we're still talking about 65
- 0:44instruction set architecture
- 0:46and assembly instructions we've learned
- 0:48three instructions so far
- 0:49add sub and add immediate
- 0:52the book and the green sheet uses a very
- 0:55compact description to
- 0:56very compactly remind you what these
- 0:58instructions do it is in a language
- 1:00called
- 1:01verilog we are not learning verilog in
- 1:04this class
- 1:05um but it is a language that in syntax
- 1:08is very similar to c
- 1:09although it does completely different
- 1:11stuff it is used for
- 1:12describing hardware but given its
- 1:17simple syntax similar to c we can infer
- 1:20what each instruction does from its
- 1:23compact description
- 1:25for example the add instruction
- 1:30adds the contents
- 1:34of the register rs2 to the contents of a
- 1:37register
- 1:38rs1 and store it is it in the
- 1:40destination register rd
- 1:42conversely the sub instruction
- 1:46takes the contents over it
- 1:50of a source register rs2 subtracts it
- 1:53from the source register rs1 from the
- 1:55contents of a source register s1
- 1:58and stores the result in the destination
- 2:01register rd
- 2:04our and immediate instruction is similar
- 2:06but a little bit different
- 2:08it takes the contents of the source
- 2:10register rs1
- 2:13and adds an immediate value to it
- 2:15immediate value
- 2:17is a constant that is encoded as a part
- 2:20of an instruction a
- 2:22instruction so it is there in the
- 2:24instruction
- 2:25the processor takes that number from the
- 2:27instruction and adds it to the
- 2:29contents of a register rs1 and
- 2:33stores the result in the destination
- 2:35register rd
- 2:37there is a limited number of registers
- 2:40in
- 2:40every processor risk fire architecture
- 2:42specifies 32 registers
- 2:45out of which we can theoretically use up
- 2:47to 31 because
- 2:49register x0 is
- 2:53hardwired to zero so we can use up to 31
- 2:55of them
- 2:56for storing temporary variables
- 3:00in them but what we'll find out
- 3:04some of these registers in more complex
- 3:06programs
- 3:07uh have some designated use
- 3:10for something else so we generally will
- 3:13be able to use less than 30 registers
- 3:15registers are scarce so it's important
- 3:17to be
- 3:19cautious on how we use them
- 3:23we want our programs to be fairly
- 3:25compact and we would like to reuse
- 3:27registers as temporary as to sort them
- 3:31temporary variables so for example in
- 3:34the
- 3:35previous segment the last example that
- 3:37i've done
- 3:38you might have seen that we could have
- 3:40used one
- 3:41less register to store temporary values
- 3:45in general it is a job of an optimizing
- 3:47compiler
- 3:49to minimize the register usage
- 3:52um when we are done with an optimization
- 3:57what we'll find out is that
- 4:00we have to use for a particular
- 4:04computational routine or for a compute
- 4:06kernel
- 4:07we have to use certain number of
- 4:09registers
- 4:10that number of registers is called the
- 4:12register
- 4:13footprint for a particular kernel
- 4:17but we have already seen that we use
- 4:20we work with larger amounts of data
- 4:24so we'll have to spill out of the
- 4:27registers and spill over
- 4:28to memory so it's important to
- 4:30understand how does the memory
- 4:32work with the processor we have seen
- 4:35this picture before
- 4:37it is a picture that will so
- 4:41frequently appear in this class that it
- 4:43is
- 4:44well worth remembering perhaps getting a
- 4:48tattoo or a temporary tattoo till the
- 4:49end of the semester until
- 4:51it gets completely engraved in our
- 4:53memory
- 4:55on the left hand side we have a
- 4:58processor
- 5:00connected to the memory and there is
- 5:02input in the output we are going to
- 5:03focus for now on the processor
- 5:06and the memory processor
- 5:09has its data path with the registers and
- 5:12and alu inside of it
- 5:14and the memory is there
- 5:17memory stores our program and the data
- 5:21that we work on
- 5:24memory can be viewed as a large
- 5:27one-dimensional array
- 5:29with the address acting as the index to
- 5:32that array
- 5:33starting at zero to access a word in
- 5:36memory processor must supply
- 5:39an address an address is right here
- 5:44and we are going to use a little
- 5:46different color for that
- 5:47address is there
- 5:51so address is calculated by a processor
- 5:54and is used to point to a particular
- 5:56word
- 5:57in the memory
- 6:00the address is typically specified as an
- 6:03offset
- 6:04to a base pointer
- 6:08so each data rate that is laid out in
- 6:10the memory will have its base pointer
- 6:12and when we would like to access a
- 6:15particular
- 6:16element in that array we would have to
- 6:18calculate an
- 6:19offset with respect to that base point
- 6:21there
- 6:23there are two operations that we would
- 6:25like to do once
- 6:26when we point to a particular word in
- 6:28the memory
- 6:30we can read or we can write
- 6:34it's really important to remember the
- 6:38direction of these two operations
- 6:43we always load from
- 6:46and store to the memory the world is
- 6:49processor centric all the main action
- 6:52happens inside the processor
- 6:54and the memory is just out there so when
- 6:56we are writing data
- 6:58we are storing to the memory so remember
- 7:01this store
- 7:02to memory because it will help us really
- 7:05if you ever get confused with the
- 7:07instructions you know what do they do
- 7:09store to the memory is going to help a
- 7:12lot
- 7:14similarly when we are reading we are
- 7:16reading
- 7:17from the memory into the processor so
- 7:21load from memory has a direction from
- 7:24the memory into the processor store 2
- 7:28has an arrow pointing from the processor
- 7:31to the memory the other thing that is
- 7:35important
- 7:35to remember is the layout of the data
- 7:39in the memory memory in is organized
- 7:42into words and words
- 7:44in risk five that we are studying this
- 7:46class
- 7:47are 32 bits wide they nicely match
- 7:51the register widths that we have in our
- 7:54processor
- 7:56so the data that is stored in the
- 7:57register you know nicely fits
- 8:00in in each memory world but keep in mind
- 8:04that we frequently
- 8:05like to work with work with something
- 8:07that is shorter than 32 bits
- 8:10like characters or you know values of
- 8:12colors in our images
- 8:14they're eight bits
- 8:17so we have to have an efficient way of
- 8:20putting together
- 8:21four bytes into a word such that we
- 8:24don't occupy too much space with
- 8:26unnecessary space with zeros um
- 8:29each word of 32 bits will fit four bytes
- 8:34um eight bits each
- 8:37so when we store
- 8:40bytes in words in memory
- 8:44which way do they go in risk 5
- 8:47architecture they follow a particular
- 8:49convention
- 8:50that is called has a funny name called
- 8:52lol endian
- 8:54what does that mean that means that the
- 8:56least significant
- 8:57byte in a word this one over here
- 9:00gets the smallest address
- 9:03inside that word so gets you know fits
- 9:06into the bits 0 to 7
- 9:09in a word it could have been done the
- 9:11other way
- 9:12it would have been called differently
- 9:14but
- 9:15it follows in risk 5 the little and
- 9:19the unconvention so essentially the
- 9:21least significant
- 9:23byte gets the lowest byte
- 9:26address then goes to the next one and
- 9:29the next one and so on
- 9:30moving to the left and then the next
- 9:35address comes around and we continue
- 9:38serpentining through the memory array
- 9:41words have their addresses then
- 9:43inherit them basically from the bytes
- 9:46so this would be a word with another 0
- 9:50this word would have an address 4 and
- 9:53this word would have an address 8 and
- 9:55this would
- 9:56have an address fault so essentially
- 10:00word address is the same as the address
- 10:02of rightmost byte
- 10:04its least significant byte
- 10:07okay hopefully this was clear to make it
- 10:11even more clear
- 10:13let's understand different options how
- 10:15this can be
- 10:17how data can be written in the memory
- 10:21we follow this convention of indian-ness
- 10:25and
- 10:25indians has you know kind of a it's a
- 10:27it's a funny
- 10:28word that you know computer scientists
- 10:31have associated with the
- 10:33novel from the 18th century from 1726
- 10:37written by an irish writer jonathan
- 10:40swept
- 10:41named gulliver's travels and in his
- 10:43travels
- 10:44um gulliver
- 10:47encounters an imaginary society where
- 10:50there is a
- 10:50an interesting rift going on where there
- 10:54is
- 10:54basically a political issue should the
- 10:56eggs be broken
- 10:58hardware legs should be broken
- 11:01little side
- 11:05or the big side so the people who break
- 11:08the eggs
- 11:09on the little side are called little
- 11:11indians and the people who break the
- 11:14eggs
- 11:16big side first are the big indians
- 11:19it's a kind of a an interesting dispute
- 11:22um sounds silly until we see what kind
- 11:25of these political disputes we have in
- 11:2721st century
- 11:30but going back to risk five and in
- 11:34instruction set architectures risk five
- 11:36hopefully you'll remember
- 11:38forever is a little endian follows the
- 11:40low ndm convention
- 11:42so risk 5 always breaks the end from the
- 11:45low side
- 11:48there are big indian architectures out
- 11:50there but let's make sure that you
- 11:52understand
- 11:53the main difference between them now
- 11:56endianness governs only the order
- 11:59of how bytes are stored in memory
- 12:03bits are always things stored the same
- 12:05way
- 12:07they the least significant bit goes
- 12:10at the lowest bit position right there
- 12:13so if you have a
- 12:14if you would like to write c2
- 12:19the more significant nibble one
- 12:22zero zero one always goes in the upper
- 12:26position in the byte and the less
- 12:28significant
- 12:29nibble zero zero one zero goes
- 12:33in the low you know this position in
- 12:35lower bits
- 12:39now when we talk about the endianness
- 12:42this is about
- 12:43storing the bytes so if we would like to
- 12:46write the number
- 12:471025 which easily decomposes to
- 12:5124 plus one we need to write
- 12:54four bytes the first two bytes are zeros
- 12:59and byte one would have zero zero zero
- 13:02zero zero one zero zero
- 13:04the byte zero would store a value of all
- 13:060s and 1
- 13:08at the end in little endian convention
- 13:13byte 0 is placed in the address 0
- 13:17the least significant address byte one
- 13:19goes to the address one
- 13:22byte two to address two and by three to
- 13:24add address three
- 13:25and and that's it big indian convention
- 13:29stores point zero in address three
- 13:33byte one in address two by two in
- 13:36address one and by three
- 13:37in address zero majority of this world
- 13:42follows little indian convention
- 13:44majority of processors out there maybe
- 13:46ninety percent
- 13:47our little indian but some important
- 13:51ones
- 13:52follow the big endian convention like
- 13:54ibm's big iron servers
- 13:56and some of the microcontrollers and
- 14:00a lot of automotive processors
- 14:03but this does this is not just you know
- 14:06endianness is not just the manner of
- 14:09processor architectures it exists in
- 14:11real world for example
- 14:13my name in majority of the world and
- 14:17in the us is written as foreign college
- 14:21my first name first then my last name
- 14:24goes second if i were to write it in
- 14:28china or in hungary and some other
- 14:30places
- 14:31finland i think follows that as well
- 14:36it would be my surname first and then
- 14:39followed by my first name um
- 14:44java packages follow kind of an a big
- 14:48indian convention
- 14:49internet addresses internet names
- 14:53follow um little endian convention
- 14:57one interesting thing are the dates so
- 14:59that there is an
- 15:00international standard iso standard um
- 15:038601
- 15:05the specifies of the dates should be
- 15:07written as
- 15:08first you know four digit years
- 15:11followed by two digit months and two
- 15:13digit days
- 15:16and that's a big endian way of writing
- 15:18dates and that's really convenient when
- 15:19you try to
- 15:20you know that's how i save everything in
- 15:22my computer because it's very easy
- 15:24way to figure out which pictures are you
- 15:27know come first
- 15:28how to order the pictures in time
- 15:32when i write them in european way
- 15:36it would come as days first months
- 15:39second and the year last a little harder
- 15:42to sort it that way
- 15:44but the us way is middle endian or
- 15:46mid-indian
- 15:47you for we first write the months then
- 15:49the days than the years
- 15:52don't ask me why hard to understand that
- 15:59like some other things not easy to
- 16:00understand anyway there are other
- 16:02examples
- 16:04of big indian of people who eat the
- 16:07crust first in a pizza the people who
- 16:09eat
- 16:10the thin part of the pizza first would
- 16:12be considerable in little indians
- 16:15there are many other examples like that
- 16:18we'll break here we'll come back to
- 16:21talk about actual load and store
- 16:24instructions that work with the memory
- 16:27see you there
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 08.1 - RISC-V lw, sw, Decisions I: Storing in Memory by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 2,114 words across 393 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.