Submit the exercise based on arrays.asm that prompts for an index and prints the
ID: 3713978 • Letter: S
Question
Submit the exercise based on arrays.asm that prompts for an index and prints the array value at that index.
.data
array: .word 10, 77, 12, 8, 40
array2: .word 107, 777, 127, 87, 407
#x = array[2] in java "x" would get the "12"
prompt: .asciiz "Enter the index:"
answer: .asciiz " The value at that index is: "
.text
la $a0, prompt
li $v0, 4 #Print the "prompt" string
syscall
li $v0, 5 #Function 5 - read an integer into $v0
syscall
la $a0, answer
li $v0, 4 #Print the "prompt" string
syscall
la $t1, array #Get the base address of array
lw $a0, ($t1) #Get the value at the address pointed to by $t1
li $v0, 1 #Print integer in $a0
syscall
li $v0, 10
syscall
Explanation / Answer
ScreenShot
-------------------------------------------------------------------------------------------------------------------------
Program
#variable declaration
.data
#array declaration and initialization
array: .word 10, 77, 12, 8, 40
array2: .word 107, 777, 127, 87, 407
#prompts for index and result
prompt: .asciiz "Enter the index:"
answer: .asciiz " The value at that index is: "
#main program
.text
#prompt the user for index
la $a0, prompt
li $v0, 4 #Print the "prompt" string
syscall
#read user entered data
li $v0, 5 #Function 5 - read an integer into $v0
syscall
#move index into t0
move $t0,$v0
#make a register t2=0 for index getting loop
li $t2,0
#Result prompt display
la $a0, answer
li $v0, 4 #Print the "prompt" string
syscall
#load array address
la $t1, array #Get the base address of array
#index check to get data
loop:
#compare index from 0-user entered index
blt $t0,$t2,exit
lw $a0,0($t1) #Get the value at the address pointed to by $t1
addi $t2,$t2,1 #increment t2 value to compare index
addi $t1,$t1,4 #increment array address
j loop #loop until get index
#display array element at the index and exit from the program
exit:
li $v0, 1 #Print integer in $a0
syscall
li $v0,10 #exit the program
syscall