Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

assembly - Nested generic for loops in MIPS

So... I have been trying to make a generic for loop to use for MIPS, but I can′t get it to work... I was thinking about using what I understood it was stacks... but it seems to still be very wrong... Basically trying to do this:

for(i=0; i<10; i++){
   for(j = i; j<10; j++){
      acc = acc + a[j];
   }
}

With this generic for function that does not work:

for:    
    addi    $sp, $sp, -4
    sw  $a1, ($sp)
    addi    $sp, $sp, -4
    sw  $a2, ($sp)
    addi    $t0, $a2, 0 
    la  $s7, ($ra)      
    jr  $s7

counter:
           addi     $t0, $t0, 1     
           jr   $s7     

increment:
    blt     $t0, $a1, counter   
    addi    $sp, $sp, 4

    addi    $sp, $sp, 4
    jr  $ra

Kinda like this:

# for(i=0; i<10; i++)
    addi  $a1, $zero, 9
    la  $a2, ($s0)
    addi    $t3, $zero, 0
    sw  $t3, ($s0)
    jal for 

        sw  $a2, ($s0)
        # for(j = i; j<10; j++)
        addi  $a1, $zero, 9 
        la  $a2, ($s1)
        lw  $t2, ($s0)
        addi    $a2, $t2, 0
        jal for
            # acc = acc + a[j]
            la  $s3, vector         
            add     $t4, $zero, $t1 
            jal     set_address 
            lw  $t4, ($s3)      
            add $t3, $t3, $t4   
    
        # j++
        jal     increment
        
        lw  $a2, ($sp)
        addi    $sp, $sp, 4
        lw  $a1, ($sp)
    
    # i++
    jal     increment

I had a bunch of other comments on this code but they were not in english so I thought it would be less confunsing... Also... I am quite aware I did not follow formalities, however I was not aware of them until now and if I cant get this to work I will have to use a different way to solve this issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think I have fixed what I had done before... Much better and easier to understand now... If anyone needs it feel free to use it.

for(i=0; i<10; i++){
   for(j = i; j<10; j++){
      acc = acc + a[j];
   }
}
for:    
    addi    $sp, $sp, -12
    sw  $a1, 0($sp)
    sw  $a2, 4($sp)
    sw  $ra, 8($sp) 
    jr  $ra

counter:
           lw   $t0, 4($sp)
           addi     $t0, $t0, 1
           sw   $t0, 4($sp)   
           lw   $t2, 8($sp) 
           jr   $t2     

increment:
    lw  $t0, 4($sp)
    lw  $t1, 0($sp)
    blt     $t0, $t1, counter   
    addi    $sp, $sp, 12
    jr  $ra
# for(i=0; i<10; i++)
    addi    $a1, $zero, 9
    addi    $a2, $zero, 0
    jal for 
        
        # for(j = i; j<10; j++)
        addi    $a1, $zero, 9
        lw  $a2, 4($sp)
        jal for     
    
            # acc = acc + a[j]
            la  $t0, vector           
            lw      $t1, ($s1)      
            jal     set_address 
            lw  $t1, ($t0)      
            add $s3, $s3, $t1   
    
    
        # j++
        jal     increment
    
    # i++
    jal     increment

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...