Arrays in for loop

This topic contains 4 replies, has 2 voices, and was last updated by  Sean 3 years, 10 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #31574

    murock
    Member

    Trying to convert some C into MIPs assembly
    This is the C

    int maximum(int array[], int size)
    {
    int max, i;
    max = array[0];
    for(i=1; i if(array > max)
    max = array
    ;
    return max;
    }

    This is what I have for the MIPS

    .data                                            #data segment

    values: .word 1,16,8,13,5 #Array of 5 words containing values
    # t0=i t1=array function t2=array t3=if condition t4=max t5=array a0=array parameter a1=size
    size: .word 5 #Size of the array

    .text #text segment

    la $t2, values #loads address of array
    lw $a1, size #load array size in $t1
    move $t0,$zero # i = 0
    loop1: sll $t1,$t0,2 # $t1 = i * 4 ,Shift whats in $t0 left by 2 and put into $t1
    add $t2,$a0,$t1 # $t2 =
    # &array

    lw $t5,0($t2) # loads array
    into t5
    blt $t5,$t4, else #If array
    addi $t4,$t5,0 # max=array

    else: addi $t0,$t0,1 # i = i + 1
    slt $t3,$t0,$a1 #IF $t0< $a1 set $t3=1
    # (i < size)
    bne $t3,$zero,loop1 # if $t3=1 branch loop

    # goto loop1
    li $v0, 1 # service 1 is print integer
    add $a0, $t4, $zero # load desired value into argument register $a0,
    syscall

    Any help would be good I’m a complete beginner

    #38269

    Sean
    Moderator

    Please enclose your code within tags - makes it much more readable :-)

    I've not read your code in detail but please let me know what issues you are having with this code. Have you tried working through with a debugger to follow through how the code is operating?

    Sean.

    #38270

    murock
    Member

    Yeah I’m having a problem with

      lw $t5,0($t2)	


    it says something is out of range?

    #38271

    murock
    Member

    Runtime exception at 0x0040001c: address out of range 0x00000000

    #38272

    Sean
    Moderator

    I have taken a look at the code – it seems ok form an initial look.

    Have you tried running the code in a simulator with a debugger attached to identify the issue?

    Sean.

Viewing 5 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic.