MIPS 汇编、堆栈和基本加法
MIPS assembly, stacks and basic addition
我目前正在为 class 做作业,它要求我们接受两个变量(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
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)
我目前正在为 class 做作业,它要求我们接受两个变量(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
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)