Assembly函数和被跳转的标签;是否需要8字节对齐?

huangapple go评论45阅读模式
英文:

Assembly functions and labels that are jumped to; is 8-BYTE alignment necessary or not?

问题

In assembly, it's not necessary for function labels or labels within functions to be aligned at an 8-BYTE boundary for jumps. Label alignment is typically not a requirement in assembly language programming. The code you provided in the first example is perfectly fine:

func:
    jmp     .ret1
.ret1: 
    mov     eax, 1
    ret
.ret2: 
    mov     eax, 2
    ret

There's no need to insert ALIGN directives in this context. Alignment requirements are often architecture-specific and can vary, but in general, they are not tied to labels within functions or functions themselves in most assembly languages.

英文:

I have a question about jumping in assembly. Is it necessary for a function label to be aligned at an 8-BYTE boundary when we want to jump to it?
For example:

func:
        jmp     .ret1
 .ret1: mov     eax, 1
        ret
 .ret2: mov     eax, 2
        ret

Here I have a function with 2 labels and I just jumped to the first one. Is it necessary for each label to be aligned at an 8-BYTE boundary?
Is the following necessary:

func:
        jmp     .ret1
   ALIGN 8
 .ret1: mov     eax, 1
        ret
   ALIGN 8
 .ret2: mov     eax, 2
        ret

What about functions? Each function must be aligned at an 8-BYTE boundary or is it not important?

答案1

得分: 3

没有这样的要求。然而,将函数入口点和其他跳转目标对齐到16字节可能会在代码不在µop缓存中时提高性能。但效果相对较小。如果有疑问,请进行基准测试。

英文:

There is no such requirement. However, aligning function entry points and other jump targets to 16 byte may improve performance when the code is not in the µop cache. The effect is fairly minor though. When in doubt, benchmark.

huangapple
  • 本文由 发表于 2023年5月21日 03:35:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定