Skip to content

Commit 07b5b86

Browse files
zelenskiCS107E BOT
authored andcommitted
release C1 lecture
commit 0b17b38a5ba9e69472695ba2e6715c7659c4cbbb Author: Julie Zelenski <[email protected]> Date: Sun Jan 12 23:16:32 2025 -0800 release C1 lecture
1 parent d34eed3 commit 07b5b86

File tree

10 files changed

+178
-1
lines changed

10 files changed

+178
-1
lines changed

_data/unreleased.csv

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ permalink,title,released
1717
"/labs/projectlab2/","Lab 9: Project Team Meeting 2",false
1818
"/lectures/Arithmetic/","Number Representation and Arithmetic",false
1919
"/lectures/Ben/","Magic of Computer Systems",false
20-
"/lectures/C_Control/","From Assembly to C",false
2120
"/lectures/C_Functions/","C Functions",false
2221
"/lectures/C_Mastery/","C Mastery",false
2322
"/lectures/C_Pointers/","C Pointers and Arrays",false

lectures/C_Control/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
released: true
3+
permalink: /lectures/C_Control/
4+
title: From Assembly to C
5+
readings: |
6+
+ Our [one-page guide to RISC-V](/guides/riscv-onepage).
7+
+ Brush up on C basics (syntax, data types, operators, control structures) via your favorite C reference; sections 1-2 of Nick Parlante's [EssentialC](http://cslibrary.stanford.edu/101/EssentialC.pdf); or chapters 1, 2, and 3 of K&R. Skip content on I/O and standard libraries (we are bare metal!)
8+
+ Read Dennis Ritchie's account of the [history of C](https://www.bell-labs.com/usr/dmr/www/chist.html)
9+
+ Try out the [Compiler Explorer](https://godbolt.org) demoed in lecture
10+
+ Read our guides on [make](/guides/make) and [gcc](/guides/gcc)
11+
---
12+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Some C examples to try out
2+
// in Compiler Explorer https://godbolt.org
3+
4+
int main(void)
5+
{
6+
unsigned int val = 0x3a;
7+
int count = 0;
8+
9+
while (val != 0) {
10+
if (val & 1) count++;
11+
val = val >> 1;
12+
}
13+
return count;
14+
}
15+
16+
int binky(int arg)
17+
{
18+
int result = arg + 13;
19+
result = result * arg;
20+
return result;
21+
}
22+
23+
int winky(int arg)
24+
{
25+
if (arg == 0)
26+
arg++;
27+
else
28+
arg--;
29+
return arg;
30+
}
31+
32+
int pinky(int arg)
33+
{
34+
int result = 1;
35+
for (int i = 0; i < arg; i++)
36+
result *= i;
37+
return result;
38+
}
39+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# This simple Makefile defines pattern rule to build
2+
# name.bin from name.c C source file
3+
4+
ARCH = -march=rv64im -mabi=lp64
5+
CFLAGS = $(ARCH) -g -Og -Wall -ffreestanding
6+
LDFLAGS = $(ARCH) -nostdlib -e main
7+
LDLIBS =
8+
9+
all: dice dice.bin
10+
11+
%.bin: %.elf
12+
riscv64-unknown-elf-objcopy $*.elf -O binary $@
13+
14+
%.elf: %.o
15+
riscv64-unknown-elf-gcc $(LDFLAGS) $^ $(LDLIBS) -o $@
16+
17+
%.o: %.c
18+
riscv64-unknown-elf-gcc $(CFLAGS) -c $< -o $@
19+
20+
%.o: %.s
21+
riscv64-unknown-elf-as $< -o $@
22+
23+
run: dice.bin
24+
mango-run $<
25+
26+
list: dice.o dice
27+
riscv64-unknown-elf-objdump --no-show-raw-insn -d dice.o > dice.list
28+
otool -v -t dice > dice.mac.list
29+
30+
# This recipe is for a native executable on hosted environment
31+
# (not cross-compiled, not freestanding)
32+
%: %.c
33+
clang -g -Wall $< -o $@
34+
35+
clean:
36+
rm -rf *.list *.bin *.elf *.o dice a.out *.dSYM dice
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <time.h>
4+
5+
const int NSIDES = 6;
6+
7+
int main(int argc, char *argv[])
8+
{
9+
int nrolls = 0;
10+
11+
srand(time(0));
12+
while (1) {
13+
nrolls++;
14+
int val = rand() % NSIDES;
15+
if (val == 4) break;
16+
}
17+
printf("It took %d tries to roll a four.\n", nrolls);
18+
return nrolls;
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This very simple Makefile builds blink.bin
2+
# using fixed recipes
3+
4+
blink.bin: blink.s
5+
riscv64-unknown-elf-as blink.s -o blink.o
6+
riscv64-unknown-elf-objcopy blink.o -O binary blink.bin
7+
8+
run: blink.bin
9+
mango-run blink.bin
10+
11+
clean:
12+
rm -f blink.o blink.bin
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# File blink.s
2+
# ------------
3+
# blink LED connected to GPIO PB0
4+
5+
lui a0,0x2000
6+
addi a1,zero,1
7+
sw a1,0x30(a0) # config PB0 as output pin
8+
loop:
9+
xori a1,a1,1 # xor ^ 1 invert bit 0
10+
sw a1,0x40(a0) # update data register will flip on<->off
11+
lui a2,0x3f00
12+
delay: # busy loop to wait for a while
13+
addi a2,a2,-1
14+
bne a2,zero,delay
15+
j loop # repeat forever
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This sample Makefile build a program for the Pi
2+
# from a single C source file
3+
# NAME.c => NAME.o => NAME.elf => NAME.bin
4+
# The recipes use pattern rules that allow you to
5+
# easily change the basename of the file to customize
6+
7+
NAME = count
8+
9+
ARCH = -march=rv64im -mabi=lp64
10+
CFLAGS = $(ARCH) -g -Og -Wall -ffreestanding
11+
LDFLAGS = $(ARCH) -nostdlib -e main
12+
OBJDUMP_FLAGS = --no-show-raw-insn --no-addresses --disassembler-color=terminal --visualize-jumps
13+
14+
all : $(NAME).bin
15+
16+
%.bin: %.elf
17+
riscv64-unknown-elf-objcopy $< -O binary $@
18+
19+
%.elf: %.o
20+
riscv64-unknown-elf-gcc $(LDFLAGS) $< -o $@
21+
22+
%.o: %.c
23+
riscv64-unknown-elf-gcc $(CFLAGS) -c $< -o $@
24+
25+
list: $(NAME).o
26+
riscv64-unknown-elf-objdump $(OBJDUMP_FLAGS) -d $<
27+
28+
clean:
29+
rm -f *.bin *.elf *.o *.list
30+
31+
run: $(NAME).bin
32+
mango-run $<
33+
34+
.PHONY: all clean run list
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Program to count the "on" bits in argument
2+
3+
int main(unsigned arg)
4+
{
5+
int count = 0;
6+
do {
7+
count += arg & 1;
8+
arg >>= 1;
9+
} while (arg);
10+
return count;
11+
}

lectures/C_Control/slides.pdf

1.89 MB
Binary file not shown.

0 commit comments

Comments
 (0)