Skip to content

02

We cannot store an immediate value into memory.

Sum of first \(n\) numbers

.text
main:

addi $t0, $zero, 5 ## n
add $t1, $zero, 0 ## sum
add $t2, $zero, 1 ## i

repeat:
bgt $t2, $t0, display
add $t1, $t1, $t2
addi $t2, $t2, 1
j repeat

display:
add $v0, $zero, 1
add $a0, $zero, $t1
syscall

#exit
add $v0, $zero, 10
syscall

Array Memory

.data
array .word 10, 20, 30

.text
la $t0, array ## array 

addi $t1, $zero, 3 ## n = 3
addi $t2, $zero, 0 ## i = 0

loop: ## for (i = 0; i < n; i++)
## i >= n (exit if i = n)
    beq $t2, $t1, exit

    ## code for displaying 1 element
    addi $a0, $zero, 3
    addi $v0, $zero, 4
    syscall

exit:
  1. Initialize array
  2. Get address of array
Last Updated: 2023-01-25 ; Contributors: AhmedThahir

Comments