简体   繁体   English

MIPS汇编(MARS)中的.align指令

[英].align directive in MIPS assembly (MARS)

I'm doing a project for my university using MIPS assembly on the program called MARS. 我正在使用名为MARS的程序的MIPS汇编为我的大学做一个项目。 I have some issues on working with the .align. 我在使用.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 ". 这是MARS给我的一个错误示例:“运行时异常为0x00400098:地址在双字边界0x1001017c上未对齐”。

The error occures when I try to load a double from the double array called 'v' : ld $f12, ($t1) 当我尝试从称为'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). 但是,双精度数是8个字节,因此索引应该已经乘以8(等效地,左移3)。 That does not match the offset calculation for the other arrays - in general you cannot reuse a scaled index like that. 这与其他数组的偏移量计算不匹配-通常,您无法像这样重复使用缩放索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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