如何使用加法乘以MIPS中的整数
我试图将两个正整数相乘,然后将它们转换为浮点值并输出该值。我不明白我做错了什么,因为我使用 $s3 作为 int i 并在每次程序分支到 multi_add 时递增。我真的不知道我做错了什么,这非常令人沮丧。我试图将其视为一个 while 循环,只要 b>i 为真,其中 i 为 0,b 为乘数。然后执行 c = c + a 和 i++。一旦 i 大于 b 或乘数,它就会退出循环并打印该值。
.data
prompta: .asciiz "input A"
promptb: .asciiz "input B"
prompt: .asciiz "f = "
newline: .asciiz "\n"
.text
main:
addi $t3, $t3, 0 #i
#Print the prompt1
li $v0, 4
la $a0, prompta
syscall
#newline
li $v0, 4
la $a0, newline
syscall
#enter int A
li $v0, 5
syscall
move $t1, $v0
#Print the prompt2
li $v0, 4
la $a0, promptb
syscall
#newline
li $v0, 4
la $a0, newline
syscall
#enter int B
li $v0, 5
syscall
move $t2, $v0
multiply: bgt $t2, $t3, multi_add #if B>i jump
j result
multi_add: add $t1, $t1, $ #c = c+a
addi $s3, $s3, 1 #i = i+1
jal multiply
result:
li $v0, 4
la $a0, prompt
syscall
mfc1 $t1, $f1
cvt.s.w $f1, $f1
li $v0, 2
add.s $f12, $f12, $f1
syscall
I'm trying to multiply two positive integers together then convert them into a float value and output that value. I don't understand what I'm doing wrong because I use $s3 as an int i and increment each time the program branches to multi_add. I truly don't know what I'm doing wrong and it's very frustrating. I'm was trying to think of it as a while loop that say as long as b>i is true where i is 0 and b is the multiplier. Then do c = c + a and i++. Once i is greater than b or the multipier it exit the loop and prints the value.
.data
prompta: .asciiz "input A"
promptb: .asciiz "input B"
prompt: .asciiz "f = "
newline: .asciiz "\n"
.text
main:
addi $t3, $t3, 0 #i
#Print the prompt1
li $v0, 4
la $a0, prompta
syscall
#newline
li $v0, 4
la $a0, newline
syscall
#enter int A
li $v0, 5
syscall
move $t1, $v0
#Print the prompt2
li $v0, 4
la $a0, promptb
syscall
#newline
li $v0, 4
la $a0, newline
syscall
#enter int B
li $v0, 5
syscall
move $t2, $v0
multiply: bgt $t2, $t3, multi_add #if B>i jump
j result
multi_add: add $t1, $t1, $ #c = c+a
addi $s3, $s3, 1 #i = i+1
jal multiply
result:
li $v0, 4
la $a0, prompt
syscall
mfc1 $t1, $f1
cvt.s.w $f1, $f1
li $v0, 2
add.s $f12, $f12, $f1
syscall
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论