CS61C 2022fa L09 Decision Making Logical Ops
CS61C 2022fa L09 Decision Making Logical Ops
UC Berkeley
in UC Berkeley
Teaching Professor Computer Architecture Teaching Professor
(a.k.a. Machine Structures) Lisa Yan
Dan Garcia
cs61c.org
THE ANIMAL TRANSLATORS
“Researchers are using machine learning (ML) systems
to decode animal communication. Scientists at
Germany's Max Planck Institute for Brain Research
used ML algorithms to analyze 36,000 mole rat chirps in
seven colonies, identifying unique vocal signatures for
each mole rat, as well as a distinct dialect for each
colony. The multi-institutional Project CETI (Cetacean
Translation Initiative) hopes to decipher the
communication of sperm whales through the efforts of
ML specialists, marine biologists, roboticists, linguists,
and cryptographers. The project will involve recording
whale sounds and movements via underwater
microphones, robotic fish, and acoustic tags. Other
projects aim to build technologies that enable human-
animal communication, with Hunter College's Diana
Reiss envisioning "a Google Translate for animals.”
www.nytimes.com/2022/08/30/science/translators-animals-naked-mole-rats.html
Review
▪ Memory is byte-addressable, but lw
and sw access one word at a time.
▪ A pointer (used by lw and sw) is just
a memory address, we can add to it
or subtract from it (using offset).
▪ Big- vs Little Endian
🢭 Tip: draw lowest byte on the right
▪ New Instructions:
lw, sw, lb, sb, lbu
Garcia, Yan
▪ Add immediate
addi rd, rs1, imm
▪ Load/store
lw rd, rs1, imm
lb rd, rs1, imm
lbu rd, rs1, imm
sw rs1, rs2, imm
sb rs1, rs2, imm
Garcia, Yan
if (i == j) bne x13,x14,Exit
f = g + h; add x10,x11,x12
Exit:
▪ May need to negate branch condition
Garcia, Yan
9
RISC-V Decision Making Logical Ops (9)
Example if-else Statement
▪ Assuming translations below, compile
f → x10 g → x11 h → x12
i → x13 j → x14
if (i == j) bne x13,x14,Else
f = g + h; add x10,x11,x12
else j Exit
f = g – h; Else:sub x10,x11,x12
Exit:
Garcia, Yan
10
RISC-V Decision Making Logical Ops (10)
Magnitude Compares in RISC-V
▪ General programs need to test < and > as well.
▪ RISC-V magnitude-compare branches:
“Branch on Less Than”
Syntax: blt reg1,reg2, Label
Meaning: if (reg1 < reg2) goto
Label;
“Branch on Less Than Unsigned”
Syntax: bltu reg1,reg2, Label
Meaning: if (reg1 < reg2“Branch on Less Than”
Syntax: blt reg1,reg2, Label
Meaning: if (reg1 < reg2) goto
Label;
)// treat registers Garcia, Yan
11
as unsigned integers
RISC-V Decision Making Logical Ops (11) goto
Magnitude Compares in RISC-V
label;
Also “Branch on Greater or Equal” bge and bgeu
Note: No ‘bgt’ or ‘ble’ instructions
Garcia, Yan
12
RISC-V Decision Making Logical Ops (12)
Loops in C/Assembly
▪ There are three types of loops in C:
🢭 while
🢭 do … while
🢭 for
▪ Each can be rewritten as either of the
other two, so the same branching method
can be applied to these loops as well.
▪ Key concept: Though there are multiple
ways of writing a loop in RISC-V, the key
to decision-making is conditional branch
Garcia, Yan
Garcia, Yan
Garcia, Yan
x y XOR(x,y
)
0 0 0
0 1 1
1 0 1
1 1 0
Garcia, Yan
Garcia, Yan
Memory
Program
Bytes
One RISC-V Instruction = 32 bits
Data
Garcia, Yan
Garcia, Yan
Garcia, Yan
Garcia, Yan