我是一名高级程序员,架构对我来说很新,所以我决定在这里阅读有关Assembly的教程:
http://en.wikibooks.org/wiki/X86_Assembly/Print_Version
在教程的下方,有关如何转换Hello World的说明!程序
#include <stdio.h>
int main(void) {
printf("Hello, world!\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
进入等效的汇编代码并生成以下内容:
.text
LC0:
.ascii "Hello, world!\12\0"
.globl _main
_main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
movl %eax, -4(%ebp)
movl -4(%ebp), %eax
call __alloca
call ___main
movl $LC0, (%esp)
call _printf
movl $0, %eax
leave
ret
Run Code Online (Sandbox Code Playgroud)
对于其中一条线,
andl $-16, %esp
Run Code Online (Sandbox Code Playgroud)
解释是:
这段代码"和"ESP与0xFFFFFFF0,使堆栈与下一个最低的16字节边界对齐.对Mingw源代码的检查表明,这可能是出现在"_main"例程中的SIMD指令,它只在对齐的地址上运行.由于我们的例程不包含SIMD指令,因此不需要此行.
我不明白这一点.有人能给我一个解释,说明将堆栈与下一个16字节边界对齐是什么意思以及为什么需要它?如何andl实现这一目标?
所以我正在尝试编写一些x86来为结构分配内存.我的c代码看起来像这样......
struc *uno = malloc(sizeof(struc));
uno->first = 0;
uno->second = 0;
uno->third = 0;
//And the struct
struct struc {
int first;
int second;
int *third;
}
Run Code Online (Sandbox Code Playgroud)
拆卸看起来像......
pushl %ebp
movl %esp, %ebp
subl $40, %esp
movl $12, (%esp)
call malloc
movl %eax, -12(%ebp)
movl -12(%ebp), %eax
movl $0, (%eax)
movl -12(%ebp), %eax
movl $0, 4(%eax)
movl -12(%ebp), %eax
movl $0, 8(%eax)
movl $0, %eax
Run Code Online (Sandbox Code Playgroud)
所以我有几个问题......
1)结构的大小是16,但为什么程序集只显示它分配12?
2)这是什么意思
movl %eax, -12(%ebp)
movl -12(%ebp), %eax
Run Code Online (Sandbox Code Playgroud)
是不是只是将eax的内容放入ebp-12的地址中.那么第二个语句是多余的?
3)当没有其他局部变量或参数被推入堆栈时,为什么esp会减少40?我以为它只需要递减16.
任何帮助表示赞赏,以及我认为您认为相关的任何内容.我对装配很新.谢谢.