[CS61C FA20] Lecture 09.1 - RISC-V Decisions II: Logical Instructions — Transcript
Full transcript
- 0:00[Music]
- 0:09welcome back to risk 5 assembly we
- 0:12have learned 15 assembly instructions so
- 0:14far
- 0:15and we are just about to add a dozen
- 0:21we are going to look into logical
- 0:24instructions in
- 0:25this five assembly they should be fairly
- 0:27familiar to us because we have seen in
- 0:30high-level languages like c or java
- 0:33bitwise ands and ors and xors
- 0:36have their direct counterparts in the
- 0:39assembly language
- 0:40and so do shifts left or right
- 0:44they're often used for isolating a byte
- 0:47or a nibble
- 0:48out of a word or by packing bytes or
- 0:51nibbles
- 0:52into a word for more efficient
- 0:56storing of data let's take a look at a
- 0:58few examples here
- 1:00logical instructions always have two
- 1:03variants
- 1:04in risk five they have a register based
- 1:06version
- 1:07and an immediate version and that should
- 1:10come
- 1:10to no surprise to you because both of
- 1:13them
- 1:13are frequently needed and frequently
- 1:15used and they're
- 1:17relatively inexpensive to implement in
- 1:20hardware
- 1:21what we'll see is that they share the
- 1:23same data path
- 1:24as arithmetic instructions
- 1:28so whatever we needed to implement ads
- 1:31and subs
- 1:31we'll be using for implementing
- 1:35ors ants xors and shifts
- 1:39so in this case let's take all an
- 1:41example
- 1:42um register based and bitwise and
- 1:46so we'll take the contents of a register
- 1:48of x6
- 1:50and it with the contents of a register
- 1:52x7 and store the result
- 1:54in the destination register x5 and its
- 1:56immediate counterpart will take the
- 1:58value from the register x6
- 2:01and it with an immediate binary value
- 2:04that represents a three and stored
- 2:06result in the
- 2:08register x5
- 2:11this kind of operation is used for
- 2:13masking as i've said
- 2:15um the previous immediate instruction
- 2:17would isolate
- 2:19the most uh the
- 2:22the least significant bits on the right
- 2:25and zero out everything else
- 2:27um in this case and immediate
- 2:30with an ff a hexadecimal will isolate
- 2:34the least significant byte
- 2:36if we end immediate with ff
- 2:39in the most significant byte position we
- 2:41would isolate
- 2:42the most significant byte keep in mind
- 2:46that the instruction itself is still
- 2:48lean
- 2:49in risk five and we don't have
- 2:51unnecessary instructions the most
- 2:52notable one that is missing there is no
- 2:54not in this client
- 2:56why because we don't need it if we
- 2:58simply xor
- 3:01our content of a register a value in the
- 3:03register with all one's
- 3:05binary um then we invert every single
- 3:08one of those bits
- 3:10remember this is done always for
- 3:11simplicity unnecessary things are not
- 3:13going to be there
- 3:15let's take a look at logical shifting so
- 3:19shift left logical comes in two forms
- 3:22sll that operates on the register
- 3:24contents
- 3:25and the immediate that works with an
- 3:28immediate value
- 3:29so in this case ss
- 3:33x11 x12 2 will take the value
- 3:37in the register x12 shift it to
- 3:40the left by two bit positions
- 3:44and fill the two least significant bit
- 3:47positions with zeros
- 3:48so if we have a hex value of two in the
- 3:53register beforehand
- 3:55after shifting it by two to the left
- 3:59we are going to end up with eight we
- 4:01have essentially
- 4:02moved this one zero
- 4:05to the left by two bit positions and
- 4:08inserted
- 4:09zeros in the least significant bit
- 4:11positions
- 4:13so a quick question for you what
- 4:15arithmetic
- 4:16operation we have just executed
- 4:19think about that for a sec we have
- 4:22multiplied it
- 4:23by four so two times four equals to
- 4:26eight
- 4:26and you can think of that this extends
- 4:29easily to
- 4:30multiplication with any power
- 4:33of two so we can easily multiply with
- 4:35two to the end
- 4:37um but here's a quick question for
- 4:41thinking about this how do we do
- 4:42multiplication by
- 4:4412 well we can
- 4:48shift to the right by three which would
- 4:51multiply by eight then
- 4:52we multiply it by four and add
- 4:56those two results we ended up with
- 4:58multiplying
- 5:00by 12. this is by the way commonly done
- 5:02in dsp
- 5:04now shift rate logical does the opposite
- 5:08and shifts the the word
- 5:12to the right by
- 5:15a given amount and fills the most
- 5:18significant bit positions with zeros
- 5:21so that's a logical shift when we are
- 5:24working inside numbers
- 5:26we generally perform the arithmetic
- 5:28shifting arithmetic shifting
- 5:30does the same shift write arithmetic
- 5:33that exist in register in in immediate
- 5:36forms moves
- 5:38and bits to the right and inserts the
- 5:40sine
- 5:41bit into the empty bits it basically
- 5:43replicates that
- 5:45topmost bit into
- 5:48the empty positions that we have emptied
- 5:50by shifting to the right
- 5:52okay so in this example if x10 contained
- 5:58a decimal value of minus 25
- 6:01see notice its topmost bit is negative
- 6:05so it's a negative
- 6:06number if we execute shift right
- 6:08arithmetic immediate
- 6:10by four bit positions the result
- 6:14is going to be this we are going to be
- 6:16we
- 6:17will shift everything to the right by
- 6:19four bits
- 6:21and fill the top most ones all with once
- 6:25what's the result of this it's
- 6:28kind of close to a division we took 25
- 6:32and divided by that by 16 and we ended
- 6:34up with
- 6:35one point something minus one point
- 6:37something
- 6:40uh there is a slight issue here by
- 6:43convention c arithmetic requires us
- 6:47to round towards zero always so in this
- 6:51case
- 6:51the result should have been rounded
- 6:54towards
- 6:55minus one not towards minus two
- 6:58which is the result that we got but
- 7:01you will have to fix that to implement
- 7:03through division
- 7:05that's doable but requires a bit more
- 7:07work
- 7:08a few more instructions actually
- 7:13that's it for the logical instructions
- 7:16we'll continue with the rest of this 5
- 7:18assembly
- 7:19in just a bit see you there
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 09.1 - RISC-V Decisions II: Logical Instructions by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 935 words across 182 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.