user671645
user671645

Reputation: 1

Assembly code problem

I want to have a program that takes 2 inputs from the user and multiplies each other using 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

Upvotes: 0

Views: 145

Answers (2)

Mike
Mike

Reputation: 8555

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

Upvotes: 0

wallyk
wallyk

Reputation: 57794

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.

Upvotes: 5

Related Questions