【发布时间】:2010-09-14 02:57:11
【问题描述】:
我在这里遇到了 MIPS 问题。我想要做的是让用户输入任何数字并让程序拼出数字中每个数字的名称。例如,用户输入 495,机器会拼出“四九五”。我正在尝试将数字的每个数字推入堆栈,然后将每个数字弹出。但是,弹出的数字并不是看起来被压入堆栈的数字。我对此感到很困惑,我希望有人能帮助我!我的代码的重要部分如下:
.text
main: li $v0, 5
syscall
move $t0, $v0
# store 10 into a register, because you can't divide by a constant
li $s1, 10
# now the input number is stored in $a0. Time to divide by 10 a few times,
#and store each digit in a stack:
DivideLoop: # $s3 = $s0 / 10
div $t0, $s1
# this is the remainder of the division
mfhi $t3
mflo $t2
# move stack over by 4
addi $sp, $sp, -4
# store remainder in stack
sw $t3, 0($sp)
move $t0, $t2
beq $t2, $zero, PrintLoop
j DivideLoop
# This loop prints each digit
PrintLoop:
# check if stack is empty. If so, branch to exit.
beq $sp, $zero, Exit
# put first word of stack into register $t0
lw $t0, 0($sp)
addi $sp, $sp, 4
【问题讨论】: