我目前正在为类分配,要求我们接受两个变量(a和b)的用户输入值并解决方程式[a - ab + 8a - 10b + 19],同时仅使用堆栈和寄存器$ t0和$ t1。
我理解我需要做什么,但我无法弄清楚为什么我的程序在编译时显示不正确的解决方案。对于堆栈,我应该在我弹出和推送时引用特定位置[例如4($ sp)]?我从谷歌搜索得到了不一致的答案。
我为你在这里看到的任何奇怪的方法道歉,我一直无法忍受它一段时间无济于事。
结果:[a = 4,b = 3,溶液= 51] [a = 6,b = 4,溶液= 67]
.data
aprompt: .asciiz "Input a value for a: "
bprompt: .asciiz "Input a value for b: "
eq: .asciiz "a - ab + 8a - 10b + 19 = "
.text
main:
la $a0, aprompt # print the prompt for a
li $v0, 4
syscall
li $v0, 5 # read input as integer
syscall
move $t0, $v0 # store variable a in $t0
la $a0, bprompt # print the prompt for b
li $v0, 4
syscall
li $v0, 5 # read input as integer
syscall
move $t1, $v0 # store variable b in $t1
addi $sp,$sp,-4 # decrement stack pointer by 4
sw $t0,($sp) # push a on the stack at 0
mult $t0,$t1 # a*b
mflo $t0 # store result in $t0
addi $sp,$sp,-4 # decrement stack pointer by 4
sw $t0,($sp) # push a*b on the stack at 4
li $t0,10 # load 10 into $t0
mult $t0,$t1 # 10*b
mflo $t0 # store result in $t0
lw $t1,($sp) # pop the stack and store in $t1
addi $sp,$sp,4 # increment stack pointer by 4
add $t0,$t0,$t1 # add a*b and 10*b
lw $t1,($sp) # pop the stack and store in $t1
addi $sp,$sp,-4 # decrement stack pointer
sw $t0,($sp) # push a*b+10*b on the stack at 0
addi $sp,$sp,4 # increment stack pointer
li $t0,8 # store 8 in $t0
mult $t0,$t1 # 8*a
mflo $t0 # store in $t0
addi $t1,$t1,19 # a + 19
add $t0,$t0,$t1 # store (a + 19) + 8*a in $t0
lw $t1,($sp) # pop the stack and store in $t1
sub $t0,$t0,$t1 # (a + 19 + 8a) - (ab + 10b)
move $a0,$t0 # print solution as integer
li $v0,1
syscall
li $v0, 10 # exit
syscall
答案 0 :(得分:2)
lw $t1,($sp) # pop the stack and store in $t1
sub $t0,$t0,$t1 # (a + 19 + 8a) - (ab + 10b)
lw $t1,($sp)
实际上是a
,而不是ab + 10b
。
您可以通过弹出ab + 10b
而不是$ sp - 4来修复它。
add $t0,$t0,$t1 # store (a + 19) + 8*a in $t0
addi $sp,$sp,-4 # decrement stack pointer
lw $t1,($sp) # pop the stack and store in $t1
sub $t0,$t0,$t1 # (a + 19 + 8a) - (ab + 10b)