Взгляните на мою функцию intprint
.Он выполняет работу рекурсивно:
# Print integer character by character on separate lines
# Tested with MARS
.globl main
main:
li $s0, 12345 # integer to be printed
li $v0, 1 # print int
move $a0, $s0
syscall
li $v0, 11 # print char
li $a0, '\n'
syscall
# print integer char by char recursively
move $a0, $s0
jal intprint
li $v0, 10 # exit
syscall
intprint:
addi $sp, $sp, -8 # preserve registers in stack
sw $ra, 0($sp)
sw $s0, 4($sp)
move $s0, $a0
ble $s0, 9, output
li $t0, 10 # divide by 10
div $s0, $t0
mfhi $s0 # remainder
mflo $a0 # quotient
jal intprint # recursion
output:
add $a0, $s0, '0'
li $v0, 11
syscall
li $a0, '\n'
syscall
lw $ra, 0($sp) # restore registers from stack
lw $s0, 4($sp)
addi $sp, $sp, 8 # restore stack pointer
jr $ra
Вывод:
12345
1
2
3
4
5