106 lines
1.6 KiB
ArmAsm
106 lines
1.6 KiB
ArmAsm
.global printi
|
|
.type printi, @function
|
|
|
|
.global printc
|
|
.type printc, @function
|
|
|
|
.global exit
|
|
.type exit, @function
|
|
|
|
.global _divide_by_zero_error
|
|
.type _divide_by_zero_error, @function
|
|
|
|
.global _start
|
|
.type _start, @function
|
|
|
|
.text
|
|
printi:
|
|
addi sp, sp, -16
|
|
sw s0, 0(sp)
|
|
sw ra, 4(sp)
|
|
addi s0, sp, 16
|
|
|
|
addi t1, zero, '\n'
|
|
sb t1, -1(s0)
|
|
|
|
# t0 - Whole number.
|
|
# t1 - Constant 10.
|
|
# a1 - Local buffer.
|
|
# t2 - Current character.
|
|
# t3 - Whether the number is negative.
|
|
lw t0, 0(s0)
|
|
addi t1, zero, 10
|
|
addi a1, s0, -2
|
|
|
|
addi t3, zero, 0
|
|
bge t0, zero, .digit10
|
|
addi t3, zero, 1
|
|
sub t0, zero, t0
|
|
|
|
.digit10:
|
|
rem t2, t0, t1
|
|
addi t2, t2, '0'
|
|
sb t2, 0(a1)
|
|
div t0, t0, t1
|
|
addi a1, a1, -1
|
|
bne zero, t0, .digit10
|
|
|
|
beq zero, t3, .write_call
|
|
addi t2, zero, '-'
|
|
sb t2, 0(a1)
|
|
addi a1, a1, -1
|
|
|
|
.write_call:
|
|
addi a0, zero, 1
|
|
addi a1, a1, 1
|
|
sub a2, s0, a1
|
|
addi a7, zero, 64 # write
|
|
ecall
|
|
|
|
lw s0, 0(sp)
|
|
lw ra, 4(sp)
|
|
addi sp, sp, 16
|
|
ret
|
|
|
|
printc:
|
|
addi sp, sp, -12
|
|
sw s0, 0(sp)
|
|
sw ra, 4(sp)
|
|
addi s0, sp, 12
|
|
|
|
addi t1, zero, '\n'
|
|
sb t1, -1(s0)
|
|
|
|
lw t0, 0(s0)
|
|
sb t0, -2(s0)
|
|
|
|
addi a0, zero, 1
|
|
addi a1, s0, -2
|
|
addi a2, zero, 2
|
|
addi a7, zero, 64 # write
|
|
ecall
|
|
|
|
lw s0, 0(sp)
|
|
lw ra, 4(sp)
|
|
addi sp, sp, 12
|
|
ret
|
|
|
|
exit:
|
|
addi a0, zero, 0
|
|
addi a7, zero, 93 # exit
|
|
ecall
|
|
# ret
|
|
|
|
_divide_by_zero_error:
|
|
addi a7, zero, 172 # getpid
|
|
ecall
|
|
|
|
addi a1, zero, 8 # SIGFPE
|
|
addi a7, zero, 129 # kill
|
|
ecall
|
|
ret
|
|
|
|
_start:
|
|
call main
|
|
call exit
|