diff options
Diffstat (limited to 'boot/common-boot.s')
| -rw-r--r-- | boot/common-boot.s | 118 |
1 files changed, 78 insertions, 40 deletions
diff --git a/boot/common-boot.s b/boot/common-boot.s index ad57b41..0c2ef6b 100644 --- a/boot/common-boot.s +++ b/boot/common-boot.s @@ -3,7 +3,7 @@ # obtain one at https://mozilla.org/MPL/2.0/. .global _is_alpha, _is_digit, _is_alnum, _is_upper, _is_lower -.global _write_out, _read_file, _write_error, _put_char, _printi +.global _write_s, _read_file, _write_error, _write_c, _write_i, _print_i .global _get, _memcmp, _memchr, _memmem, _memcpy .global _divide_by_zero_error, _exit, _mmap .global _strings_index, _string_equal @@ -175,16 +175,20 @@ _is_alnum: addi sp, sp, 16 ret -.type _write_out, @function -_write_out: +# Writes a string to the standard output. +# +# Parameters: +# a0 - Length of the string. +# a1 - String pointer. +.type _write_s, @function +_write_s: # Prologue. addi sp, sp, -8 sw ra, 4(sp) sw s0, 0(sp) addi s0, sp, 8 - mv a2, a1 - mv a1, a0 + mv a2, a0 li a0, STDOUT li a7, SYS_WRITE ecall @@ -244,53 +248,87 @@ _divide_by_zero_error: ecall ret -# a0 - Whole number. +# Writes a number to a string buffer. +# +# t0 - Local buffer. # 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: +# +# Parameters: +# a0 - Whole number. +# a1 - Buffer pointer. +# +# Sets a0 to the length of the written number. +.type _print_i, @function +_print_i: + addi sp, sp, -32 + sw ra, 28(sp) + sw s0, 24(sp) + addi s0, sp, 32 + + li t1, 10 + addi t0, s0, -9 + + li t3, 0 + bgez a0, .Lprint_i_digit10 + li t3, 1 + neg a0, a0 + +.Lprint_i_digit10: rem t2, a0, t1 addi t2, t2, '0' - sb t2, 0(a1) + sb t2, 0(t0) div a0, a0, t1 - addi a1, a1, -1 - bne zero, a0, .digit10 + addi t0, t0, -1 + bne zero, a0, .Lprint_i_digit10 - beq zero, t3, .write_call + beq zero, t3, .Lprint_i_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 + sb t2, 0(t0) + addi t0, t0, -1 - lw s0, 0(sp) - lw ra, 4(sp) - addi sp, sp, 16 +.Lprint_i_write_call: + mv a0, a1 + addi a1, t0, 1 + sub a2, s0, t0 + addi a2, a2, -9 + sw a2, 0(sp) + + call _memcpy + + lw a0, 0(sp) + + lw ra, 28(sp) + lw s0, 24(sp) + addi sp, sp, 32 + ret + +# Writes a number to the standard output. +# +# Parameters: +# a0 - Whole number. +.type _write_i, @function +_write_i: + addi sp, sp, -32 + sw ra, 28(sp) + sw s0, 24(sp) + addi s0, sp, 32 + + addi a1, sp, 0 + call _print_i + + addi a1, sp, 0 + call _write_s + + lw ra, 28(sp) + lw s0, 24(sp) + addi sp, sp, 32 ret # Writes a character from a0 into the standard output. -.type _put_char, @function -_put_char: +.type _write_c, @function +_write_c: # Prologue addi sp, sp, -16 sw ra, 12(sp) |
