0
.data
prompt1: .asciiz "\n\n Enter an integer please:"

array: .space 24
linefeed: .asciiz "\n"
enterkey: .asciiz "Press any key to end program."


.text

main:



li $s0, 0

  for:

  bge $s0, 6, end_for

  li $v0, 4 #syscall to print string

  la $a0, prompt1  #address of string to print

  syscall

  li $v0, 5 #syscall to read an integer

  syscall

  move $t1,$v0

  sw $t1,array($t0) #save the number to read into array 

  addi $t0,$t0,4

  addi $s0,$s0,1

  j for

end_for:


# print out a line feed

li $v0,4 # code for print_string

la $a0,linefeed # point $a0 to linefeed string

syscall # print linefeed

# wait for the enter key to be pressed to end program

li $v0,4 # code for print_string

la $a0,enterkey # point $a0 to enterkey string

syscall # print enterkey

# wait for input by getting an integer from the user (integer is ignored)

li $v0,5 # code for read_int

syscall #get int from user --> returned in $v0

# All done, thank you!

li $v0,10 # code for exit

syscall # exit program

this is my code.I am trying to store 6 integers in an array and then read again the array of integers and sum them and then printing the sum.I apologize for my bad english

Michael
  • 57,169
  • 9
  • 80
  • 125
mitsos
  • 1
  • 1
  • 2
  • 3

1 Answers1

1

It would be basically the same loop you have just written, but instead of writing the number to the array you would have to read it from the array and sum the values.

E.g.:

  li $s0, 0
  li $a0, 0
  li $t0, 0
forsum:    
  bge $s0, 6, end_forsum    
  lw $t1,array($t0)  # Load the number from array 
  addu $a0, $a0, $t1 # Compute the sum

  addi $t0,$t0,4    
  addi $s0,$s0,1      
  j forsum
end_forsum:

  li $v0,1 
  syscall   # Print sum

You can also compute the sum while reading the values from the user input...

gusbro
  • 22,357
  • 35
  • 46