将整数移动到新寄存器而不在MIPS中覆盖
我对MIPS和这个网站非常陌生(这是我的第一篇帖子),所以请在这里耐心听我说……我必须取一个用户输入的数字,用除法(HI/LO)将其逐整数反转,并将余数存储到一个新的寄存器中,以便将反转后的数字与原始数字进行比较,看它是否是回文。这很好,得到了除法部分(我想?),但一旦我第二次除法,并尝试将第二个余数添加到第一个除法的余数上,它将简单地覆盖寄存器的内容,而不是在其末尾添加数字,对吗?我在网上找不到这个问题的答案。我怎样才能做到这一点?因为简单的“移动”会覆盖内容,对吗?这是到目前为止我的代码将整数移动到新寄存器而不在MIPS中覆盖,mips,Mips,我对MIPS和这个网站非常陌生(这是我的第一篇帖子),所以请在这里耐心听我说……我必须取一个用户输入的数字,用除法(HI/LO)将其逐整数反转,并将余数存储到一个新的寄存器中,以便将反转后的数字与原始数字进行比较,看它是否是回文。这很好,得到了除法部分(我想?),但一旦我第二次除法,并尝试将第二个余数添加到第一个除法的余数上,它将简单地覆盖寄存器的内容,而不是在其末尾添加数字,对吗?我在网上找不到这个问题的答案。我怎样才能做到这一点?因为简单的“移动”会覆盖内容,对吗?这是到目前为止我的代码 l
li $v0, 4 # System call code for print string
la $a0, Prompt # Load address for Prompt into a0
syscall
li $v0, 5 # System call code for read integer
syscall # Read the integer into v0
move $t0, $v0 # Move the value into t0
move $t9, $t0
li $s0, 10 # Load 10 into s0 for division
li $s1, 0 # Load 0 into s1 for division
div $t0, $s0 # Divides t0 by 10
mfhi $t1 # Move remainder into t1
mflo $t0 # Move quotient into t0
从本质上讲,我希望将剩余的连接在一起,而不是将它们添加在一起或覆盖寄存器。假设第一个余数是3,第二个是6,第三个是9。最后,我不希望它是18或9。我希望是369 您的
div
/mfhi
/mflo
很好。您需要的是一个具有第二个变量的循环
我已经创建了等效的C代码,并将其添加为注释块,并创建了一个工作程序[请原谅这种不必要的风格清理]:
# int
# rev(int inp)
# {
# int acc;
# int dig;
#
# acc = 0;
#
# while (inp != 0) {
# dig = inp % 10;
# inp /= 10;
#
# acc *= 10;
# acc += dig;
# }
#
# return acc;
# }
.data
Prompt: .asciiz "Enter number to reverse:\n"
nl: .asciiz "\n"
.text
.globl main
main:
li $v0,4 # System call code for print string
la $a0,Prompt # Load address for Prompt into a0
syscall
li $v0,5 # System call code for read integer
syscall # Read the integer into v0
bltz $v0,exit # continue until stop requested
move $t0,$v0 # Move the value into t0
li $t3,0 # initialize accumulator
li $s0,10 # Load 10 into s0 for division
li $s1,0 # Load 0 into s1 for division
next_digit:
beqz $t0,print # more to do? if no, fly
div $t0,$s0 # Divides t0 by 10
mfhi $t1 # Move remainder into t1 (i.e. dig)
mflo $t0 # Move quotient into t0 (i.e. inp)
mul $t3,$t3,$s0 # acc *= 10
add $t3,$t3,$t1 # acc += dig
j next_digit # try for more
print:
li $v0,1 # print integer syscall
move $a0,$t3 # get value to print
syscall
li $v0,4
la $a0,nl
syscall
j main
exit:
li $v0,10
syscall
很抱歉我没有回应,非常感谢你的帮助!