57 lines
1 KiB
ArmAsm
57 lines
1 KiB
ArmAsm
.text
|
|
|
|
.globl len
|
|
.globl puts
|
|
.globl exit
|
|
|
|
len:
|
|
mv t0, a0
|
|
mv t1, t0
|
|
|
|
lenloop:
|
|
lb t2, (t1)
|
|
beq t2, x0, lencleanup
|
|
addi t1, t1, 1
|
|
j lenloop
|
|
|
|
lencleanup:
|
|
sub a0, t1, t0
|
|
ret
|
|
|
|
puts:
|
|
#here a0 should be the pointer to the string
|
|
|
|
#save s2 becuase we are gonna use it
|
|
addi sp, sp, -16
|
|
sd s2, 12(sp)
|
|
|
|
#save a0 because we need it later
|
|
mv s2, a0
|
|
|
|
#get length because the stdout systemcall needs it in a2
|
|
addi sp, sp, -16 # Allocate stack space
|
|
sd ra, 12(sp) # Save return address
|
|
jal ra, len
|
|
ld ra, 12(sp) # Restore return address
|
|
addi sp, sp, 16 # Deallocate stack space
|
|
#a0 should be the length
|
|
mv a2, a0 #stdout expects it in a2
|
|
|
|
li a0, 1 # file descriptor = 1 (stdout)
|
|
li a7, 64 # syscall write (64)
|
|
|
|
mv a1, s2 #this should still be the pointer to the text
|
|
|
|
#restore s2 for the next guy
|
|
ld s2, 12(sp)
|
|
addi sp, sp, 16
|
|
|
|
ecall
|
|
|
|
ret
|
|
|
|
exit:
|
|
li a0, 0 # exit code
|
|
li a7, 93 # syscall exit
|
|
ecall
|
|
|