问题描述
我刚开始学习汇编使用GCC编译器来组装我的code的MAC地址。不幸的是,对于学习如何做到这一点,如果你是一个初学者的资源非常有限。我终于设法找到了一些简单的示例code,我可以开始围绕说唱我的头,我知道了装配和正常运行。这里是code:
I am just starting to learn assembly for the mac using the GCC compiler to assemble my code. Unfortunately, there are VERY limited resources for learning how to do this if you are a beginner. I finally managed to find some simple sample code that I could start to rap my head around, and I got it to assemble and run correctly. Here is the code:
.text # start of code indicator.
.globl _main # make the main function visible to the outside.
_main: # actually label this spot as the start of our main function.
push %rbp # save the base pointer to the stack.
mov %rsp, %rbp # put the previous stack pointer into the base pointer.
subl $8, %esp # Balance the stack onto a 16-byte boundary.
movl $0, %eax # Stuff 0 into EAX, which is where result values go.
leave # leave cleans up base and stack pointers again.
ret
的注释说明了在code一些事情(我有点明白什么线2 - 5做),但我不明白这个最意思。我明白了什么寄存器以及每个在这里注册( RBP
, RSP
, ESP 和 EAX
)的用途以及他们是多么大,我也理解(通常情况下)堆栈是什么,但是这仍然会在我头上。谁能告诉我到底是什么这是在干什么?此外,任何人都可以点我一个很好的教程,适合初学者的方向是什么?
The comments explain some things in the code (I kind of understand what lines 2 - 5 do), but I dont understand what most of this means. I do understand the basics of what registers are and what each register here (rbp
, rsp
, esp
and eax
) is used for and how big they are, I also understand (generally) what the stack is, but this is still going over my head. Can anyone tell me exactly what this is doing? Also, could anyone point me in the direction of a good tutorial for beginners?
推荐答案
堆栈是如下的。而在日常生活中栈(外部计算机,我的意思)向上生长,在x86和x86-64的处理器堆栈向下增长。请参阅第86堆栈 一个有用的文章(但同样注意,它使用32与英特尔的语法比特x86汇编,而不是64位X86-64组装与AT&安培; T语法)
The instructions so far are the standard entry sequence (replace $8
according to the stack usage). Wikibooks has a useful article on x86 functions and stack frames (but note again that it uses 32-bit x86 assembly with Intel syntax, not 64-bit x86-64 assembly with AT&T syntax).
然后:
movl $0, %eax
这是显而易见的。商店0到 EAX
。这有什么好做的堆栈。
This is obvious. Store 0 into eax
. This has nothing to do with the stack.
leave
这是等同于 MOV RSP,RBP
然后按流行RBP
。
ret
而这一点,最后,设置裂口
来保存在值[SS:RSP]返回
,有效code指针返回到该过程被称为,并增加了8〜 RSP
。
And this, finally, sets rip
to the value stored at [ss:rsp]
, effective returning the code pointer back to where this procedure was called, and adds 8 to rsp
.
这篇关于这是什么汇编函数序言/结尾code。与RBP / RSP /离开干什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!