[CS61C FA20] Lecture 08.3 - RISC-V lw, sw, Decisions I: Decision Making — Transcript
Full transcript
- 0:00[Music]
- 0:09welcome back
- 0:10to risk five assembly language we have
- 0:12learned
- 0:13eight risk five assembly instructions so
- 0:15far in the next 15 minutes we are
- 0:17almost going to double that number the
- 0:19instructions that we have learned so far
- 0:21deal with addition and subtraction
- 0:23operations with the memory
- 0:24we can actually build a computer with
- 0:26that that would be more like a
- 0:28calculator or
- 0:30a stack machine we can we could build a
- 0:32stack machine those kind of
- 0:33things were popular say in the 60s or
- 0:3570s
- 0:36and all the action there happens on the
- 0:39stack
- 0:40but in order to support modern
- 0:43programming languages
- 0:44we need to support computer decision
- 0:46making example of that in c
- 0:48is the if statement and
- 0:51in risk 5 there is no x if statement but
- 0:54there is its counterpart i wouldn't say
- 0:56it's an equivalent it's a counterpart
- 0:58um that that is a set of branch
- 1:02instructions
- 1:03branch instructions are those that
- 1:05support decision making they
- 1:06change the the order of executions of
- 1:10instructions in the code so
- 1:14here is an example of one of those
- 1:16instructions
- 1:17branch if equal mnemonic is
- 1:20beq and it compares
- 1:24the contents of the registers reg 1 and
- 1:26reg 2
- 1:27and if they are the same if they are
- 1:30equal
- 1:30then it will jump to a label l1
- 1:34label l1 is just a symbolic address for
- 1:36another part of the code
- 1:39otherwise if the contents are not the
- 1:41same
- 1:42it will execute the next statement
- 1:45next instruction that is in in our
- 1:48program
- 1:50next line that is in our program
- 1:53um beq has its counterpart
- 1:57b and e which stands for branch
- 2:01if not equal it will take a look at the
- 2:04contents of two registers and if they're
- 2:05not equal
- 2:06it will branch there are two types of
- 2:10branches in
- 2:10assembly and risk five is no different
- 2:12than that there are conditional branches
- 2:14and unconditional branches
- 2:16conditional branches basically evaluate
- 2:18something that we
- 2:20asked them too um examples what we have
- 2:23seen are already branch of equal
- 2:25and branch if not equal but we can also
- 2:28compare magnitude so we have two
- 2:30instructions
- 2:31branch if less than and which is blt
- 2:35and branch if greater or greater than or
- 2:38equal
- 2:39which is bge
- 2:43and then we have unsigned versions of
- 2:45those bltu
- 2:46and bgeu
- 2:50there is an unconditional branch which
- 2:53in risk five is a jump
- 2:54it is just encode is j and then a label
- 2:58whenever we run into that
- 3:02instruction we basically jump to another
- 3:05location in a program
- 3:07it's like a go to um
- 3:11it is always as executed it corresponds
- 3:14to an
- 3:14always statement now
- 3:19when somebody may ask
- 3:22oh can we just make an unconditional
- 3:25branch out of
- 3:26a conditional one for example we say
- 3:29branch if equal x0
- 3:32x0 label yeah and that will definitely
- 3:35always
- 3:36jump to a label but there is one
- 3:40subtlety
- 3:40there the range of that branch the range
- 3:43of branches
- 3:44is shorter because we are limited
- 3:48to 32-bit instructions in risk five
- 3:51so these 32-bit instructions you know
- 3:54you have to
- 3:55encode somehow the type of instruction
- 3:58which kind of registers are we comparing
- 4:00which one ones of the 32 registers are
- 4:02we comparing
- 4:03and then there is a little bit to place
- 4:05the label
- 4:06which is essentially an immediate value
- 4:10in jump we just say jump and then have
- 4:14some kind of an immediate value so the
- 4:16range of an unconditional
- 4:17jump is is is longer it's
- 4:20reaches farther let's take a look at a
- 4:23couple of examples of how
- 4:25does an if statement translate to risk
- 4:28five
- 4:28so here is an a piece of c code
- 4:32if i equal equal j compares the
- 4:36values of i and j then we should
- 4:40say that f equals to g plus h
- 4:44let's see how does that translate into
- 4:47risk five assembly
- 4:49the first thing we need to figure out if
- 4:52i and j
- 4:53are the same and their contents are in
- 4:55the registers
- 4:56x13 and x14 and look at this here is
- 4:59something that might surprise you
- 5:00but totally makes sense instead of
- 5:03saying
- 5:04branch if equal x13 x14
- 5:08we are using b and e we are inverting
- 5:12this this
- 5:13this uh equal equal
- 5:16condition why are we doing that it
- 5:19it makes sense because if you think
- 5:22about how does the flow
- 5:24of execution go here this is what
- 5:27happens
- 5:29if b and e
- 5:34branch if not equal if these two the
- 5:36contents of the registers are
- 5:38not equal we'll go this way
- 5:42if it is not true we'll go down
- 5:45so in this case in the
- 5:49c in instruction
- 5:53we would like to add the two numbers if
- 5:56i
- 5:56and j are equal which is essentially
- 5:59what
- 6:00happens automatically on the next
- 6:02instruction
- 6:03if the branch is not taken so it does
- 6:06make sense in this case
- 6:08to flip this around and ask oh if
- 6:11they're not equal
- 6:12then jump around this instruction that
- 6:15you would like to do
- 6:16if they are equal so if they're not
- 6:19equal we'll jump around
- 6:21or branch around to be more precise go
- 6:24to the exit
- 6:25if they are equal then
- 6:29the ad will be executed
- 6:32hopefully this made sense this is a very
- 6:35common thing that we'll do
- 6:37and it is very helpful if you think
- 6:39about it this way
- 6:40the direction of true and direction of
- 6:46false here is another example
- 6:49it's an if else statement and it's
- 6:52exactly the same that except that we
- 6:54added the x
- 6:57condition here that will take
- 7:00f equals to g minus h
- 7:03if i is not equal to j now this one
- 7:07you can do in different ways and look
- 7:09similar
- 7:10but let's stick to the idea that we
- 7:13always flip this original condition so
- 7:15we'll have b
- 7:16and e and then if b and e is true
- 7:20b and if x 13 and x 14 are not equal
- 7:24we will skip to else in else
- 7:28while sub sub will subtract
- 7:32the two registers
- 7:35and then we'll exit if they are
- 7:41if b and e does not
- 7:45branch if this instruction doesn't
- 7:47branch we'll execute the next one so
- 7:49we'll add
- 7:50the f and j j and h and store it in f
- 7:54now there is a common mistake and i've
- 7:57made it gazillion times
- 7:59and that mistake is by omitting
- 8:02this j instruction to exit what happens
- 8:05if we don't have it there so then
- 8:08if we don't branch
- 8:11here we will add the two the contents of
- 8:15the two registers and put them in x10
- 8:18then if there is no jump we will execute
- 8:21the next instruction that will subtract
- 8:23the contents of two registers and put
- 8:26them in put the result in extend
- 8:28and it'll always do that and you'll be
- 8:30looking at there debugging this code and
- 8:32scratching your head what
- 8:33you know why why do i never get this
- 8:36ad to to to happen even though i
- 8:40wrote it in there well because it gets
- 8:41over it and the content or register gets
- 8:43overwritten
- 8:44so this j this jump is a must
- 8:51just a quick words about the magnitude
- 8:53comparison risk five
- 8:54we said we already have them we have
- 8:56branch on less than
- 8:58we also have branch on greater or equal
- 9:00but we do not
- 9:02have a branch on less
- 9:06or less than or equal and we do not have
- 9:09um branch on greater than y
- 9:13because we are trying to save to reduce
- 9:16the number of these instructions
- 9:18and those are absolutely not necessary
- 9:20we get them by flipping
- 9:22the order of two operands the contents
- 9:26of the two registers
- 9:27that we would like to compare so they're
- 9:30absolutely not necessary
- 9:32but what is necessary is to have an
- 9:34unsigned version
- 9:35of a branch so there is a bltu
- 9:40and bg eu
- 9:43that compare the unsigned versions of
- 9:46the integers it's really
- 9:50important to to remember which one are
- 9:52we using when so whenever we are
- 9:54comparing
- 9:55um signed numbers into this complement
- 9:58we are using
- 9:59blt and uh if we would like to compare
- 10:02unsigned
- 10:03it is a bltu now one thing that
- 10:07is kind of convenient to remember here
- 10:09which instruction do we have
- 10:11do we have blt in our instruction set or
- 10:14bgt
- 10:16well the way how i remember it is by
- 10:18remembering the
- 10:19ring that there does exist a type
- 10:22of a blt sandwich but i have not yet
- 10:25seen
- 10:26a bgt sandwich blt stands for bacon
- 10:29lettuce tomato
- 10:31all right let's move on and take a look
- 10:33at a little bit more
- 10:35complex control so you'll find different
- 10:38types of loops and see
- 10:39for example while do while and four
- 10:43they are mapped on exactly the same
- 10:47set of branching eq ranging
- 10:50branching instructions that we have seen
- 10:52so far no difference there
- 10:54let's take a look at a little bit more
- 10:55complex example here so here is a
- 10:58part here is a piece of c code
- 11:01that looks at an array of 20 integers
- 11:06and adds them all up so
- 11:09color coding here is used to easily
- 11:12identify what goes where which registers
- 11:15correspond to which variables so first
- 11:18we'll take the pointer
- 11:19to the first element in the array a0 and
- 11:22copy to register eight
- 11:24because we're going to be changing it
- 11:26and we do not want to destroy it that's
- 11:28another good practice that we would like
- 11:32to use in the
- 11:32in the assembly we don't want to destroy
- 11:35the content the starting content of that
- 11:37you know of that register because
- 11:40we may need it later then we need to
- 11:43initialize everything will initialize
- 11:44sum to zero
- 11:45index i to zero and
- 11:48we do need to to store the
- 11:52final value 20 in
- 11:55the register x13 because we'll need to
- 11:58compare
- 11:59um the i to that value
- 12:02and find out if we uh we have completed
- 12:05our summation
- 12:06um remember we don't have branching with
- 12:09immediates in risk five
- 12:11contents of both variables that are
- 12:13going to be
- 12:14compared need to be in the register so
- 12:16there is no
- 12:17comparison to uh to an immediate
- 12:21so let's start our loop here is the
- 12:23start of the loop and there is one
- 12:25really important thing that i want
- 12:26everybody to remember
- 12:28test the condition if you should exit
- 12:31right away
- 12:32because it's the same logic that we've
- 12:34been following following so far
- 12:37if the condition to exit the loop is
- 12:39true we just
- 12:40go to the end to done
- 12:43so in this case if x11
- 12:46if i has reached 20 we just
- 12:50go to done that's yes if not we stay
- 12:54inside the loop so when we are in the
- 12:57loop
- 12:58we load the next value of the next
- 13:01element of
- 13:03a into register x12 we add it
- 13:06to the running value of the sum which is
- 13:09in x10
- 13:10then we increment the pointer
- 13:14remember we are we would like to load
- 13:15the next integer integers are 4 bytes
- 13:18wide
- 13:19risk five addresses every byte in the
- 13:21memory so we need to increment this
- 13:23pointer by four this is a
- 13:25very common mistake always keep that in
- 13:28mind
- 13:28because you're going to be having a fun
- 13:31time
- 13:31debugging these if you are not
- 13:33incrementing if you're working with
- 13:35integers and not incrementing memory
- 13:37pointers very four
- 13:38we increment i by one and that's
- 13:42basically it
- 13:43we jump back to the beginning of the
- 13:45loop when we we are just missing one
- 13:48more
- 13:49label which is done to say that we have
- 13:52exited this loop
- 13:54and that's it that wraps up our
- 13:57discussion of branches see you in a bit
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 08.3 - RISC-V lw, sw, Decisions I: Decision Making by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 1,945 words across 343 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.