In: Computer Science
Write a MIPS assembly language program that implements the following pseudo-code operation:
result = x + y – z + A[j]
x and y should be in reserved memory words using the .word directive and labeled as x and y. Initialize x=10 and y=200.
Read in z from the console. Input the value -8. This is the value for z, not for –z. Store this value in memory with the label z. To begin, you could just initialize z to a value using .word directive and test your code.
Read in j from the console. The value of j should be in the range [0, 3]. Input the value 2. You do not need to check j for the proper range on input.
Reserve 4 words in memory for A with the label A. Assume A is referenced starting at A[0]. Initialize these 4 words to: 4, 9, 15, 20. E.g.,:
A: .word 4, 9, 15, 20
Which number is A[j]? In order to get an element, you will need the load address instruction, which loads the address of a label into a register. E.g., to load the starting address of label A, A[0], into $t0, you would do the following:
la $t0, A
You can then use $t0 as an address in the normal way for an lw or sw instruction, as needed.
Store the final result in a reserved memory word labeled result.
Your final data section might look like this:
.data
x: .word 10
y: .word 200
z: .word 0
A: .word 4, 9, 15, 20
result: .word 0
prf: .asciiz "The result is: " #String for printing
crlf: .asciiz "\n" #For printing a cr/lf
Output the result to the console prefixed by a string that reads (without the quotes):
"The result is: "
The result should follow this string on the same line. E.g., the output to the console might be:
The result is: 5768
Check your data segment to be sure values are stored appropriately. Only the memory locations for z and result should be changed from initial values.
MIPS PROGRAM :-
.data
x: .word 10
y: .word 200
z: .word 0
A: .word 4, 9, 15, 20
result: .word 0
prf: .asciiz "The result is: " #String for printing
crlf: .asciiz "\n" #For printing a cr/lf
.text
li $v0,5
syscall
move $t1,$v0 #reading value of z
from console
la $t0,z
sw $t1,0($t0) #storing the input
value in z memory space
li $v0,5
syscall
move $t1,$v0 #reading value of j
from console
sll $t1,$t1,#2 #calculating address
offset according to index
la $t0,A #loading
starting address of A
add $t2, $t0, $t1 #calculating address of
A[j]
lw $t3, 0($t2) #A[j]
li $t1, 0
la $t0, A
lw $t2, 0($t0) #A
add $t1, $t1, $t2 #result_val = A
la $t0, B
lw $t2, 0($t0) #B
add $t1, $t1, $t2 #result_val = A + B
la $t0, Z
lw $t2, 0($t0) #Z
sub $t1, $t1, $t2 #result_val = A + B - Z
add $t1, $t1, $t3 #result_val = A + B - Z + A[j]
la $t0, result
sw $t1, 0($t0) #storing the
resultant value in result memory space
li $v0,4
la $a0,result
syscall
li $v0,1
move $a0,$t1
syscall
li $v0,10
#exit
syscall