Skip to content

Commit

Permalink
feat: add ex 5-2 ~ 5-9
Browse files Browse the repository at this point in the history
  • Loading branch information
ludics committed Sep 5, 2023
1 parent 2ed54b3 commit 7c81252
Show file tree
Hide file tree
Showing 26 changed files with 665 additions and 0 deletions.
48 changes: 48 additions & 0 deletions code/exercises/ex_5_2/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
EXEC = test

SRC = ${EXEC}.s

GDBINIT = ../gdbinit

CROSS_COMPILE = riscv64-unknown-elf-
CFLAGS = -nostdlib -fno-builtin -march=rv32ima -mabi=ilp32 -g -Wall

QEMU = qemu-system-riscv32
QFLAGS = -nographic -smp 1 -machine virt -bios none

GDB = gdb-multiarch
CC = ${CROSS_COMPILE}gcc
OBJCOPY = ${CROSS_COMPILE}objcopy
OBJDUMP = ${CROSS_COMPILE}objdump

.DEFAULT_GOAL := all
all:
@${CC} ${CFLAGS} ${SRC} -Ttext=0x80000000 -o ${EXEC}.elf
@${OBJCOPY} -O binary ${EXEC}.elf ${EXEC}.bin

.PHONY : run
run: all
@echo "Press Ctrl-A and then X to exit QEMU"
@echo "------------------------------------"
@echo "No output, please run 'make debug' to see details"
@${QEMU} ${QFLAGS} -kernel ./${EXEC}.elf

.PHONY : debug
debug: all
@echo "Press Ctrl-C and then input 'quit' to exit GDB and QEMU"
@echo "-------------------------------------------------------"
@${QEMU} ${QFLAGS} -kernel ${EXEC}.elf -s -S &
@${GDB} ${EXEC}.elf -q -x ${GDBINIT}

.PHONY : code
code: all
@${OBJDUMP} -S ${EXEC}.elf | less

.PHONY : hex
hex: all
@hexdump -C ${EXEC}.bin

.PHONY : clean
clean:
rm -rf *.o *.bin *.elf

10 changes: 10 additions & 0 deletions code/exercises/ex_5_2/test.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.text
.global _start
_start:
li a0, 1
li a1, 2
li a2, 3
add a3, a0, a1
sub a4, a3, a2
j _start
.end # End of file
48 changes: 48 additions & 0 deletions code/exercises/ex_5_3/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
EXEC = test

SRC = ${EXEC}.s

GDBINIT = ./gdbinit

CROSS_COMPILE = riscv64-unknown-elf-
CFLAGS = -nostdlib -fno-builtin -march=rv32ima -mabi=ilp32 -g -Wall

QEMU = qemu-system-riscv32
QFLAGS = -nographic -smp 1 -machine virt -bios none

GDB = gdb-multiarch
CC = ${CROSS_COMPILE}gcc
OBJCOPY = ${CROSS_COMPILE}objcopy
OBJDUMP = ${CROSS_COMPILE}objdump

.DEFAULT_GOAL := all
all:
@${CC} ${CFLAGS} ${SRC} -Ttext=0x80000000 -o ${EXEC}.elf
@${OBJCOPY} -O binary ${EXEC}.elf ${EXEC}.bin

.PHONY : run
run: all
@echo "Press Ctrl-A and then X to exit QEMU"
@echo "------------------------------------"
@echo "No output, please run 'make debug' to see details"
@${QEMU} ${QFLAGS} -kernel ./${EXEC}.elf

.PHONY : debug
debug: all
@echo "Press Ctrl-C and then input 'quit' to exit GDB and QEMU"
@echo "-------------------------------------------------------"
@${QEMU} ${QFLAGS} -kernel ${EXEC}.elf -s -S &
@${GDB} ${EXEC}.elf -q -x ${GDBINIT}

.PHONY : code
code: all
@${OBJDUMP} -S ${EXEC}.elf | less

.PHONY : hex
hex: all
@hexdump -C ${EXEC}.bin

.PHONY : clean
clean:
rm -rf *.o *.bin *.elf

12 changes: 12 additions & 0 deletions code/exercises/ex_5_3/gdbinit
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
display/z $a0
display/z $a1
display/z $a2
display/z $a3
display/z $a4
display/z $a5
display/z $a6

set disassemble-next-line on
b _start
target remote : 1234
c
12 changes: 12 additions & 0 deletions code/exercises/ex_5_3/test.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.text
.global _start
_start:
li a0, 1
li a1, 2
li a2, 3
li a3, 4
add a4, a0, a1
add a5, a2, a3
sub a6, a4, a5
j _start
.end # End of file
48 changes: 48 additions & 0 deletions code/exercises/ex_5_4/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
EXEC = test

SRC = ${EXEC}.s

GDBINIT = ./gdbinit

CROSS_COMPILE = riscv64-unknown-elf-
CFLAGS = -nostdlib -fno-builtin -march=rv32ima -mabi=ilp32 -g -Wall

QEMU = qemu-system-riscv32
QFLAGS = -nographic -smp 1 -machine virt -bios none

GDB = gdb-multiarch
CC = ${CROSS_COMPILE}gcc
OBJCOPY = ${CROSS_COMPILE}objcopy
OBJDUMP = ${CROSS_COMPILE}objdump

.DEFAULT_GOAL := all
all:
@${CC} ${CFLAGS} ${SRC} -Ttext=0x80000000 -o ${EXEC}.elf
@${OBJCOPY} -O binary ${EXEC}.elf ${EXEC}.bin

.PHONY : run
run: all
@echo "Press Ctrl-A and then X to exit QEMU"
@echo "------------------------------------"
@echo "No output, please run 'make debug' to see details"
@${QEMU} ${QFLAGS} -kernel ./${EXEC}.elf

.PHONY : debug
debug: all
@echo "Press Ctrl-C and then input 'quit' to exit GDB and QEMU"
@echo "-------------------------------------------------------"
@${QEMU} ${QFLAGS} -kernel ${EXEC}.elf -s -S &
@${GDB} ${EXEC}.elf -q -x ${GDBINIT}

.PHONY : code
code: all
@${OBJDUMP} -S ${EXEC}.elf | less

.PHONY : hex
hex: all
@hexdump -C ${EXEC}.bin

.PHONY : clean
clean:
rm -rf *.o *.bin *.elf

8 changes: 8 additions & 0 deletions code/exercises/ex_5_4/gdbinit
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
display/z $a0
display/z $a1
display/z $a2

set disassemble-next-line on
b _start
target remote : 1234
c
10 changes: 10 additions & 0 deletions code/exercises/ex_5_4/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <stdio.h>

int main()
{
unsigned int a = 0x87654321;
unsigned int low = a & 0xffff;
unsigned int high = a >> 16;
printf("low: %x, high: %x\n", low, high);
return 0;
}
9 changes: 9 additions & 0 deletions code/exercises/ex_5_4/test.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.text
.global _start
_start:
li a0, 0x87654321
slli a1, a0, 16
srli a1, a1, 16
srli a2, a0, 16
j _start
.end # End of file
48 changes: 48 additions & 0 deletions code/exercises/ex_5_5/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
EXEC = test

SRC = ${EXEC}.s

GDBINIT = ./gdbinit

CROSS_COMPILE = riscv64-unknown-elf-
CFLAGS = -nostdlib -fno-builtin -march=rv32ima -mabi=ilp32 -g -Wall

QEMU = qemu-system-riscv32
QFLAGS = -nographic -smp 1 -machine virt -bios none

GDB = gdb-multiarch
CC = ${CROSS_COMPILE}gcc
OBJCOPY = ${CROSS_COMPILE}objcopy
OBJDUMP = ${CROSS_COMPILE}objdump

.DEFAULT_GOAL := all
all:
@${CC} ${CFLAGS} ${SRC} -Ttext=0x80000000 -o ${EXEC}.elf
@${OBJCOPY} -O binary ${EXEC}.elf ${EXEC}.bin

.PHONY : run
run: all
@echo "Press Ctrl-A and then X to exit QEMU"
@echo "------------------------------------"
@echo "No output, please run 'make debug' to see details"
@${QEMU} ${QFLAGS} -kernel ./${EXEC}.elf

.PHONY : debug
debug: all
@echo "Press Ctrl-C and then input 'quit' to exit GDB and QEMU"
@echo "-------------------------------------------------------"
@${QEMU} ${QFLAGS} -kernel ${EXEC}.elf -s -S &
@${GDB} ${EXEC}.elf -q -x ${GDBINIT}

.PHONY : code
code: all
@${OBJDUMP} -S ${EXEC}.elf | less

.PHONY : hex
hex: all
@hexdump -C ${EXEC}.bin

.PHONY : clean
clean:
rm -rf *.o *.bin *.elf

8 changes: 8 additions & 0 deletions code/exercises/ex_5_5/gdbinit
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
display/z $a0
display/z $a1
display/z $t0

set disassemble-next-line on
b _start
target remote : 1234
c
11 changes: 11 additions & 0 deletions code/exercises/ex_5_5/test.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.data
array: .word 0x11111111, 0xffffffff

.text
.global _start
_start:
la t0, array # load address of array into t0
lw a0, 0(t0) # load first element of array into a0 (i)
lw a1, 4(t0) # load second element of array into a1 (j)
j _start # Jump to _start
.end # End of file
48 changes: 48 additions & 0 deletions code/exercises/ex_5_6/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
EXEC = test

SRC = ${EXEC}.s

GDBINIT = ./gdbinit

CROSS_COMPILE = riscv64-unknown-elf-
CFLAGS = -nostdlib -fno-builtin -march=rv32ima -mabi=ilp32 -g -Wall

QEMU = qemu-system-riscv32
QFLAGS = -nographic -smp 1 -machine virt -bios none

GDB = gdb-multiarch
CC = ${CROSS_COMPILE}gcc
OBJCOPY = ${CROSS_COMPILE}objcopy
OBJDUMP = ${CROSS_COMPILE}objdump

.DEFAULT_GOAL := all
all:
@${CC} ${CFLAGS} ${SRC} -Ttext=0x80000000 -o ${EXEC}.elf
@${OBJCOPY} -O binary ${EXEC}.elf ${EXEC}.bin

.PHONY : run
run: all
@echo "Press Ctrl-A and then X to exit QEMU"
@echo "------------------------------------"
@echo "No output, please run 'make debug' to see details"
@${QEMU} ${QFLAGS} -kernel ./${EXEC}.elf

.PHONY : debug
debug: all
@echo "Press Ctrl-C and then input 'quit' to exit GDB and QEMU"
@echo "-------------------------------------------------------"
@${QEMU} ${QFLAGS} -kernel ${EXEC}.elf -s -S &
@${GDB} ${EXEC}.elf -q -x ${GDBINIT}

.PHONY : code
code: all
@${OBJDUMP} -S ${EXEC}.elf | less

.PHONY : hex
hex: all
@hexdump -C ${EXEC}.bin

.PHONY : clean
clean:
rm -rf *.o *.bin *.elf

8 changes: 8 additions & 0 deletions code/exercises/ex_5_6/gdbinit
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
display/z $a0
display/z $a1
display/z $t0

set disassemble-next-line on
b _start
target remote : 1234
c
26 changes: 26 additions & 0 deletions code/exercises/ex_5_6/test.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.data
s: .word 0, 0

.macro set_struct st
la t0, \st
sw a0, 0(t0)
sw a1, 4(t0)
.endm

.macro get_struct st
la t0, \st
lw a0, 0(t0)
lw a1, 4(t0)
.endm

.text
.global _start
_start:
li a0, 0x12345678 # a = 0x12345678
li a1, 0x87654321 # b = 0x87654321
set_struct s
li a0, 0
li a1, 0
get_struct s
j _start
.end

0 comments on commit 7c81252

Please sign in to comment.