diff options
Diffstat (limited to 'boot/common-boot.s')
| -rw-r--r-- | boot/common-boot.s | 70 |
1 files changed, 68 insertions, 2 deletions
diff --git a/boot/common-boot.s b/boot/common-boot.s index c6fcb98..e8eba52 100644 --- a/boot/common-boot.s +++ b/boot/common-boot.s @@ -1,5 +1,6 @@ .global _is_alpha, _is_digit, _is_alnum, _is_upper, _is_lower -.global _write_out, _read_file, exit, _memcmp, _write_error, _put_char +.global _write_out, _read_file, _memcmp, _write_error, _put_char, _printi +.global _divide_by_zero_error, _exit .section .rodata @@ -119,6 +120,13 @@ _is_alpha: addi sp, sp, 16 ret +# Detects whether the passed character is a digit +# (a value between 0 and 9). +# +# Parameters: +# a0 - Exemined value. +# +# Sets a0 to 1 if it is a digit, to 0 otherwise. .type _is_digit, @function _is_digit: li t0, '0' - 1 @@ -201,10 +209,68 @@ _read_file: ret # Terminates the program. a0 contains the return code. +# +# Parameters: +# a0 - Status code. .type _exit, @function -exit: +_exit: li a7, SYS_EXIT ecall + # ret + +.type _divide_by_zero_error, @function +_divide_by_zero_error: + addi a7, zero, 172 # getpid + ecall + + addi a1, zero, 8 # SIGFPE + addi a7, zero, 129 # kill + ecall + ret + +# a0 - Whole number. +# t1 - Constant 10. +# a1 - Local buffer. +# t2 - Current character. +# t3 - Whether the number is negative. +.type printi, @function +_printi: + addi sp, sp, -16 + sw s0, 0(sp) + sw ra, 4(sp) + addi s0, sp, 16 + addi t1, zero, 10 + addi a1, s0, -1 + + addi t3, zero, 0 + bge a0, zero, .digit10 + addi t3, zero, 1 + sub a0, zero, a0 + +.digit10: + rem t2, a0, t1 + addi t2, t2, '0' + sb t2, 0(a1) + div a0, a0, t1 + addi a1, a1, -1 + bne zero, a0, .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 # Writes a character from a0 into the standard output. .type _put_char, @function |
