[CS61C FA20] Lecture 12.1 - RISC-V Instruction Formats II: B-Format — Transcript
Full transcript
- 0:00[Music]
- 0:10hey
- 0:11hello welcome back to risk 5 instruction
- 0:14encoding
- 0:15we have covered three types of
- 0:17instruction formats so far
- 0:20those the the first format that we
- 0:22covered was
- 0:23the r format that covered r type
- 0:25instructions which are those
- 0:26instructions that operate
- 0:28between the registers in the
- 0:30microprocessor for arithmetic and logic
- 0:33instructions then we covered the i
- 0:35format that covered
- 0:37immediate operations with the immediates
- 0:40and then we covered the s format
- 0:41that covered stores let's take a look at
- 0:45the
- 0:46b format which will be covering branches
- 0:49and let's see what is a subtle detail
- 0:53why we needed yet another
- 0:56instruction format for branches first
- 0:59let's take a look at what let's recall
- 1:01what the branches do
- 1:04conditional branches evaluate
- 1:07a condition between the registers
- 1:10in this case we will need two source
- 1:12registers x1 and x2
- 1:14and this branch if equal will compare
- 1:17the values that are in registers x1 and
- 1:19x2
- 1:20and branch to a label which is another
- 1:24address in the code if that condition is
- 1:27met
- 1:28if the condition is not met if the value
- 1:30in x1 is not equal to the value in x2
- 1:33it will execute the next extraction in
- 1:36sequence
- 1:38so one thing that we notice branch
- 1:40instructions
- 1:42could use the same or similar format
- 1:45as the stores like the stores branches
- 1:49need two source registers they don't
- 1:51need the destination register
- 1:52and they need some immediate value to
- 1:55represent
- 1:56where is the label or some
- 1:58representation of the label is needed
- 2:01so the main question here is how do we
- 2:03encode that value
- 2:05of a label where do we branch to
- 2:09in order to answer that let's take a
- 2:11look at what
- 2:12do branches do and what are they usually
- 2:15being used to
- 2:16so branches in our assembly code are a
- 2:19product
- 2:20often of loops we have seen that there
- 2:22are three types of loops if
- 2:24else while in four in c that tend to get
- 2:27translated into branches
- 2:32these loops that when translated to the
- 2:35assembly code are
- 2:36relatively of a short length you know
- 2:38the number of instructions that are
- 2:39inside
- 2:40the loop is typically less than 50
- 2:44maybe a few hundred at the maximum um
- 2:47and if we need to go to some
- 2:50farther away part of a code like a
- 2:52procedure
- 2:53we would use a different type of an
- 2:55instruction which would be a jump
- 2:57which we'll see is of a j format
- 3:00the other important observation is about
- 3:03where the code is
- 3:05generally we know that the code resides
- 3:09in the memory and it is in a separate
- 3:11location
- 3:12from the data so it is in the
- 3:16limited part of a memory and it does not
- 3:18mix with the data
- 3:20in general that code or sometimes
- 3:23traditionally has been called
- 3:25text for some old reasons a
- 3:28code is a subset of
- 3:32our memory addresses and the maximum
- 3:35reach of a branch
- 3:36should not exceed the size of the code
- 3:38otherwise we'll be
- 3:39branching into a part of the memory
- 3:41where there is no program so
- 3:43things would not make sense the other
- 3:45thing to keep in mind
- 3:47as we are executing these loops the
- 3:49current address
- 3:50current address that we are
- 3:53where the instruction is that we are
- 3:54executing points to the program counter
- 3:57and as we
- 3:57execute them in sequence the program
- 3:59counter increments
- 4:01and then when we hit the branch it goes
- 4:04back
- 4:04to the original position and then we
- 4:06keep executing
- 4:07and that's our loop so
- 4:11the address of a current of the
- 4:13instruction that is currently being
- 4:14executed
- 4:15is in the program counter so it makes
- 4:18sense
- 4:19to use so-called pc relative addressing
- 4:23for the branches what does that mean pc
- 4:26relative means
- 4:27that we are going to use immediate
- 4:31value to point to the offset
- 4:35to calculate the offset from the current
- 4:38instruction that is in the program
- 4:40counter
- 4:44so in which units should that
- 4:48offset be expressed first if we are
- 4:51using
- 4:52very similar format to what we have used
- 4:54for stores
- 4:56as types we know that we can use up to
- 4:5912 bits
- 5:00to encode our offsets
- 5:03these 12 bits come from repurposing the
- 5:06destination register that we don't need
- 5:08there are five bits there and seven bits
- 5:11for the func seven field
- 5:13so that gives us 12 bits or a total
- 5:17range of
- 5:17two to the twelve addresses or plus
- 5:21minus two to the eleventh addresses when
- 5:24we need to go
- 5:25both forwards and backwards
- 5:28so what should we use as the unit of
- 5:30addressing should we use bytes as we
- 5:31used in stores
- 5:33no because we don't want to branch into
- 5:36a middle of an
- 5:36instruction somewhere in the code
- 5:39remember our code
- 5:40in the the the rb32 base instruction set
- 5:45consists of instructions that are always
- 5:4732 bits wide
- 5:49so if we branch into an address that is
- 5:52not aligned with the start of an
- 5:54instruction we would be
- 5:57branching into what looks like a garbage
- 6:00to us
- 6:01so we don't want to branch into the
- 6:02middle of an instruction
- 6:04so i think the other option is just to
- 6:06branch
- 6:07to the beginning of the instruction
- 6:09always that branch should be divisible
- 6:12branch value should be divisible left by
- 6:14four
- 6:15and it has also another benefit if we
- 6:18always branch the start of the word
- 6:22is that we have now increased the reach
- 6:24of our branches
- 6:26it is 2 to the plus minus 2 to the 11
- 6:30times the number of words and each word
- 6:32is 4 bytes are 32
- 6:34bits so that gives us you know tens of
- 6:36kilobytes of range for our branch
- 6:39which is nice
- 6:42so here is how the branch calculation
- 6:45would work
- 6:46if we don't take the branch our program
- 6:49counter
- 6:50would point to the next instruction in
- 6:53the code
- 6:54where is the next instruction in the
- 6:55code
- 6:57that is you know follows in in our code
- 7:00well it is in the next word which is
- 7:03exactly four bytes away
- 7:05we have seen that before if we do take
- 7:07the branch
- 7:08we need to calculate the offset with
- 7:10respect to the program counter
- 7:13and that offset is going to be
- 7:17the immediate value or 12 bits
- 7:2012 bit wide long immediate times
- 7:244 because in this scenario
- 7:27we would be calculating that
- 7:31offset in the unit of words
- 7:35and keep in mind keep in mind that this
- 7:37immediate value
- 7:39can be positive or negative
- 7:42making the branch go forwards or
- 7:45backwards
- 7:46but this is not really how we do it
- 7:50in risk five keep in mind this is not
- 7:53true
- 7:53there is a quirk a network
- 7:57is in a feature of the isa that supports
- 8:02um so-called compressed instruction set
- 8:05so what is that now we haven't
- 8:07talked about that before compressed
- 8:09instruction set
- 8:10is a subset of the isa
- 8:13that has 16-bit instructions now 32
- 8:17we have talked about 32 6428
- 8:20rb 32 rv 64 rv
- 8:24128 but all the instructions were 32
- 8:27bits except
- 8:27there is a subset that uses 16-bit
- 8:31instructions
- 8:33why why is that because in some
- 8:38applications
- 8:40this code size really matters
- 8:43so you want to have your code have the
- 8:46smallest possible
- 8:47footprint so that is usually for really
- 8:49inexpensive
- 8:50consumer devices a good example of those
- 8:54are like flash memory keys
- 8:57and things like those over there if you
- 9:01need to use more of a memory to store
- 9:04the your code you have to pay more for
- 9:07that
- 9:08and people don't want to do that
- 9:11so that code compactness really matters
- 9:14um so that's why risk 5 supports
- 9:16so-called
- 9:17a compressed instruction format and
- 9:19those instructions are
- 9:2016 bits wide and don't confuse that with
- 9:24rvs other rv
- 9:27types all those are 32-bit instructions
- 9:30now
- 9:32here is a catch
- 9:36we have we may have a code that has
- 9:39both 32-bit instructions and 16-bit
- 9:42instructions
- 9:43so what do we do we need to be able to
- 9:46branch into 16-bit instructions
- 9:50that may be offset by two bytes from the
- 9:53start of the word
- 9:55so that's why risk five does not this
- 9:58made a
- 9:59decision not to have two different types
- 10:02of branches
- 10:03to just have one type of a branch that
- 10:05is
- 10:06able to index in you know by
- 10:10two bytes two that is able to calculate
- 10:13the offset by two bytes
- 10:15so by default all risk 5 branches
- 10:19use this unit that multiplies the
- 10:22immediate
- 10:23by 2.
- 10:26so that is less of a reach than we would
- 10:30have had
- 10:31if we were calculating the offset
- 10:34with respect to words but it is still
- 10:37good enough
- 10:40keep in mind though that
- 10:44in this class we are only using 32-bit
- 10:46instructions we are not
- 10:47studying a compressed instruction set
- 10:49that means that
- 10:51if we don't correctly specify that
- 10:53offset
- 10:54we could branch into a middle of an
- 10:56instruction and that would not be good
- 10:57that would
- 10:58in our case produce garbage would read a
- 11:01garbage
- 11:02instruction so as a result
- 11:05the range of our
- 11:08branches is plus
- 11:11minus 1 kilowatt or
- 11:154 kilobytes so let's take a look at now
- 11:18how does the
- 11:19actual branch calculation look like
- 11:23if we don't take the branch
- 11:26we are going to execute the next
- 11:28extraction in the code which is four
- 11:29bytes away
- 11:31if we do take the branch we
- 11:34are taking we are loading a new value in
- 11:38the program counter
- 11:39that equals to the value of the
- 11:41immediate times two
- 11:43remember that it is always times two so
- 11:47those offsets are always going to be
- 11:50even numbers i'm gonna see an example
- 11:53just in a second
- 11:54but we have arrived to so we can see
- 11:58how does the b format actually look like
- 12:01so our b format looks really complicated
- 12:04the most complicated format that we have
- 12:05so
- 12:06seen so far but it actually is not it is
- 12:09pretty much the same
- 12:10as the s format because we'll find the
- 12:14op
- 12:14code in the familiar place then we'll
- 12:16find func 3 in the familiar place which
- 12:19will specify the type of a branch that
- 12:21we
- 12:21are going to execute and has rs1 and rs2
- 12:25in the familiar places the rest the
- 12:29former space that we use for the
- 12:31destination register
- 12:33and the funct7 fields are basically
- 12:37taken by
- 12:38the bits that specify the immediate
- 12:40values
- 12:41except that these immediate bits are
- 12:44shuffled over all
- 12:45over the place and initially that does
- 12:48not quite
- 12:49make that much sense
- 12:53but we'll see in the next slide how does
- 12:55it make sense
- 12:56now keep in mind that these 12 bits
- 12:59represent
- 13:00a 13 bit value because that 13 bit value
- 13:04always has the least significant bit
- 13:06equal to zero
- 13:08as you specified before so
- 13:12we don't need to store that last bit the
- 13:14least significant bit because we know
- 13:16that it is always equal to zero
- 13:18we just need to store 12 bits for a
- 13:2113-bit
- 13:22representational offset
- 13:25let's take a look at an example here
- 13:28so here is our risk-five code that
- 13:30executes a loop
- 13:32in in the beginning of that loop there
- 13:34is an
- 13:36an instruction prime chip equal
- 13:39contents of two registers to the end
- 13:42so it would branch if the the contents
- 13:45if the values in x19 and x10
- 13:48are equal to each other it would branch
- 13:51to the instruction that is specified by
- 13:53the label end
- 13:54what is the end right there
- 13:58so this instruction is at
- 14:01the value of pc
- 14:04program counter this instruction is four
- 14:07bytes away ppc plus four
- 14:10this one is at pc plus eight pc
- 14:13plus twelve what is the instruction that
- 14:15end points to
- 14:17this target instruction what should be
- 14:19inside the end
- 14:21plus 16 bytes
- 14:24so the branch offset is we
- 14:28calculated by calculating the number of
- 14:29instructions that we have from the
- 14:31branch
- 14:32and multiplying that by
- 14:36the number of bits or the number of
- 14:37bytes that we need to
- 14:39branch forward so that is 16 bytes
- 14:43one note is here if
- 14:47we accidentally put a zero here
- 14:51as the value for the offset we are
- 14:53always
- 14:54going to be branching to ourselves
- 14:58and that is an unfortunate situation of
- 15:00so-called infinite loop
- 15:02that we don't have a way to exit out of
- 15:06and also keep in mind that the infinite
- 15:09loop
- 15:09was the address of all the address of
- 15:12apple computer
- 15:13one infinite loop cupertino california
- 15:17plenty of funny names like this one in
- 15:20the silicon valley
- 15:24okay so offset here is 16 bytes
- 15:28and let's see how we should represent
- 15:32that
- 15:33so let's take a look at make a first
- 15:35pass at this instruction encoding
- 15:38we have our instruction
- 15:41and our it is supposed to
- 15:45branch forward by four bytes and we're
- 15:48going to determine this number four at
- 15:49the compilation time
- 15:52um branch does have a new opcode
- 15:55um one one zero zero zero
- 15:58one one and then we'll find out that
- 16:01the func three fields specify the type
- 16:04of a branch
- 16:05branch if um equal
- 16:08and then we need to put the val the
- 16:11the numbers of the registers that are in
- 16:14rs1 and rs2
- 16:15rs1 is x
- 16:1819 so we are going to have a binary
- 16:20value of 19 in the field for our that
- 16:23specifies rs1
- 16:24and then we are going to have final
- 16:26revalue for
- 16:2710 in this field that specifies rs2
- 16:32so the last thing that we need to do is
- 16:34to figure out how does this immediate
- 16:37um get placed in there so we do want to
- 16:40specify
- 16:41immediate of 16 bytes or actually what
- 16:44will turn out to be
- 16:46eight byte pairs right remember i
- 16:50don't need to keep that least
- 16:52significant bit so 16
- 16:54bytes is the same as 12 as eight byte
- 16:57pairs right
- 17:02before we go and figure out exactly what
- 17:05were
- 17:06what would be the value of the immediate
- 17:08let's
- 17:09figure out the reasoning for this kind
- 17:11of
- 17:13strange immediate encoding so here are
- 17:16the four types of instructions that we
- 17:18have seen so far
- 17:20and three of them use you have to encode
- 17:23immediates
- 17:25so i type encoded immediates
- 17:28in the upper bits from
- 17:31a bit position 20 to 31
- 17:34and that would translate into the
- 17:37immediate value that would be placed
- 17:39in the bottom 12 bits and the top bit
- 17:42would always
- 17:43end up being sign extended remember we
- 17:46always have to extend the
- 17:48value of the of the immediate
- 17:51to fill all 32 bits now the next type
- 17:54was the s-type
- 17:56and here immediate was spread into
- 18:00two places into two fields
- 18:03the former rd field had lower five bits
- 18:07and former funk seven field had the
- 18:10upper seven bits so we would put
- 18:14together the
- 18:15s type of intermediate um by taking bits
- 18:197 through 11 the lower 5
- 18:23and the upper 7 over here and we would
- 18:25extend the one that was in uh
- 18:27bit position 31.
- 18:32now b type when you take a look at it
- 18:35here
- 18:38is almost identical to
- 18:41the the encoding that we have used
- 18:44for the s immediate so all the bits are
- 18:48exactly in the same places
- 18:50and most importantly the 31st bit is in
- 18:53the topmost position the
- 18:55most valuable position position so
- 18:58that's the one that is going to be
- 19:00sign extended we always will find that
- 19:01one that needs to be signed extended
- 19:03in the in the top most bit position
- 19:08and all what we see here what has
- 19:11happened is
- 19:12we have moved a bit in 7-bit
- 19:15from in 7-bit from this bit position
- 19:20over there in our encoding the reason
- 19:23why this
- 19:23looks kind of strange is because we are
- 19:27humans it looks strange to us
- 19:28this is totally normal to a processor it
- 19:31allah it is a lot easier
- 19:33because we are helping processor figure
- 19:36out
- 19:37where the immediates are by keeping them
- 19:39mostly in the same place
- 19:42so our registers are going to be in the
- 19:44same place and most of our immediates
- 19:46are going to be found
- 19:47in the same place as well
- 19:50we'll see there is a benefit to that
- 19:54let's finish our example that we started
- 19:57uh some time ago
- 19:58so we're going to finish uh encoding
- 20:00this branch if equal
- 20:03instruction with an offset that should
- 20:05specify
- 20:0616 bytes or eight
- 20:09pairs of bytes so
- 20:13we are going to encode this value of the
- 20:16immediate
- 20:17with an actual position
- 20:21actual value of 16 will be placed in
- 20:24that immediate inappropriate bit
- 20:25positions
- 20:27so first thing that will notice since
- 20:29we're always
- 20:30branching by pairs of bytes we don't
- 20:34need to store the least significant bit
- 20:35it is always zero there is no reason to
- 20:38to waste our precious instruction space
- 20:42to encode zeros so we'll throw that one
- 20:44away
- 20:45then we'll take the next
- 20:48least significant four bits and place
- 20:51them
- 20:52in this bit position that was previously
- 20:55partially occupied by rd
- 20:57then we'll take the next ones place them
- 21:00in the
- 21:01lower 6 bits of the func 7 field
- 21:04add this immediate 11 in the least
- 21:08significant bit position where rd was
- 21:11and finally put the most significant
- 21:15immediate bit into the very first bit of
- 21:18the instruction
- 21:19and that's it so then we can find out
- 21:21exactly what is our value
- 21:24well um this bit
- 21:27is discarded we need to take these four
- 21:30and place them down there and that is
- 21:33our immediate
- 21:36this wraps up a somewhat lengthy
- 21:38discussion on how
- 21:40do branch instructions look like here is
- 21:42a summary of all six branch instructions
- 21:44that we have had
- 21:46they have an opcode here it is the same
- 21:49for all six of them
- 21:51and the funct3 field
- 21:54specified which branch type it is
- 21:59one thing that they wanted to you to
- 22:01notice
- 22:03you might have seen that the last two
- 22:05bits in the upcode are always 11
- 22:08and so far the next two bits are zero
- 22:11zero
- 22:18this is a feature we are only really
- 22:20using the top three bits
- 22:22in the func field to specify the type
- 22:25of the instruction that we are working
- 22:27with the bottom
- 22:30four bits are essentially used
- 22:34to encode the other
- 22:37parts of the isa and to add the
- 22:40extensions
- 22:43we don't need to pay attention to that
- 22:45now this is just for your information
- 22:47but i'll pause here see you after a
- 22:50break
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 12.1 - RISC-V Instruction Formats II: B-Format by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 3,054 words across 554 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.