[CS61C FA20] Lecture 12.2 - RISC-V Instruction Formats II: Upper Immediates — Transcript
Full transcript
- 0:00[Music]
- 0:10hi welcome back to a new episode
- 0:12of risk 5 instruction instruction
- 0:14encodings we have seen
- 0:16four race five instruction formats so
- 0:19far so we're going to add one more
- 0:21that's the one that is going to help us
- 0:23work with long immediates what we have
- 0:25seen so far that our immediates have
- 0:27been limited
- 0:28to being 12 bits but occasionally we
- 0:30need them to be longer
- 0:31so let's try to see a mechanism how we
- 0:34can
- 0:34use all 32 bits as an immediate value
- 0:39but before then let's recap how our
- 0:41branches work because they'll help us
- 0:43develop a bit of an intuition what we
- 0:45are trying to do
- 0:47our branches have that
- 0:51immediate field and what we have done so
- 0:53far we have
- 0:55hand compiled what replaces the value
- 0:58that replaces the label
- 0:59in the branch that label corresponds to
- 1:02the offset
- 1:03how many instructions away from our
- 1:06current
- 1:06program counter is our target branch
- 1:13now what happens if we move that
- 1:15instruction in the code
- 1:17well if you just move it branch
- 1:19instruction ahead of another instruction
- 1:21that we had in a code
- 1:22we have to clearly
- 1:26change the the the value that sits in
- 1:29the label
- 1:30that replaces the label because we have
- 1:32changed the distance
- 1:33relative distance between the two
- 1:35instructions
- 1:36so the relative offset from the program
- 1:39counter is not the same
- 1:41but often what we are going to see will
- 1:44be dealing with a movable code
- 1:46so most of these compilations are done
- 1:48by the assembly
- 1:50uh process and we often will link
- 1:53different procedures and different
- 1:55functions
- 1:56with external libraries so the code may
- 1:58be moving
- 2:00in in the memory or most generally will
- 2:04be moving in the memory so when we move
- 2:06a piece of a code the entire piece of a
- 2:08code in memory that includes
- 2:10loops and branches inside of it does it
- 2:14and do we need to replace retarget
- 2:17all of our labels and the answer is no
- 2:20and that's an advantage
- 2:22of pc relative addressing and what
- 2:25um there is also a term used for that
- 2:28that this
- 2:29is now position independent code so if
- 2:31you move a whole procedure
- 2:34to a new location all
- 2:38relative addressing is preserved there
- 2:40is another
- 2:41important thing to keep in mind is that
- 2:43the range of our branches since we are
- 2:46really limited to 13 bit offsets where
- 2:49we
- 2:50drop the last bit we keep the 12 bits in
- 2:52our instruction encoding
- 2:54the range is limited to plus minus 1024
- 2:58instructions away from the branch
- 3:01instruction
- 3:02so what happens if we need to branch
- 3:04really but it may happen that we have to
- 3:06branch
- 3:07to a location that is outside plus minus
- 3:091024 instructions
- 3:12um we need help we can do that with a
- 3:14single branch instruction
- 3:16um and here is an example how this will
- 3:19happen
- 3:20so in this case we have an instruction
- 3:23branch on equal
- 3:24extend x0 to a far away location that is
- 3:27outside of
- 3:28our 20 024 range so this
- 3:31compares the value in the register x10
- 3:33with a 0
- 3:34and then branches to a far location
- 3:38if x10 is equal to 0 if it is not equal
- 3:41to 0 that goes
- 3:43and executes the next instruction in
- 3:45sequence
- 3:47now the way how we would do this we are
- 3:49going to go flip this
- 3:50branch condition so we are going to
- 3:52branch on if not
- 3:53equal when x10 is not 0
- 3:57to the next and next here is our next
- 4:00instruction that was supposed to be
- 4:02executed but in between if
- 4:05x10 is actually equal to zero we are
- 4:07going to put a jump
- 4:08and the idea here is that the jump is
- 4:11going to have
- 4:12a much longer reach than our
- 4:15branches so that's what you're going to
- 4:18see
- 4:19as a consequence of the next little
- 4:22section
- 4:23about long immediates so
- 4:26there are there is a format that helps
- 4:29support
- 4:30long immediates this is a u format
- 4:33that stands for upper immediate
- 4:36instructions or some people
- 4:37say unusually long immediates
- 4:41in the instruction um
- 4:45remember our immediates were limited so
- 4:47far to 12 bits
- 4:49in the instructions in the i format
- 4:52also in the branches and and in in
- 4:55stores
- 4:57and we got that by reusing the
- 5:01one of the register fields and the
- 5:04the funct7 field now 12 bits
- 5:08is good but if you want to put
- 5:12load the entire 32-bit operand
- 5:15into a register uh all the entire 32-bit
- 5:19value into a register
- 5:22then we are missing 20 bits so
- 5:25the u format provides a way
- 5:28for getting these missing 20 bits into
- 5:32our register
- 5:33so here is how the instruction looks
- 5:35like it's fairly straightforward
- 5:37it has an opcode in the lowest seven
- 5:39bits there
- 5:40there should be no surprise this is the
- 5:42same field what we have seen before
- 5:44and there are two different top codes
- 5:46for two instructions that we have there
- 5:48the instructions are lui and now epc
- 5:52louis stands for load upper immediate
- 5:56essentially loads an upper immediate
- 5:59into a register destination register rd
- 6:03and leaves the bottom 12 bits resets the
- 6:06the bottom 12 bits to zeros and then
- 6:10au pc adds upper immediate
- 6:13to pc to the program counter
- 6:17and stores the result in the destination
- 6:19register rd
- 6:21now there are two separate top codes
- 6:23that we are using for these two
- 6:25instructions and that's expensive
- 6:27there is an interesting trick that is
- 6:29happening here it's outside of this
- 6:30class
- 6:31the top code is used across different
- 6:33variants
- 6:34of the risk 5 instruction set
- 6:36architecture
- 6:37rv 32 64 128
- 6:41now so
- 6:44what we need for this instruction is
- 6:45essentially 12 bits to say
- 6:48what kind of instruction it is and what
- 6:50is the destination register
- 6:52we have miraculously freed up the top 20
- 6:54bits
- 6:55and that's where we can put our 20-bit
- 6:57immediate
- 6:58very well let's see how we use this so
- 7:01louis writes
- 7:02the upper 20 bits in the destination
- 7:05with the immediate value and clears the
- 7:06lower 20 bits
- 7:08so when we would like to put in a 32-bit
- 7:12immediate into a destination register we
- 7:15need to chop
- 7:16that immediate into two halves
- 7:19this has happened to some other louis
- 7:21which was a french king
- 7:23any similarities are accidental here so
- 7:27we are going to cut this
- 7:30immediate into two hops first we are
- 7:32going to louie the upper 20 bits
- 7:35into the destination register that is
- 7:36shown in here
- 7:38in x10 that is going to put
- 7:41the upper five nibbles into that
- 7:45destination register it is going to
- 7:47leave the lower
- 7:49three nibbles as zeros then we can
- 7:52simply do
- 7:53add immediate of the value that contains
- 7:56the lower three nibbles so in this case
- 7:58we would like to put the final value
- 7:59eight seven six five four three two one
- 8:02into a register x10 and we did it in two
- 8:05steps in the first step
- 8:06lui put eight seven six five four
- 8:10into the upper 20 bits or upper
- 8:13five nibbles and then add immediate
- 8:16finish that by adding three to one
- 8:20to the bottom this worked fine
- 8:23but there is a catch it does not always
- 8:27work
- 8:28out of the box here is an issue dead
- 8:31beef is a completely legit
- 8:34hexadecimal number so how do we
- 8:37load that beef into the register x7 x10
- 8:41so let's try to follow the same
- 8:42procedure
- 8:44first we go and load the
- 8:48the louis the first part that b
- 8:51all right that's good so now x10
- 8:53contains the value
- 8:55that b000 and then if we add the
- 8:58immediate
- 9:00if there we end up with dead
- 9:03a if oh what happened here we were
- 9:06supposed to get dead beef
- 9:07we got that eighth
- 9:10and the problem there is remember
- 9:15add immediate looks at this
- 9:18the most significant bit of e and
- 9:22sine extends it so since it found a
- 9:25one in the most significant bit position
- 9:27of hexadecimal value e
- 9:29it sign extended it all the way to the
- 9:32upper 32 bits
- 9:34so when adding these operands in the
- 9:36upper five nibbles
- 9:38it placed all once which is equivalent
- 9:39to a minus one
- 9:41that's why this bit b
- 9:44the the the nibble that contain b got
- 9:48decremented
- 9:50so that's a problem this doesn't sound
- 9:52right so what do we do well
- 9:54we know that that this is not right so
- 9:57but can't we just use add immediate
- 10:00unsigned isn't there an instruction like
- 10:02that
- 10:02nope there is no
- 10:05add immediate unsigned in risk five
- 10:09remember we basically ran out of fund
- 10:11three um
- 10:13space and there was no room for another
- 10:16instruction in there so ad immediate was
- 10:20voted off the island
- 10:21uh at immediate unsigned was voted off
- 10:23the island um
- 10:25so we have to live with what we got here
- 10:27we would you know in order to add more
- 10:29instruction
- 10:30we had we would have had to spend
- 10:33another
- 10:34um up field
- 10:37up code field so what do we do well we
- 10:39know that what is going to happen we are
- 10:41going to have
- 10:42one less one bit lower
- 10:46value in the fifth
- 10:49nibble from the top so we are going to
- 10:54on purpose make it a little bit
- 10:57bigger bigger by a value of one so we
- 11:00have added
- 11:02plus one here
- 11:05this is supposed to be plus one
- 11:09so how do we set that beef in register
- 11:12extent we first write
- 11:14dead c by using a lui and then we add
- 11:18immediate
- 11:20if value to that and that is going to
- 11:22set
- 11:23the correct value in register x 10.
- 11:27now there is good news this is a known
- 11:31thing known procedure so every time
- 11:34we have that a compiler can take care of
- 11:37things
- 11:37so there is a pseudo instruction so the
- 11:41operation
- 11:42that is load immediate and whenever we
- 11:45try
- 11:46to load a long
- 11:49immediate into register any but in this
- 11:52case x then
- 11:53if we just write li x10 and the value
- 11:57is going to do the proper thing it is
- 12:00going to
- 12:00break it up into two proper instructions
- 12:02that will finish the job for us
- 12:04so just don't do this louis addy just
- 12:07use
- 12:08li in assembly code and you get yourself
- 12:10set with the appropriate
- 12:13long immediate okay
- 12:16that's it for louis for now we are going
- 12:18to see some
- 12:20few more uses of blue in the next
- 12:22segment but for now
- 12:25let's see one missing instruction that
- 12:27we have
- 12:28that we mentioned earlier on which is
- 12:31aui pc that
- 12:32adds upper immediate value to the
- 12:34current content of the program counter
- 12:36and stores the result
- 12:37in the destination register rd so this
- 12:40is great for
- 12:41pc relative addressing because we can
- 12:44add offsets to the current content of
- 12:48the program counter
- 12:49that sounds great in the simplest case
- 12:52something that we
- 12:53really wanted to have early on
- 12:56if we add a zero
- 13:00we use zero as the immediate value
- 13:04in our pc we are simply storing
- 13:07about the current value of the program
- 13:10counter in the destination register
- 13:12it's incredibly convenient that's
- 13:14essentially our return address
- 13:16so uh well by doing this we can put an
- 13:19address
- 13:21of a label in um
- 13:25in our epc by or in
- 13:28register x10 by saying iepc x10
- 13:32comma 0. that's it that wraps up upper
- 13:35immediates
- 13:36we are going to see one final format
- 13:39after the break
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 12.2 - RISC-V Instruction Formats II: Upper Immediates by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 1,901 words across 339 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.