2014-07-14 87 views
3

我正在通過比較一個c程序和它的程序集等價來學習程序集。程序集「mov」指令

這是代碼。

.file "ex3.c" 
.section .rodata 
.LC0: 
    .string "I am %d years old.\n" 
.LC1: 
    .string "I am %d inches tall.\n" 
    .text 
    .globl main 
    .type main, @function 
main: 
    pushl %ebp //establish stack frame// 
    movl %esp, %ebp //move esp into ebp, all contents saved down stack// 
    andl $-16, %esp //16 from esp for local var space// 
    subl $32, %esp//stack frame reserving - 32 bytes// 
    movl $10, 24(%esp) 
    movl $72, 28(%esp) 
    movl 24(%esp), %eax 
    movl %eax, 4(%esp) 
    movl $.LC0, (%esp) 
    call printf 
    movl 28(%esp), %eax 
    movl %eax, 4(%esp) 
    movl $.LC1, (%esp) 
    call printf 
    movl $0, %eax 
    leave 
    ret 
    .size main, .-main 
    .ident "GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2" 
    .section .note.GNU-stack,"",@progbits 

對於此行:

movl $10, 24(%esp) 

如果我沒有理解它正確跟它移動值爲10到ESP寄存器。但是24是幹什麼的?我不認爲它移入ESP,因爲要移動的「$」(我認爲)

+0

「將10的值移入esp寄存器」 - 不。 esp周圍的括號表示間接。 24是位移,又名偏移量。 –

+1

在Intel語法中,它會更容易理解:'mov dword ptr [esp + 24],10' –

回答

7
movl $10,24(%esp) 

表示的值是指:將一個文字小數-10長(4字節)爲4字節內存位置開始於(esp寄存器加上十進制數24)指向的地址 - 基本上它是一個局部變量。

5

換句話說movl $10,24(%esp)

裝置:負載10*(esp + 24)

在C即等於:

*(unsigned long *)(myptr + 24) = 10;

其中myptr取與esp寄存器的值。