Compare commits
No commits in common. "7c0e11359348b99c5c05351effb41198a9fd01db" and "9ff42d8fbc5f2837c5e48c0d67c707fd11a52970" have entirely different histories.
7c0e113593
...
9ff42d8fbc
6 changed files with 31 additions and 130 deletions
12
README.md
12
README.md
|
@ -1,12 +0,0 @@
|
||||||
# Risc-V Assembly Language Programming
|
|
||||||
|
|
||||||
## Resources
|
|
||||||
|
|
||||||
- [RISC-V Guru](https://risc-v.guru/) and [direct link to the instruction set](https://risc-v.guru/instructions/)
|
|
||||||
- [RISC-V Greehsheet](https://raw.githubusercontent.com/rswinkle/riscv_book/master/references/riscv_greensheet.pdf) and its [large version](https://raw.githubusercontent.com/rswinkle/riscv_book/master/references/riscv_greensheet_large.pdf)
|
|
||||||
- [RISC-V ISA Manual](https://riscv.org/wp-content/uploads/2017/05/riscv-spec-v2.2.pdf)
|
|
||||||
- [RISC-V Specifications](https://riscv.org/technical/specifications/)
|
|
||||||
|
|
||||||
### Misc
|
|
||||||
|
|
||||||
- [RISC-V Assembly Language Programming](https://github.com/rswinkle/riscv_book/)
|
|
19
end.s
19
end.s
|
@ -1,19 +0,0 @@
|
||||||
# Just print the word 'exit' to the address 0x10000000
|
|
||||||
.globl end
|
|
||||||
end:
|
|
||||||
addi a0, x0, 0x65 # 'e'
|
|
||||||
li a1, 0x10000000
|
|
||||||
sb a0, (a1)
|
|
||||||
|
|
||||||
addi a0, x0, 0x78 # 'x'
|
|
||||||
sb a0, (a1)
|
|
||||||
|
|
||||||
addi a0, x0, 0x69 # 'i'
|
|
||||||
sb a0, (a1)
|
|
||||||
|
|
||||||
addi a0, x0, 0x74 # 't'
|
|
||||||
sb a0, (a1)
|
|
||||||
|
|
||||||
addi a0, x0, 0x0A
|
|
||||||
sb a0, (a1) # '\n'
|
|
||||||
ret
|
|
16
hello.s
Normal file
16
hello.s
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
.section .text
|
||||||
|
.globl _hello
|
||||||
|
|
||||||
|
_hello:
|
||||||
|
# Write the string "Hello, World!\n" to stdout
|
||||||
|
la a0, msg # Load the address of the string into a0
|
||||||
|
li a7, 4 # syscall number for write
|
||||||
|
li a1, 13 # Length of the string
|
||||||
|
li a2, 1 # File descriptor: stdout
|
||||||
|
ecall # Make the system call to write
|
||||||
|
|
||||||
|
ret # Return from the function
|
||||||
|
|
||||||
|
.section .data
|
||||||
|
msg:
|
||||||
|
.ascii "Hello, World!\n"
|
17
link.ld
17
link.ld
|
@ -1,17 +0,0 @@
|
||||||
MEMORY {
|
|
||||||
SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 1024
|
|
||||||
DRAM (rwx) : ORIGIN = 0x80000000, LENGTH = 1024
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTIONS {
|
|
||||||
.text : {
|
|
||||||
main.o(.text.*)
|
|
||||||
*(.text)
|
|
||||||
} > DRAM
|
|
||||||
.data : {
|
|
||||||
*(.data)
|
|
||||||
} > DRAM AT > SRAM
|
|
||||||
.bss : {
|
|
||||||
*(.bss)
|
|
||||||
} > DRAM
|
|
||||||
}
|
|
77
main.s
77
main.s
|
@ -1,69 +1,12 @@
|
||||||
# Define variables
|
.section .text
|
||||||
.data
|
.globl _start
|
||||||
.align 4
|
|
||||||
num1: .word 5 # First number
|
|
||||||
num2: .word 7 # Second number
|
|
||||||
result: .word 0 # Variable to store the result
|
|
||||||
msg: .ascii "Hello, World!\n"
|
|
||||||
array: .space 100 # 100 bytes of space
|
|
||||||
|
|
||||||
# Main program entry point
|
_start:
|
||||||
.globl start
|
# Call the hello function
|
||||||
.text
|
lui a0, %hi(_hello) # Load upper immediate of _hello address
|
||||||
start:
|
addi a0, a0, %lo(_hello) # Add lower immediate of _hello address
|
||||||
call test_routine
|
jalr ra, a0, 0 # Jump and link to _hello, ra is return address
|
||||||
call hello_manual
|
|
||||||
# call hello # This doesn't work
|
|
||||||
call end
|
|
||||||
ebreak
|
|
||||||
|
|
||||||
# Arbitrary test routine
|
# Exit the program
|
||||||
test_routine:
|
li a7, 10 # syscall number for exit
|
||||||
la a0, num1 # Load the address of num1 into register a6
|
ecall # Make the system call to exit
|
||||||
li a1, 10 # Load the value 10 into register a5
|
|
||||||
xori a2, a1, 0x1 # Calculate the value of a2
|
|
||||||
sw a1, 0(a0) # Store the value in memory
|
|
||||||
ret
|
|
||||||
|
|
||||||
# Prints "hello" manually
|
|
||||||
hello_manual:
|
|
||||||
addi a0, x0, 0x68
|
|
||||||
li a1, 0x10000000
|
|
||||||
sb a0, (a1) # 'h'
|
|
||||||
|
|
||||||
addi a0, x0, 0x65
|
|
||||||
sb a0, (a1) # 'e'
|
|
||||||
|
|
||||||
addi a0, x0, 0x6C
|
|
||||||
sb a0, (a1) # 'l'
|
|
||||||
|
|
||||||
addi a0, x0, 0x6C
|
|
||||||
sb a0, (a1) # 'l'
|
|
||||||
|
|
||||||
addi a0, x0, 0x6F
|
|
||||||
sb a0, (a1) # 'o'
|
|
||||||
|
|
||||||
addi a0, x0, 0x0A
|
|
||||||
sb a0, (a1) # '\n'
|
|
||||||
ret
|
|
||||||
|
|
||||||
# Is supposed to print "Hello, World!\n" to stdout via ecall, but doesn't work
|
|
||||||
hello:
|
|
||||||
# Write the string "Hello, World!\n" to stdout
|
|
||||||
la a0, msg # Load the address of the string into a0
|
|
||||||
li a7, 4 # syscall number for write
|
|
||||||
li a1, 13 # Length of the string
|
|
||||||
li a2, 1 # File descriptor: stdout
|
|
||||||
ecall # Make the system call to write
|
|
||||||
ret # Return from the function
|
|
||||||
|
|
||||||
# Another routine, presumably written for the rars emulator?
|
|
||||||
hello2:
|
|
||||||
li a0, 0x1
|
|
||||||
la a1, msg
|
|
||||||
li a2, 13
|
|
||||||
li a7, 64
|
|
||||||
ecall
|
|
||||||
|
|
||||||
# To avoid the program from exiting
|
|
||||||
loop: j loop
|
|
||||||
|
|
20
makefile
20
makefile
|
@ -10,10 +10,8 @@ SIZE = ${CROSS}-size
|
||||||
TARGET = bin.elf
|
TARGET = bin.elf
|
||||||
|
|
||||||
# QEMU command and flags
|
# QEMU command and flags
|
||||||
# QEMU = qemu-riscv64-static
|
QEMU = qemu-riscv64-static
|
||||||
QEMU = qemu-system-riscv64
|
QEMU_FLAGS =
|
||||||
QEMU_FLAGS += --machine virt# # Use the virt machine
|
|
||||||
QEMU_FLAGS += --nographic# # No graphical output
|
|
||||||
|
|
||||||
# Flags for compiler and assembler
|
# Flags for compiler and assembler
|
||||||
CFLAGS += -static# # Use static linking
|
CFLAGS += -static# # Use static linking
|
||||||
|
@ -25,13 +23,6 @@ CFLAGS += -march=rv64i # Use RV64I ISA, i.e., integer only
|
||||||
CFLAGS += -mabi=lp64 # Use LP64 ABI, i.e., 64-bit longs and pointers, 32-bit ints
|
CFLAGS += -mabi=lp64 # Use LP64 ABI, i.e., 64-bit longs and pointers, 32-bit ints
|
||||||
CFLAGS += -Os# # Optimize for size
|
CFLAGS += -Os# # Optimize for size
|
||||||
|
|
||||||
LDFLAGS += -T link.ld # Use the linker script
|
|
||||||
LDFLAGS += --no-dynamic-linker
|
|
||||||
LDFLAGS += -m elf64lriscv
|
|
||||||
LDFLAGS += -static
|
|
||||||
LDFLAGS += -nostdlib
|
|
||||||
LDFLAGS += -s
|
|
||||||
|
|
||||||
# Use GC=0 to disable garbage collection
|
# Use GC=0 to disable garbage collection
|
||||||
ifneq ($(GC), 0)
|
ifneq ($(GC), 0)
|
||||||
CFLAGS += -ffreestanding
|
CFLAGS += -ffreestanding
|
||||||
|
@ -52,7 +43,7 @@ OBJS := $(AS_SRCS:.s=.o)
|
||||||
|
|
||||||
# Default target
|
# Default target
|
||||||
all: $(OBJS)
|
all: $(OBJS)
|
||||||
@$(LD) $(LDFLAGS) -o $(TARGET) $(OBJS)
|
@$(CC) $(CFLAGS) -o $(TARGET) $(OBJS)
|
||||||
@echo "LD $(OBJS)"
|
@echo "LD $(OBJS)"
|
||||||
|
|
||||||
# Compile assembly sources
|
# Compile assembly sources
|
||||||
|
@ -61,9 +52,8 @@ all: $(OBJS)
|
||||||
@echo "CC $<"
|
@echo "CC $<"
|
||||||
|
|
||||||
# Run the binary in QEMU
|
# Run the binary in QEMU
|
||||||
run: all
|
run: all
|
||||||
@echo "To exit: Ctrl+A, X"
|
$(QEMU) $(QEMU_FLAGS) ./$(TARGET)
|
||||||
@$(QEMU) $(QEMU_FLAGS) -bios $(TARGET)
|
|
||||||
|
|
||||||
# View the text section of the binary
|
# View the text section of the binary
|
||||||
inspect: all
|
inspect: all
|
||||||
|
|
Loading…
Add table
Reference in a new issue