user2779884
user2779884

Reputation: 1

while loop with an array in mips

Consider the following c or java-like code: int i = 0; int x = 5; int A[10]; declare an array of integers with 10 element while (i < 10) { A[i] = i+x; i++; } Write a MIPS program to implement the program. Use as few instructions as possible.

I can't seem to figure this out. Heres what i have done.

# i=0, x=5. Array is 10 elements. (While i<10, A[i]=i+x, i++.)
# Array should be [5,6,7,8,9,10,11,12,13,14]
    .data
intgrs: .word 0:10  # array of 10 elements to contain integers
size:   .word 10    #size of array
    .text
    .globl main
main:
    la $t0, intgrs  # load address or array
    la $t5, size    # load address of size variable
    lw $t5, 0($t5)  #load array size
    li $t2, 0   # i=0
    li $t6, 5   # x=5
loop:
    add $t4, $t2, $t6 # i+x
    sw $t4, 0($t0)  # A[0] = 0+5 = 5
    add $t2, $t2, 1 # i++
    beq $t2, $t5, loop
exit:   li $v0, 10  # exit system call
    syscall

Upvotes: 0

Views: 2585

Answers (1)

Konrad Lindenbach
Konrad Lindenbach

Reputation: 4961

Change beq $t2, $t5, loop to blt $t2, $t5, loop.

Upvotes: 1

Related Questions