简体   繁体   中英

.align directive in MIPS assembly (MARS)

I'm doing a project for my university using MIPS assembly on the program called MARS. I have some issues on working with the .align. I think i don't understand at all how this directive works.

This is an example of error that MARS gives to me: " Runtime exception at 0x00400098: address not aligned on doubleword boundary 0x1001017c ".

The error occures when I try to load a double from the double array called 'v' : ld $f12, ($t1)

This is my code:

.data
    msg1: .asciiz "### SETTE E MEZZO ###\n\n"
    msg2: .asciiz "carta estratta: "

A:
.align 2
 .word 1,2,3,4,5,6,7,8,9,10,    1,2,3,4,5,6,7,8,9,10,  1,2,3,4,5,6,7,8,9,10,  1,2,3,4,5,6,7,8,9,10 
s:
.align 0
 .byte 'c','c','c','c','c','c','c','c','c','c','q','q','q','q','q','q','q','q','q','q','f','f','f','f','f','f','f','f','f','f','p','p','p','p','p','p','p','p','p','p'

v:
.align 3
.double 1,2,3,4,5,6,7,0.5,0.5,0.5,  1,2,3,4,5,6,7,0.5,0.5,0.5,  1,2,3,4,5,6,7,0.5,0.5,0.5,  1,2,3,4,5,6,7,0.5,0.5,0.5

.text .globl main

main: la $a0, msg1
li $v0, 4 syscall # print stringa titolo

 ### SALVO GLI INDIRIZZI DEGLI ARRAY IN REGISTRI ###
la $s0, A       # $s0 <- &A (indirizzo array numero carte)
la $s1, s       # $s1 <- &s (indirizzo array semi carte)
la $s2, v       # $s2 <- &v (indirizzo array valori carte)

li $s3, 0       # $s3 <- somma carte PLAYER 

jal Player



li $v0, 10      #exit
syscall

Player: #PUSH subu $sp, $sp, 24 #abbasso lo stack pointer di 20 perchè devo salvarmi 5 registri ciascuno da 4 byte (quindi 5*4 = 20 byte) sw $fp, 20($sp) sw $ra, 16($sp) sw $s0, 12($sp) sw $s1, 8($sp) sw $s2, 4($sp) sw $s3, 0($sp) #modifico lo stack pointer in modo che punti alla prima parola del record di attivazione addiu $fp, $sp, 16 #------------------ jal Random move $s4, $v0 # numero casuale in $s4

mul $t0, $s4, 4     # $t0 <- index
add $t1, $s0, $t0   # $t1 <- &A[index]
lw $a0, 0($t1)      #vado a prendere nell'array A una carta a caso in base al numero random che funge da incdice        
li $v0, 1   
syscall         # print del numero della carta
sw $zero, 0($t1)    # A[index] <- 0    in modo che non venga estratto due volte lo stesso numero

add $t1, $s1, $s4   # $t1 <- &s[index]
lb $a0, 0($t1)      #vado a prendere nell'array s il seme
li $v0, 11
syscall 

add $t1, $s2, $t0   # $t1 <- &v[index]  
l.d $f12, ($t1)
li $v0, 3

#POP
lw $s3, 0($sp)
lw $s2, 4($sp)
lw $s1, 8($sp)
lw $s0, 12($sp)
lw $ra, 16($sp)
lw $fp, 20($sp)
addi $sp, $sp, 20
#------------------

jr $ra

Random:

li $v0, 42 li $a1, 40 syscall # estrae pseudo-random number da 0 a 9 move $v0, $a0 # numero casuale in $v0 (VALORE DI RITORNO) jr $ra

What I'm doing wrong? I would appreciate it very much if someone could explain it to me. Thank you in advance.

In this line,

mul $t0, $s4, 4     # $t0 <- index

You have multiplied an index by what's supposed to be a size of an element, in order to get an offset from the start of the array.

However, doubles are 8 bytes, so the index should have been multiplied by 8 (equivalently, shifted left by 3). That does not match the offset calculation for the other arrays - in general you cannot reuse a scaled index like that.

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