Implement if statements

This commit is contained in:
2025-04-29 00:55:20 +02:00
parent 963d32e8d1
commit 9c66cec171
7 changed files with 476 additions and 213 deletions

View File

@@ -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