MIPS procedure not working properly

2
        .data
redData:    .word   0:4
greenData:      .word   0:4
prmpt1:  .asciiz "Enter x-coordinate for red particle (0 to 10):"
prmpt2:  .asciiz "Enter y-coordinate for red particle (0 to 10):"
prmpt3:  .asciiz "Enter x-coordinate for green particle (0 to 10):"
prmpt4:  .asciiz "Enter y-coordinate for green particle (0 to 10):"
prmpt5:  .asciiz "cycle "
prmpt6:  .asciiz "red particle (x,y,xVel,yVel): "
prmpt7:  .asciiz "green particle (x,y,xVel,yVel): "
prmpt8:  .asciiz "Collison: oops, end of simulation!\n"
space:   .asciiz " "
endl:    .asciiz "\n"
# i     $s0
# cycle $s1 = 0
# dist  $s2

.text

main:   li      $s1,0

la      $s3,redData     #  redData[2] = 1 ;
li      $s4,1
sw      $s4,8($s3)
sw      $s4,12($s3)     #  redData[3] = 1 ;
la      $s3,greenData   #  greenData[2] = -1 ;
li      $s4,-1
sw      $s4,8($s3)
sw      $s4,12($s3)     #  greenData[3] = -1 ;

la      $a0,prmpt1      #  cout << prmpt1 ;
li      $v0,4
syscall
la      $s3,redData
li      $v0,5           #  cin >> redData[0] ;
syscall
sw      $v0,($s3)
la      $a0,prmpt2      #  cout << prmpt2 ;
li      $v0,4
syscall
li      $v0,5           #  cin >> redData[1] ;
syscall
sw      $v0,4($s3)
la      $a0,prmpt3      #  cout << prmpt3 ;
li      $v0,4
syscall
la      $s3,greenData   #  cin >> greenData[0] ;
li      $v0,5
syscall
sw      $v0,($s3)
la      $a0,prmpt4      #  cout << prmpt4 ;
li      $v0,4
syscall
li      $v0,5           #  cin >> greenData[1] ;
syscall
sw      $v0,4($s3)

loop:                           #  do {
la      $a0,prmpt5      #    cout << "cycle " << cycle << endl ;
li      $v0,4
syscall
move    $a0,$s1
li      $v0,1
syscall
la      $a0,endl
li      $v0,4
syscall
la      $a0,prmpt6      #    cout << "red particle (x,y,xVel,yVel): "
li      $v0,4
syscall
la      $s3, redData
lw      $a0,($s3)       #       << redData[0]
li      $v0,1
syscall
la      $a0,space       #       << " "
li      $v0,4
syscall
lw      $a0,4($s3)      #       << redData[1]
li      $v0,1
syscall
la      $a0,space       #       << " "
li      $v0,4
syscall
lw      $a0,8($s3)      #       << redData[2]
li      $v0,1
syscall
la      $a0,space       #       << " "
li      $v0,4
syscall
lw      $a0,12($s3)     #       << redData[3]
li      $v0,1
syscall
la      $a0,endl        #       << endl ;
li      $v0,4
syscall
la      $a0,prmpt7      #    
li      $v0,4
syscall
la      $s3, greenData
lw      $a0,($s3)       #       << greenData[0]
li      $v0,1
syscall
la      $a0,space       #       << " "
li      $v0,4
syscall
lw      $a0,4($s3)      #       << greenData[1]
li      $v0,1
syscall
la      $a0,space       #       << " "
li      $v0,4
syscall
lw      $a0,8($s3)      #       << greenData[2]
li      $v0,1
syscall
la      $a0,space       #       << " "
li      $v0,4
syscall
lw      $a0,12($s3)     #       << greenData[3]
li      $v0,1
syscall
la      $a0,endl        #       << endl ;
li      $v0,4
syscall
la      $a0,endl        #       << endl ;
li      $v0,4
syscall

la      $a0,redData     #    updatePoint(redData) ;
jal     updatePoint
la      $a0,greenData   #    updatePoint(greenData) ;
jal     updatePoint

la      $s3,redData     #    dist = findDistance(redData[0],
lw      $a0,($s3)       #       redData[1], greenData[0],
lw      $a1,4($s3)      #       greenData[1]) ;
la      $s4,greenData
lw      $a2,($s4)
lw      $a3,4($s4)
jal     findDistance
move    $s2,$v0
add     $s1,$s1,1       #    cycle++ ;
ble     $s2,2,exit      #  } while ((dist > 2) && (cycle < 10)) ;
blt     $s1,10,loop
exit:
bgt     $s2,2,end       #  if (dist <= 2) {
la      $a0,prmpt8      #    cout << prmpt8 ;
li      $v0,4
syscall
la      $a0,prmpt6      #    cout << "red particle (x,y,xVel,yVel): "
li      $v0,4
syscall
la      $s3, redData
lw      $a0,($s3)       #       << redData[0]
li      $v0,1
syscall
la      $a0,space       #       << " "
li      $v0,4
syscall
lw      $a0,4($s3)      #       << redData[1]
li      $v0,1
syscall
la      $a0,space       #       << " "
li      $v0,4
syscall
lw      $a0,8($s3)      #       << redData[2]
li      $v0,1
syscall
la      $a0,space       #       << " "
li      $v0,4
syscall
lw      $a0,12($s3)     #       << redData[3]
li      $v0,1
syscall
la      $a0,endl        #       << endl ;
li      $v0,4
syscall
la      $a0,prmpt7     
li      $v0,4
syscall
la      $s3, greenData
lw      $a0,($s3)       #       << greenData[0]
li      $v0,1
syscall
la      $a0,space       #       << " "
li      $v0,4
syscall
lw      $a0,4($s3)      #       << greenData[1]
li      $v0,1
syscall
la      $a0,space       #       << " "
li      $v0,4
syscall
lw      $a0,8($s3)      #       << greenData[2]
li      $v0,1
syscall
la      $a0,space       #       << " "
li      $v0,4
syscall
lw      $a0,12($s3)     #       << greenData[3]
li      $v0,1
syscall
la      $a0,endl        #       << endl ;
li      $v0,4
syscall
la      $a0,endl        #       << endl ;
li      $v0,4
syscall
end:    li      $v0,10          #  }
syscall                 #}

 updatePoint:    addi   $sp,$sp,-20
         sw      $a0,0($sp)  #holds the array 
         sw      $ra,4($sp)
         sw      $s2,8($sp)
         sw      $s1,12($sp)
         sw      $s0,16($sp)

         move    $s2,$a0   #makes a copy of the array
         lw      $s0,0($s2)
         move    $a0,$s0    
         lw      $s0,4($s2)
         move    $a1,$s0                 
         li      $a2,0
         li      $a3,-1
         jal     findDistance     

         move     $s1,$v0      
         bgt      $s1,0,L8  
         lw       $s0,8($s2)    
         move     $a2,$s0               
         bge      $a2,0,L8      
         sub      $a2,$0,$a2
         move     $s2,$a2
         sw       $v0,8($s2)       #I have to store to memory use sw
                                   #not sure if this works
  L8:           
        lw      $s0,0($s2)
        move    $a0,$s0    #a0=0
         lw      $s0,4($s2)
         move    $a1,$s0   #a                
         li      $a2,10
         li      $a3,-1
         jal     findDistance    

         move     $s1,$v0      
         bgt      $s1,0,L9  # (distance < 1)
         lw       $s0,8($s2)    #s1=2
         move     $a2,$s0   
         ble     $a2,0,L9       
         sub     $a2,$0,$a2
         move    $s2, $a2
         sw      $v0,8($s2)
   L9:           
         lw      $s0,0($s2)
         move    $a0,$s0    #a0=0
         lw      $s0,4($s2)
         move    $a1,$s0   #a                
         li      $a2,-1
         li      $a3,0
         jal     findDistance     

         move     $s1,$v0      
         bgt      $s1,0,L10 
         lw       $s0,12($s2)   
         move     $a3,$s0   
         bge     $a3,0,L10      
         sub     $a3,$0,$a3
         move     $v0,$a3
         move    $s2,$a3
         sw      $v0,12($s2)

L10:     move    $a0,$s2 
         lw      $s0,0($s2)
         move    $a0,$s0    #a0=0
         lw      $s0,4($s2)
         move    $a1,$s0   #a                
         li      $a2,-1
         li      $a3,10
         jal     findDistance    

         move     $s1,$v0     
         bgt      $s1,0,L11 
         lw       $s0,12($s2)   #s1=2
         move     $a3,$s0   
         ble      $a3,0,L11                  
         sub     $a3,$0,$a3
         move    $s2,$a3
         sw      $v0,12($s2)

L11:        
        lw      $s0,0($s2)
        move    $a0,$s0    #a0=0
        lw      $s0,4($s2)
        move    $a1,$s0   #a  
        lw      $s0,8($s2)
        move    $a2,$s0
        lw      $s0,12($s2)
        move    $a3,$s0              
        add    $a0,$a0,$a2
        add     $a1,$a1,$a3
        move    $s0,$a0          
        sw      $v0,0($s0)           
        move    $s0,$a1
        sw      $v0,4($s0)



         lw      $a0,0($sp)
         lw      $ra,4($sp)
         lw      $s2,8($sp)
         lw      $s1,12($sp)
         lw      $s0,16($sp)
         addi    $sp,$sp,20
         jr      $ra

         lw      $a0,0($sp)
         lw      $ra,4($sp)
         lw      $s2,8($sp)
         lw      $s1,12($sp)
         lw      $s0,16($sp)
         addi    $sp,$sp,20
         jr      $ra


findDistance:
#arg0=a0
#arg1=a1...         

    li     $t0,0     #distX         
    li     $t1,0    #distY
    sub    $t0,$a0,$a2
    bge    $t0,0,next
    sub    $t0,$0,$t0


next:sub    $t1,$a1,$a3 #distY = arg1 - arg3;

    bge    $t1,$0,if3           #if (distY < 0)
    sub    $t1,$0,$t1           #distY = - distY;


 if3:
    blt $a0,0,orr
    bge $a2,0,elseif

orr:
    move    $v0,$t1
    j        out

 elseif:        
    blt      $a1,0,orr2
    bge      $a3,0,else

orr2:       move     $v0,$t0
    j    out

else:       add     $t0,$t0,$t1
    move      $v0,$t0       

out:        
    jr       $ra

the above code is part of a C++ program that I have to convert to MIPS and so far I have manage to write the procedures and main is given. my problem so far is that every time I tried to do sw to return the result I get "Runtime exception at 0x00400510: store address not aligned on word boundary 0x00000001" So, I did a step by step and everything works fine up to the point when it gets to L11: by the way updatePoint is a void function.

add    $a0,$a0,$a2
move    $s0,$a0          
sw      $v0,0($s0)           
move    $s0,$a1
sw      $v0,4($s0)

the error appear after running the first sw and before that is good. at the end before the rror register $a0 holds 1 and $a1 holds 9 which are the numbers that I am supposed to return to main; however when I tried to do sw to return the result to main it always give me the out of boundary erro and I can't figure out how to do solve it if it helps after add $a0,$a0,$a2 register $a0 holds 00000001, register $a1 contains 00000009 and $v0 which is the returning register contains 00000002 now $v0 supposed to return 1 in the first position of the array and 9 in the second position of the array. anyhelp is gratefully apreciate.

Update: for future reference I solve the problem by adding the following 4 lines to each statement

         lw       $v0,0($sp)      # load base array address
         add      $v0,$v0,4
         lw       $v1,0($v0)
         move     $v1,$a1
         sw       $v1,0($v0)
assembly
mips
mars-simulator
asked on Stack Overflow Oct 16, 2016 by HackCSC85 • edited Apr 30, 2021 by Peter Cordes

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0