Skip to content

Commit 5969a17

Browse files
committed
Add comments and loops for hello assembly program
It would be one of the student exercises.
1 parent 839ab04 commit 5969a17

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

tests/asm-hello/hello.S

+31-15
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
1+
# RISC-V assembly program to print "Hello World!" to stdout.
2+
13
.org 0
4+
# Provide program starting address to linker
25
.global _start
36

4-
.data
5-
6-
str: .ascii "Hello World!\n"
7-
.set str_size, .-str
8-
97
/* newlib system calls */
108
.set SYSEXIT, 93
119
.set SYSWRITE, 64
1210

13-
.text
11+
.data
12+
str: .ascii "Hello World!\n"
13+
.set str_size, .-str
1414

15+
.text
1516
_start:
16-
li a7, SYSWRITE
17-
li a0, 1
18-
la a1, str
19-
li a2, str_size
20-
ecall
21-
22-
li a7, SYSEXIT
23-
add a0, x0, 0
24-
ecall
17+
li t0, 0
18+
li t1, 5
19+
20+
# dummy test for jal instruction
21+
.L1:
22+
jal x0, .L2
23+
.L2:
24+
nop
25+
26+
loop:
27+
beq t0, t1, end
28+
29+
li a7, SYSWRITE # "write" syscall
30+
li a0, 1 # 1 = standard output (stdout)
31+
la a1, str # load address of hello string
32+
li a2, str_size # length of hello string
33+
ecall # invoke syscall to print the string
34+
addi t0, t0, 1
35+
j loop
36+
37+
end:
38+
li a7, SYSEXIT # "exit" syscall
39+
add a0, x0, 0 # Use 0 return code
40+
ecall # invoke syscall to terminate the program

tests/asm-hello/hello.ld

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
OUTPUT_ARCH( "riscv" )
1+
OUTPUT_ARCH("riscv")
22
ENTRY(_start)
33

44
SECTIONS

0 commit comments

Comments
 (0)