开发者

Assembly code problem

I want to have a program that takes 2 inputs from the user and multiplies each other 开发者_如何学Gousing addition(for example 3*2 adds 2 three times) but I don't know what's wrong with it. Thanks in advance

.text    
.globl main
main:
  li           $v0, 5                       # Code for read int.
  syscall                                   # Ask the system for service.
  move         $s0, $v0                     # Copy to safer location.

  # Ask for another number.
  li           $v0, 5                       # Code for read int.
  syscall                                  # Ask the system for service.

loop:
  add  $s0,$s0,$s0
  addi $v0,$v0,-1
  bne $v0,$zero,loop                                  

  li      $v0, 1
  syscall                                 # print out actual sum
  li      $v0, 10                         # Code for program exit.
  syscall


What do you think add $s0,$s0,$s0 does?

It doubles the number. If the iteration count were 6, then it would double the first value six times.

Is this homework? If so, please add that tag to the question.


You are doubling the value every time, not adding the initial value to it as you want. That means it becomes $s0 = 6, $s0 = 12, $s0 = 24, $s0 = 48, $s0 = 96 etc.

Use a temporary register to hold the initial value of $s0 ($t0) and when you do your add do it like add $s0 $s0 $t0 (or something to this effect) and it will do what you think your code is doing

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜