MIPS组件对齐对齐n [英] MIPS Assembly Alignment Align n

查看:166
本文介绍了MIPS组件对齐对齐n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

指令.align n在数组中起什么作用?
更具体地说,假设我具有以下代码部分:

What does the directive .align n do in an array?
To be more specific let's say that I have the following part of code:

array: .align 2
       .space 800     

它的重要性是什么,为什么不跳过它并使用

What's its importance and why not just skip it and use

       .space 800

这是学校的作业理论问题.

This is a school's assignment theory question.

推荐答案

直接从MARS帮助工具提示中获取:

Taken directly from MARS helping tooltips:

在指定的字节边界上对齐下一个数据项(0 =字节,1 =半字,2 =字,3 =双精度字)

Align next data item on specified byte boundary (0=byte, 1=halfword, 2=word, 3=double)

考虑此代码

  la $t0, array

.data
  .space 3

array: 
  .space 12

它被组装成

lui $at, 0x1001
ori $t0, $at, 0x0003          #$t0 = 0x10010003

显示array位于地址0x10010003.

showing that array is at address 0x10010003.

使用.align指令:

  la $t0, array

.data
  .space 3

array:
  .align 3 
  .space 12

赠予:

lui $at, 0x1001
ori $t0, $at, 0x0008          #$t0 = 0x10010008

现在地址是0x10010008.

Now the address is 0x10010008.

区别在于,后一个地址在双边界上对齐.
双精度数是8个字节长,地址是8个字节的倍数.

The difference is that the latter address is aligned on double boundary.
A double is 8 bytes long and the address is a multiple of 8 bytes.

对齐方式可提供更好的性能,因为CPU以字为单位读取内存,并且此块从四个地址的倍数开始(0x0、0x4、0x8等).
如果请求一个0x3的字,CPU需要读取0x0的一个字和0x4的一个字并将结果合并.

Alignment gives better performance because the CPU reads memory in chunks of words and this blocks starts at address multiple of four (0x0, 0x4, 0x8, ...).
If a word at 0x3 is requested, the CPU need to read the one at 0x0 and the one at 0x4 and merge the results.

在某些体系结构中明确不支持未对齐的访问.如果我没记错的话,就 data 而言,MIPS并不是那么严格(但它是用于代码的).

Unaligned accesses are explicitly not supported in some architecture. If I recall correctly, MIPS is not such strict when it comes to data (but it is for code).

请注意,.align n在2 n 边界上对齐下一个数据项,或者等效地(作为练习,留给读者阅读)将数据项移至 n 位的低位为零的地址.

Beware that .align n align the next data item on 2n boundary, or equivalently (left as an exercise to the reader) it moves the data item to an address whose lower n bits are zero.

MARS似乎是将标签附加"到下一个空间分配指令(例如.byte.space等). 而不是当前位置计数器.
因此,可以在.align.space指令之间移动标签.

MARS appears, contrary to most assembler, to "attach" labels to the next space allocating directive (e.g. .byte, .space, etc.) and not the current location counter.
Thus the label can be moved between the .align and .space directives.

  .align 2            |    array: 
array:                |      .align 2
  .space 12           |      .space 12 

这篇关于MIPS组件对齐对齐n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆