Skip to content

Commit 2592657

Browse files
tofergreggCS107E BOT
authored andcommitted
released lecture 2
commit 9d515861bb0c98d6fbdc74755a166f1b1a9a7c7c Author: Chris Gregg <[email protected]> Date: Fri Jan 10 00:10:23 2025 -0800 released lecture 2
1 parent b129c10 commit 2592657

File tree

9 files changed

+86
-1
lines changed

9 files changed

+86
-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/projectlab1/","Lab 8: Project Team Meeting 1",false
1818
"/labs/projectlab2/","Lab 9: Project Team Meeting 2",false
1919
"/lectures/Arithmetic/","Number Representation and Arithmetic",false
20-
"/lectures/Assembly/","RISC-V assembly and machine code",false
2120
"/lectures/Ben/","Magic of Computer Systems",false
2221
"/lectures/C_Control/","From Assembly to C",false
2322
"/lectures/C_Functions/","C Functions",false

lectures/Assembly/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
released: true
3+
permalink: /lectures/Assembly/
4+
title: RISC-V assembly and machine code
5+
readings: |
6+
+ Here is a link to the [Ripes simulator](https://ripes.me/) demoed in lecture.
7+
+ Our [one-page guide to RISC-V](/guides/riscv-onepage).
8+
+ Enjoy Danny Cohen's article
9+
[Holy Wars and a Plea for Peace](/readings/holywars.pdf) on the history of little-endian vs. big-endian.
10+
---
11+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Demo of cross-development toolchain
2+
3+
Assembly a one instruction program:
4+
5+
add x3,x1,x2
6+
7+
8+
# assemble add.s to make an 'object file' add.o
9+
riscv64-unknown-elf-as add.s -o add.o
10+
11+
# number of bytes in add.o
12+
ls -l add.o
13+
14+
# convert the object file to a binary file
15+
riscv64-unknown-elf-objcopy add.o add.bin -O binary
16+
17+
# number of bytes in add.bin
18+
ls -l add.bin
19+
20+
# "size" of add.o
21+
riscv64-unknown-elf-size add.o
22+
23+
# print a hexdump of the binary
24+
hexdump -C add.bin

lectures/Assembly/code/add/add.s

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add x3,x1,x2
2+
addi a0,zero,21
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
run:
2+
riscv64-unknown-elf-as blink.s -o blink.o
3+
riscv64-unknown-elf-objcopy blink.o -O binary blink.bin
4+
mango-run blink.bin
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# File: blink.s
2+
# -------------
3+
# blink LED connected to GPIO PB0
4+
5+
lui a0,0x2000 # a0 holds base addr PB group = 0x2000000
6+
addi a1,zero,1 # a1 holds constant 1
7+
sw a1,0x30(a0) # config PB0 as output
8+
9+
loop:
10+
xori a1,a1,1 # xor ^ 1 invert a1
11+
sw a1,0x40(a0) # set data value of PB0 to a1
12+
13+
lui a2,0x3f00 # a2 = init countdown value
14+
delay: # busy loop to wait for a while
15+
addi a2,a2,-1 # decrement a2
16+
bne a2,zero,delay # keep counting down until a2 is zero
17+
18+
j loop # back to top of outer loop

lectures/Assembly/code/on/doit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
riscv64-unknown-elf-as on.s -o on.o
2+
riscv64-unknown-elf-objcopy on.o -O binary on.bin

lectures/Assembly/code/on/on.s

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#false File on.s
2+
# ---------
3+
# turn on LED connected to GPIO PB0
4+
5+
# configure PB0 for output
6+
#
7+
# Base address of GPIO peripheral is 0x02000000
8+
# PB0 configured by config0 register of PB group
9+
# PB config0 register is at offset 0x30 from GPIO base
10+
11+
lui a0,0x2000 # GPIO base address in a0
12+
addi a1,zero,1 # constant 1 for output in a1
13+
sw a1,0x30(a0) # config PB0 as output
14+
15+
# set PB0 value to 1
16+
#
17+
# PB0 value is bit 0 in PB data register
18+
# PB data register is at offset 0x40 from GPIO base
19+
20+
# a0 set to gpio base above, a1 has value 1
21+
sw a1,0x40(a0) # turn on PB0
22+
23+
# infinite loop
24+
loop:
25+
j loop

lectures/Assembly/slides.pdf

4.33 MB
Binary file not shown.

0 commit comments

Comments
 (0)