如何使用加法乘以MIPS中的整数

发布于 2025-01-20 02:06:02 字数 994 浏览 1 评论 0原文

我试图将两个正整数相乘,然后将它们转换为浮点值并输出该值。我不明白我做错了什么,因为我使用 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文