What does 0x4 do in “movl $0x2d, 0x4(%esp)”?

♀尐吖头ヾ 提交于 2019-12-05 17:36:29

问题


I am looking into assembly code generated by GCC. But I don't understand:

movl $0x2d, 0x4(%esp)

In the second operand, what does 0x4 stands for? offset address? And what the use of register EAX?


回答1:


movl $0x2d, 0x4(%esp) means to take the current value of the stack pointer (%esp), add 4 (0x4) then store the long (32-bit) value 0x2d into that location.

The eax register is one of the general purpose 32-bit registers. x86 architecture specifies the following 32-bit registers:

eax  Accumulator Register
ebx  Base Register
ecx  Counter Register
edx  Data Register
esi  Source Index
edi  Destination Index
ebp  Base Pointer
esp  Stack Pointer

and the names and purposes of some of then harken back to the days of the Intel 8080.

This page gives a good overview on the Intel-type registers. The first four of those in the above list can also be accessed as a 16-bit or two 8-bit values as well. For example:

3322222222221111111111
10987654321098765432109876543210
<-             eax            ->
                <-     ax     ->
                <- ah -><- al ->

The pointer and index registers do not allow use of 8-bit parts but you can have, for example, the 16-bit bp.




回答2:


0x4(%esp) means *(%esp + 4) where * mean dereferencing.

The statement means store the immediate value 0x2d into some local variable occupying the 4th offset on the stack.

(The code you've shown is in AT&T syntax. In Intel syntax it would be mov [esp, 4], 2dh)




回答3:


0x4 in the second operand is an offset from the value of the register in the parens. EAX is a general purpose register used for assembly coding (computations, storing temporary values, etc.) formally it's called "Accumulator register" but that's more historic than relevant.

You can read this page about the x86 architecture. Most relevant to your question are the sections on Addressing modes and General purpose registers




回答4:


GCC assembly operands follow a byte (b), word (w), long (l) and so on such as :

movb
movw
movl

Registers are prefixed with a percentage sign (%).

Constants are prefixed with a dollar sign ($).

In the above example in your question that means the 4th offset from the stack pointer (esp).

Hope this helps, Best regards, Tom.




回答5:


You're accessing something four bytes removed from where the stack pointer resides. In GCC this indicates a parameter (I think -- positive offset is parameters and negative is local variables if I remember correctly). You're writing, in other words, the value 0x2D into a parameter. If you gave more context I could probably tell you what was going on in the whole procedure.



来源:https://stackoverflow.com/questions/2386684/what-does-0x4-do-in-movl-0x2d-0x4esp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!