.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
上面的代码是我必须转换为MIPS的C ++程序的一部分,到目前为止我已经设法编写了程序并给出了main。到目前为止,我的问题是,每当我尝试做sw返回结果时,我得到“运行时异常在0x00400510:存储地址未在字边界0x00000001上对齐”所以,我一步一步做了一切都很正常当它到达L11时:顺便说一下updatePoint是一个void函数。
add $a0,$a0,$a2
move $s0,$a0
sw $v0,0($s0)
move $s0,$a1
sw $v0,4($s0)
运行第一个sw之后出现错误,之前是好的。在rror寄存器结束之前,$ a0持有1,$ a1持有9,这是我应该返回main的数字;然而,当我试图做sw将结果返回到main时,它总是给我带出边界错误并且我无法弄清楚如果在添加$ a0,$ a0,$ a2寄存器$ a0后有帮助怎么解决它保持00000001,寄存器$ a1包含00000009和$ v0这是返回寄存器包含00000002现在$ v0应该在数组的第一个位置返回1,在数组的第二个位置返回9。任何帮助都是感激不尽的。
更新:供将来参考我通过在每个语句中添加以下4行来解决问题
lw $v0,0($sp) # load base array address
add $v0,$v0,4
lw $v1,0($v0)
move $v1,$a1
sw $v1,0($v0)