0

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
2
  • Ok, so what is the issue? Commented Sep 14, 2013 at 19:05
  • right. sorry. it keeps stopping before the value of $t2 is equal to $t5. basically it runs once and then stops. Commented Sep 14, 2013 at 19:07

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Normally you'd want bne because that's a single machine instruction, while blt is a pseudo-instruction for slt / bnez.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.