简体   繁体   中英

Error Accessing Array in MIPS assembly

I made this code for finding LCM of two numbers. It is the starting chunk which is having problems. I tried to find the problem but couldn't figure it out. It is giving me error of unaligned address and other exceptions when I try to Load word or Store word. Here is the code:

.data

user: .asciiz "enter first number\n"
user2: .asciiz "enter second number\n"

array1: .space 500
array2: .space 500
array3: .space 500

.text

main:

la $a0,user
li $v0,4
syscall

li $v0,5
syscall

move $s0,$v0

la $a0,user2
li $v0,4
syscall

li $v0,5
syscall

move $s1,$v0

li $t0,0
li $t1,0
li $t2,2    
li $t3,3
li $t4,0
li $t5,5
li $t6,7
li $t7,0
li $t8,0
li $t9,0
li $s8,0

la $t8,array1
la $t9,array2

j Loop1



Loop1:

div $s0,$t2

mflo $s2
mfhi $s3 # remainder

beq $s2,1,Loop2

xor $s5,$s3,$0

beq $s5,1,Odd3

add $t4,$t7,$t8

sw $t2,0($t4)  # error

addi $t7,$t7,4


j Loop1

Regards

If it complains about unaligned address, then you should go look why it's unaligned. Assemblers are typically smart enough to align data as appropriate, but you are using the .space directive which doesn't have any type (and hence alignment) information. By chance your strings make the arrays unaligned. You can fix this by manually adding a .align 2 directive before array1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM