-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEs3.asm
28 lines (23 loc) · 1.07 KB
/
Es3.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Write a procedure that, given an array stored in data segment and a number 'n'
# return the n-th element of the array (remember that arrays are '0'-index based)
.data
array: .word 3, 5, 6, 77, 9, 129, 34, 4
result: .word 0
.text
.globl main
main:
la $a0, array # loading array address in convnetional parameter register for the procedure
li $a1, 3 # loading second value
jal procedure # jump for the procedure saving back address in $31 or $ra
move $s0, $v0 # moving the return value of the procedure to the saved register
end:
la $t5, result # loading address where to store the result
sw $s0, 0($t5) # storing the result in memory
li $v0, 10 # calling exit syscall function
syscall
procedure:
li $t0, 4
mul $t0, $a1, $t0 # multipying the input number and 4 to create a shift (every word is 4 bytes, 4*n = (n+1)-th value of memory)
add $t0, $a0, $t0 # adding the shift value to the address to point (n+1)-nth element
lw $v0, 0($t0) # loading the pointed value in the conventional return register
jr $ra # jump back to the instruction address after jal