Hey I\'m having trouble printing an array in MIPS using a loop.Heres the code .d
ID: 3616710 • Letter: H
Question
Hey I'm having trouble printing an array in MIPS using a loop.Heres the code .data numbers: .space 36 message1: .asciiz "Enter an integer: " message2: .asciiz "The array contains the following: " next_line: .asciiz " ".text .globl main main:
la $a1,numbers # $a1 is the base address ofthe array li $a2,9 # $a2 = 9; jal readArray jal printArray jr $ra
readArray: li $t0,0 # i = 0; loop: bge $t0, $a2, Exit#if (i >= 9) Exit the loop; # cout << message1 <<endl; la $a0,message1 li $v0,4 syscall # cin >> numbers[i]; li $v0,5 syscall sw $v0,($a1)
addi $a1,$a1, 4 # move the array over by 1 element addi $t0,$t0, 1 #i++ j loop
printArray: li $t0,0 #1 = 0; # cout << message2 <<endl; la $a0,message2 li $v0,4 syscall loop1: bge $t0, $a2,Exit1 #if (i >= 9) Exit theloop; li $v0,1 move $a0,0($a1) syscall #cout << " " <<endl; la $a0,next_line li $v0,4 syscall addi $a1,$a1, 4 addi $t0,$t0, 1 j loop1 Exit1: jr $ra
The line I'm getting a syntax error is: move $a0,0($a1). I'm not sure how to get the address of my arrayinto $a0. I also tried addi$t3, ($a1), 0, and then move $a0, $t3, butthat didn't work either. Any help would beappreciated.
.data numbers: .space 36 message1: .asciiz "Enter an integer: " message2: .asciiz "The array contains the following: " next_line: .asciiz " "
.text .globl main main:
la $a1,numbers # $a1 is the base address ofthe array li $a2,9 # $a2 = 9; jal readArray jal printArray jr $ra
readArray: li $t0,0 # i = 0; loop: bge $t0, $a2, Exit#if (i >= 9) Exit the loop; # cout << message1 <<endl; la $a0,message1 li $v0,4 syscall # cin >> numbers[i]; li $v0,5 syscall sw $v0,($a1)
addi $a1,$a1, 4 # move the array over by 1 element addi $t0,$t0, 1 #i++ j loop
printArray: li $t0,0 #1 = 0; # cout << message2 <<endl; la $a0,message2 li $v0,4 syscall loop1: bge $t0, $a2,Exit1 #if (i >= 9) Exit theloop; li $v0,1 move $a0,0($a1) syscall #cout << " " <<endl; la $a0,next_line li $v0,4 syscall addi $a1,$a1, 4 addi $t0,$t0, 1 j loop1 Exit1: jr $ra
The line I'm getting a syntax error is: move $a0,0($a1). I'm not sure how to get the address of my arrayinto $a0. I also tried addi$t3, ($a1), 0, and then move $a0, $t3, butthat didn't work either. Any help would beappreciated.
readArray: li $t0,0 # i = 0; loop: bge $t0, $a2, Exit#if (i >= 9) Exit the loop; # cout << message1 <<endl; la $a0,message1 li $v0,4 syscall # cin >> numbers[i]; li $v0,5 syscall sw $v0,($a1)
addi $a1,$a1, 4 # move the array over by 1 element addi $t0,$t0, 1 #i++ j loop
printArray: li $t0,0 #1 = 0; # cout << message2 <<endl; la $a0,message2 li $v0,4 syscall loop1: bge $t0, $a2,Exit1 #if (i >= 9) Exit theloop; li $v0,1 move $a0,0($a1) syscall #cout << " " <<endl; la $a0,next_line li $v0,4 syscall addi $a1,$a1, 4 addi $t0,$t0, 1 j loop1 Exit1: jr $ra printArray: li $t0,0 #1 = 0; # cout << message2 <<endl; la $a0,message2 li $v0,4 syscall loop1: bge $t0, $a2,Exit1 #if (i >= 9) Exit theloop; li $v0,1 move $a0,0($a1) syscall #cout << " " <<endl; la $a0,next_line li $v0,4 syscall addi $a1,$a1, 4 addi $t0,$t0, 1 j loop1 Exit1: jr $ra
The line I'm getting a syntax error is: move $a0,0($a1). I'm not sure how to get the address of my arrayinto $a0. I also tried addi$t3, ($a1), 0, and then move $a0, $t3, butthat didn't work either. Any help would beappreciated.
Explanation / Answer
Given below is the fixed code for the question.
Please do rate the answer if it was helpful. Thank you
.data
numbers: .space 36
message1: .asciiz "Enter an integer: "
message2: .asciiz "The array contains the following: "
next_line: .asciiz " "
.text
.globl main
main:
la $a1,numbers # $a1 is the base address ofthe array
li $a2,9 # $a2 = 9;
jal readArray
la $a1,numbers # $a1 is the base address ofthe array
li $a2,9 # $a2 = 9;
jal printArray
#exit
li $v0, 10
syscall
#-------------------
readArray:
li $t0,0 # i = 0;
loop: bge $t0, $a2, Exit#if (i >= 9) Exit the loop;
# cout << message1 <<endl;
la $a0,message1
li $v0,4
syscall
# cin >> numbers[i];
li $v0,5
syscall
sw $v0,($a1)
addi $a1,$a1, 4 # move the array over by 1 element
addi $t0,$t0, 1 #i++
j loop
Exit:
jr $ra
#-----------------------------------
printArray:
li $t0,0 #1 = 0;
# cout << message2 <<endl;
la $a0,message2
li $v0,4
syscall
loop1:
bge $t0, $a2,Exit1 #if (i >= 9) Exit theloop;
li $v0,1
lw $a0,0($a1)
syscall
#cout << " " <<endl;
la $a0,next_line
li $v0,4
syscall
addi $a1,$a1, 4
addi $t0,$t0, 1
j loop1
Exit1:
jr $ra
output
====
Enter an integer:
2
Enter an integer:
4
Enter an integer:
6
Enter an integer:
8
Enter an integer:
10
Enter an integer:
12
Enter an integer:
14
Enter an integer:
16
Enter an integer:
18
The array contains the following:
2
4
6
8
10
12
14
16
18
-- program is finished running --