-
Notifications
You must be signed in to change notification settings - Fork 0
/
intout.s
51 lines (42 loc) · 1.58 KB
/
intout.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#
# Risc-V Assembler program to convert an integer to a string
# and output the result to stdout.
#
# a0-a2 - parameters to linux function services
# a7 - linux function number
#
.data
stack: .space 1024 # Allocate space for stack
newline: .asciz "\n" # Newline character
.section .text
.global _start # Provide program starting address to linker
.align 2
_start:
la sp, stack # set stack pointer
la a4, stack # set address of stack to a4
li a2, 10 # load constant for division
li a0, 147856790 # load integer to print
convert_loop:
rem a3, a0, a2 # get remainder
addi a3, a3, 48 # convert to ascii
sw a3, 0(sp) # store in stack
addi sp, sp, -4 # decrement stack pointer
div a0, a0, a2 # divide by 10
bnez a0, convert_loop # loop until a0 is zero
addi a0, x0, 1 # 1 = StdOut
addi sp, sp, 4 # increment stack pointer to point to the start of the string
mv a1, sp # copy stack pointer to a1
sub a2, a4, sp # calculate length of string
slli a2, a2, 2 # multiply length by 4
addi a7, x0, 64 # linux write system call
ecall # Call linux to output the string
addi a0, x0, 1 # 1 = StdOut
la a1, newline # load address of helloworld
addi a2, x0, 1 # length of our string
addi a7, x0, 64 # linux write system call
ecall # Call linux to output the string
# Call linux to terminate the program
# Exit the program cleanly
li a7, 93 # Exit system call for RISC-V
li a0, 0 # Exit status code
ecall