elna/tools/builtin.s

106 lines
1.6 KiB
ArmAsm
Raw Normal View History

2024-09-15 23:03:25 +02:00
.global printi
.type printi, @function
2024-10-04 18:26:10 +02:00
.global printc
.type printc, @function
.global exit
.type exit, @function
2024-10-06 18:07:57 +02:00
.global _divide_by_zero_error
.type _divide_by_zero_error, @function
2024-09-21 23:35:32 +02:00
.global _start
.type _start, @function
2024-09-15 23:03:25 +02:00
.text
printi:
2024-09-27 00:22:44 +02:00
addi sp, sp, -16
2024-09-15 23:03:25 +02:00
sw s0, 0(sp)
sw ra, 4(sp)
2024-09-27 00:22:44 +02:00
addi s0, sp, 16
addi t1, zero, '\n'
sb t1, -1(s0)
# t0 - Whole number.
# t1 - Constant 10.
# a1 - Local buffer.
# t2 - Current character.
2024-09-29 19:50:55 +02:00
# t3 - Whether the number is negative.
2024-09-27 00:22:44 +02:00
lw t0, 0(s0)
addi t1, zero, 10
addi a1, s0, -2
2024-09-29 19:50:55 +02:00
addi t3, zero, 0
bge t0, zero, .digit10
addi t3, zero, 1
sub t0, zero, t0
2024-09-27 00:22:44 +02:00
.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
2024-09-15 23:03:25 +02:00
2024-09-29 19:50:55 +02:00
beq zero, t3, .write_call
addi t2, zero, '-'
sb t2, 0(a1)
addi a1, a1, -1
2024-09-15 23:03:25 +02:00
2024-09-29 19:50:55 +02:00
.write_call:
2024-09-27 00:22:44 +02:00
addi a0, zero, 1
addi a1, a1, 1
sub a2, s0, a1
2024-10-06 18:07:57 +02:00
addi a7, zero, 64 # write
2024-09-27 00:22:44 +02:00
ecall
2024-09-15 23:03:25 +02:00
lw s0, 0(sp)
lw ra, 4(sp)
2024-09-27 00:22:44 +02:00
addi sp, sp, 16
2024-09-15 23:03:25 +02:00
ret
2024-09-21 23:35:32 +02:00
2024-10-04 18:26:10 +02:00
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
2024-10-06 18:07:57 +02:00
addi a7, zero, 64 # write
2024-10-04 18:26:10 +02:00
ecall
lw s0, 0(sp)
lw ra, 4(sp)
addi sp, sp, 12
ret
exit:
2024-09-21 23:35:32 +02:00
addi a0, zero, 0
2024-10-06 18:07:57 +02:00
addi a7, zero, 93 # exit
2024-09-21 23:35:32 +02:00
ecall
2024-10-04 18:26:10 +02:00
# ret
2024-10-06 18:07:57 +02:00
_divide_by_zero_error:
addi a7, zero, 172 # getpid
ecall
addi a1, zero, 8 # SIGFPE
addi a7, zero, 129 # kill
ecall
ret
2024-10-04 18:26:10 +02:00
_start:
call main
call exit