0

so as i was last said in my previous question:

I have an exercise in my univercity and i would like to have some help! first of all i'm new in mips language! So, my exercise is asking to make a programm in qtSpim which it will read 8 integers from the console then it will save it in an array to the memory and last it will print the sextuple of those integers in reserve order! Can you help me a little bit with the coding?

So, i tried a little bit alone and this is what i did so far:

  # $18=c

.data

myArray: .space 32

.align 2   

    str_s:  .asciiz "give me 8 numbers in 8 lines:" 
    str_nl: .asciiz "\n"



.text

    .globl main     #label "main" must be global
    .globl loop

main:

la $17, myArray

                            #BUILD THE PROMPT
    addi $2, $0, 4    # system call code for print_string
    la   $4, str_s    # pseudo-istruction: address of string
    syscall           # print the string from str_s

    add $18, $0, $0    # c=0

loop:

    addi $2, $0, 5   #system call for read_string
    syscall
    add $16, $2, $0  #copy return int from $2 to $16
    sw  $16, 0($17)  #save int from $16 to the array

    addi $18, $18, 1   # c=c+1
    bne  $18, 8, loop  #repeat while (c!=n)

loop:

j main

So as you can se in the second loop i have to print the integers of the array in reverse order...in google all i could found is to print integers but is it different now that i have a string? should i do it with integers from the beggining?

`

Michael
  • 57,169
  • 9
  • 80
  • 125
coding girl
  • 9
  • 1
  • 2
  • _"is it different now that i have a string?"_ What strings? Your comment `#system call for read_string` is misleading, because system call 5 is `read_int`, not `read_string`. – Michael Feb 28 '16 at 17:19

1 Answers1

-2
.data
arr: .space 32
msg1: .asciiz "put in first integer"
msg2: .asciiz "put in second integer"
msg3: .asciiz "put in third integer"
msg4: .asciiz "put in fourth integer"
msg5: .asciiz "put in fifth integer"
msg6: .asciiz "put in sixth integer"
msg7: .asciiz "put in seventh integer"
msg8: .asciiz "put in eighth integer"
.globl main
.text
main:
la $s0, arr
li $v0, 4
la $a0, msg1
syscall

li $v0, 5
syscall
sw $v0, 28($s0)

li $v0, 4
la $a0, msg2
syscall
li $v0, 5
syscall
sw $v0, 24($s0)

li $v0, 4
la $a0, msg3
syscall
li $v0, 5
syscall
sw $v0, 20($s0)

li $v0, 4
la $a0, msg4
syscall
li $v0, 5
syscall
sw $v0, 16($s0)

li $v0, 4
la $a0, msg5
syscall
li $v0, 5
syscall
sw $v0, 12($s0)

li $v0, 4
la $a0, msg6
syscall
li $v0, 5
syscall
sw $v0, 8($s0)

li $v0, 4
la $a0, msg7
syscall
li $v0, 5
syscall
sw $v0, 4($s0)

li $v0, 4
la $a0, msg8
syscall
li $v0, 5
syscall
sw $v0, 0($s0)

li $v0, 1
lw $a0,0($s0)
syscall 

 li $v0, 1
 lw $a0,4($s0)
 syscall 

 li $v0, 1
 lw $a0,8($s0)
 syscall 

li $v0, 1
lw $a0,12($s0)
syscall 

li $v0, 1
lw $a0,16($s0)
syscall 

li $v0, 1
lw $a0,20($s0)
syscall 

 li $v0, 1
lw $a0,24($s0)
syscall 

li $v0, 1
lw $a0,28($s0)
syscall 

li $v0, 10#terminate execution
syscall

basically, what I do here is I allocate the space I created in arr to $s0 and then I use syscall to take the inputs and store them in $s0 and last but not least I print them. Easy very piecy.

The code is now fixed!!

Zac Uwyo H
  • 137
  • 1
  • 10
  • 1
    _"Try it out."_. I did, it doesn't work. It doesn't even assemble because you're using `addi` with 3 register operands. If we assume that you meant to use `addu` instead of `addi` there's still the problem that the code doesn't produce the correct result (it outputs the first element in the array 8 times). – Michael Mar 01 '16 at 09:32
  • The code is fixed!! Everything is good. I execute it. It works well on my side at least. – Zac Uwyo H Mar 01 '16 at 18:51