CS61C: Great Ideas in Computer Architecture (aka Machine Structures)
Lecture 10: RISC-V Part 3: Control Flow, Bitwise Ops
Instructors: Lisa Yan, Justin Yokota
#
CS 61C
Spring 2024
Agenda
2
CS 61C
Spring 2024
Agenda
3
CS 61C
Spring 2024
RISC-V Guiding Philosophy
4
CS 61C
Spring 2024
Control Flow in C
int x = 5;�int y = 10;�int z = x+y;
5
Current Line
Next Line
CS 61C
Spring 2024
Control Flow in C
Lines in C that affect program flow:
if(cond) {� line;�}
line;
6
Current Line
Next Line if cond
Next Line if !cond
CS 61C
Spring 2024
Control Flow in C
Lines in C that affect program flow:
if(cond) {� line;�}�else {� line;�}�line;
7
Current Line
Next Line if cond
Next Line if !cond
CS 61C
Spring 2024
Control Flow in C
Lines in C that affect program flow:
while(cond) {� line;� line;�}
8
Current Line
Next Line
CS 61C
Spring 2024
Control Flow in C
Lines in C that affect program flow:
do {� line;� line;�} while(cond);�line;
9
Current Line
Next Line if cond
Next Line if !cond
CS 61C
Spring 2024
Control Flow in C
Lines in C that affect program flow:
for(line;cond;line) {� line;� line;�}�line;
10
Current Line (cond)
Next Line if !cond
Next Line if cond
CS 61C
Spring 2024
Control Flow in C
Lines in C that affect program flow:
while(true) {� line;� break;�}�line;
11
Current Line
Next Line
CS 61C
Spring 2024
Control Flow in C
Lines in C that affect program flow:
int foo(n) {� int a = 5;� return a+n;�}�...�line;�foo(5);�line;
12
Current Line
Next Line
CS 61C
Spring 2024
Control Flow in C
Lines in C that affect program flow:
int foo(n) {� int a = 5;� return a+n;�}�...�line;�foo(5);�line;�foo(5);�line;
13
Current Line
Next Line?
Next Line?
CS 61C
Spring 2024
New C operator: goto and Labels
A label is an identifier to a particular line of code
The goto statement changes the next line to be run to the labelled line
Target: line;� line;� line;� goto Target;� line;
14
Current Line
Next Line
CS 61C
Spring 2024
goto Example: Handling Mallocs
int* a = malloc(sizeof(int)*1000);
int* b = malloc(sizeof(int)*1000000);
int* c = malloc(sizeof(int)*1000000000);
FILE* d = fopen(filename);
15
CS 61C
Spring 2024
goto Example: Handling Mallocs
int* a = malloc(sizeof(int)*1000);
int* b = malloc(sizeof(int)*1000000);
int* c = malloc(sizeof(int)*1000000000);
FILE* d = fopen(filename);
16
Bad code: malloc can fail (returning NULL), and we should catch that before it causes a segfault
CS 61C
Spring 2024
goto Example: Handling Mallocs
int* a = malloc(sizeof(int)*1000);
if(a == NULL) allocation_failed();
int* b = malloc(sizeof(int)*1000000);
if(b == NULL) allocation_failed();
int* c = malloc(sizeof(int)*1000000000);
if(c == NULL) allocation_failed();
FILE* d = fopen(filename);
if(d == NULL) allocation_failed();
17
CS 61C
Spring 2024
goto Example: Handling Mallocs
int* a = malloc(sizeof(int)*1000);
if(a == NULL) allocation_failed();
int* b = malloc(sizeof(int)*1000000);
if(b == NULL) allocation_failed();
int* c = malloc(sizeof(int)*1000000000);
if(c == NULL) allocation_failed();
FILE* d = fopen(filename);
if(d == NULL) allocation_failed();
18
Bad code: leaks memory since a gets allocated but never freed.
CS 61C
Spring 2024
goto Example: Handling Mallocs
int* a = malloc(sizeof(int)*1000);
if(a == NULL) allocation_failed();
int* b = malloc(sizeof(int)*1000000);
if(b == NULL) {
free(a);
allocation_failed();
}
int* c = malloc(sizeof(int)*1000000000);
if(c == NULL) {
free(b);
free(a);
allocation_failed();
}
FILE* d = fopen(filename);
if(d == NULL) {
free(c);
free(b);
free(a);
allocation_failed();
}
19
CS 61C
Spring 2024
goto Example: Handling Mallocs
int* a = malloc(sizeof(int)*1000);
if(a == NULL) goto ErrorA;
int* b = malloc(sizeof(int)*1000000);
if(b == NULL) goto ErrorB;
int* c = malloc(sizeof(int)*1000000000);
if(c == NULL) goto ErrorC;
FILE* d = fopen(filename);
if(d == NULL) {
free(c);
ErrorC: free(b);
ErrorB: free(a);
ErrorA: allocation_failed();
}
20
CS 61C
Spring 2024
NEVER USE goto!!!!
21
CS 61C
Spring 2024
Agenda
22
CS 61C
Spring 2024
Reducing C with goto: Break
while(true) {� line;� break;�}�line;
23
CS 61C
Spring 2024
Reducing C with goto: Break
while(true) {� line;� goto AfterWhile;�}�AfterWhile: line;
24
CS 61C
Spring 2024
Reducing C with goto: If
if(cond) {
line;
line;
} else {
line;
line;
}
line;
25
CS 61C
Spring 2024
Reducing C with goto: If
if(cond) goto IfCase;
goto ElseCase;
IfCase:
line;
line;
goto AfterIf;
ElseCase:
line;
line;
AfterIf: line;
26
CS 61C
Spring 2024
Reducing C with goto: If without an Else
if(!cond) goto AfterIf;
line;
line;
AfterIf: line;
27
CS 61C
Spring 2024
Reducing C with goto: Do-While
do {
line;
line;
} while(cond)
line;
28
CS 61C
Spring 2024
Reducing C with goto: Do-While
Loop: line;
line;
if(cond) goto Loop;
line;
29
CS 61C
Spring 2024
Reducing C with goto: While
while(cond) {
line;
line;
}
line;
30
CS 61C
Spring 2024
Reducing C with goto: While
Loop: if(!cond) goto AfterLoop;
line;
line;
goto Loop;
AfterLoop: line;
31
CS 61C
Spring 2024
Reducing C with goto: For
for(startline;cond;incline) {
line;
line;
}
line;
32
CS 61C
Spring 2024
Reducing C with goto: For
startline;
while(cond) {
line;
line;
incline;
}
line;
33
CS 61C
Spring 2024
Reducing C with goto: For
startline;
Loop: if(cond) goto AfterLoop
line;
line;
incline;
goto Loop
AfterLoop: line;
34
CS 61C
Spring 2024
Agenda
35
CS 61C
Spring 2024
RISC-V Control Flow Operations
36
CS 61C
Spring 2024
RISC-V Control Flow Operations
List of branch instructions:
37
CS 61C
Spring 2024
RISC-V Control Flow Operations: Example
int a = 0;
for(int i = 0; i < 10; i++) {
if(i == 7) {
break;
}
a = a + i;
}
a = a + 50;
38
CS 61C
Spring 2024
RISC-V Control Flow Operations: Example
int a = 0;
for(int i = 0; i < 10; i++) {
if(i == 7) goto End;
a = a + i;
}
End: a = a + 50;
39
CS 61C
Spring 2024
RISC-V Control Flow Operations: Example
int a = 0;
int i = 0;
Loop: if(i >= 10) goto End;
if(i == 7) goto End;
a = a + i;
i = i + 1;
goto Loop;
End: a = a + 50;
40
CS 61C
Spring 2024
RISC-V Control Flow Operations: Example
int a = 0;
int i = 0;
Loop:
int j = 10;
if(i >= j) goto End;
j = 7;
if(i == j) goto End;
a = a + i;
i = i + 1;
goto Loop;
End: a = a + 50;
41
CS 61C
Spring 2024
RISC-V Control Flow Operations: Example
li x10 0 #int a = 0;
li x5 0 #int i = 0;
Loop:
li x6 10 #int j = 10;
bge x5 x6 End #if(i >= j) goto End;
li x6 7 #j = 7;
beq x5 x6 End #if(i == j) goto End;
add x10 x10 x5 #a = a + i;
addi x5 x5 1 #i = i + 1;
j Loop #goto Loop;
End: addi x10 x10 50 #a = a + 50;
42
CS 61C
Spring 2024
Agenda
43
CS 61C
Spring 2024
Review: Bitwise Operations in C
44
CS 61C
Spring 2024
Review: Bitwise Operations in C
Shifts have numerical equivalents in math
For left shifts, we always fill the new bits with 0s
For right shifts, dividing an unsigned number should fill the bits with 0s
But right-shifting a signed number shouldn't fill with 0s always
45
CS 61C
Spring 2024
Review: Bitwise Operations in C
46
CS 61C
Spring 2024
Arithmetic Operations in RISC-V
47
CS 61C
Spring 2024
Arithmetic Operations that AREN'T in RISC-V
48
CS 61C
Spring 2024
Practice: Bitwise Operations
What is in register x12 after the following instructions?
li x10 0x34FF
slli x12 x10 0x10
srli x12 x12 0x08
and x12 x12 x10
49
CS 61C
Spring 2024
50
CS 61C
Spring 2024
51
CS 61C
Spring 2024
52
CS 61C
Spring 2024
Practice: Bitwise Operations
What is in register x12 after the following instructions?
li x10 0x34FF
slli x12 x10 0x10
srli x12 x12 0x08
and x12 x12 x10
53
Register | Value |
x10 | 0x0000 0000 |
x12 | 0x0000 0000 |
CS 61C
Spring 2024
Practice: Bitwise Operations
What is in register x12 after the following instructions?
li x10 0x34FF
slli x12 x10 0x10
srli x12 x12 0x08
and x12 x12 x10
54
Register | Value |
x10 | 0x0000 34FF |
x12 | 0x0000 0000 |
CS 61C
Spring 2024
Practice: Bitwise Operations
What is in register x12 after the following instructions?
li x10 0x34FF
slli x12 x10 0x10
srli x12 x12 0x08
and x12 x12 x10
55
Register | Value |
x10 | 0x0000 34FF |
x12 | 0x34FF 0000 |
CS 61C
Spring 2024
Practice: Bitwise Operations
What is in register x12 after the following instructions?
li x10 0x34FF
slli x12 x10 0x10
srli x12 x12 0x08
and x12 x12 x10
56
Register | Value |
x10 | 0x0000 34FF |
x12 | 0x0034 FF00 |
CS 61C
Spring 2024
Practice: Bitwise Operations
What is in register x12 after the following instructions?
li x10 0x34FF
slli x12 x10 0x10
srli x12 x12 0x08
and x12 x12 x10
57
Register | Value |
x10 | 0x0000 34FF |
x12 | 0x0000 3400 |
CS 61C
Spring 2024
Summary: RISC-V so far
58
CS 61C
Spring 2024