[英]Understanding subtraction and multiplication in assembly code
有人可以解釋反匯編代碼中的這些步驟嗎? 我有一個大致的想法,但我仍然感到困惑。 我知道前兩個指令設置了堆棧,而eax將是一個返回值,但僅此而已。
我要尋找的是以下步驟的目的:
push %ebp - base stack frame pointer
mov %esp, %ebp - stack pointer
sub $0x10, %esp - subtracts 16 from ?
mov 0x8(%ebp), %eax - ?
imul 0xc(%ebp), %eax - multiply 12 and ?
mov %eax, -0x4(%ebp) - ?
mov -0x4(%ebp), %eax - puts -0x4(%ebp) not sure what that would be , into eax making it the return value?
leave
ret
; Standard prolog: stack frame setup
push ebp ; save the old frame pointer
mov ebp, esp ; set the frame pointer to the current top of the stack
sub esp, 0x10 ; make space for 16 bytes of local variables
; Do the stuff
mov eax, [ebp+8] ; copy the first parameter in eax
imul eax, [ebp+0xc] ; multiply eax with the second parameter
mov [ebp-4], eax ; move the result to the first local variable
mov eax, [ebp-4] ; move it back to eax (?) => set it as return value
; Standard cdecl epilog - clean up locals & return
leave ; restore the old frame pointer
; same as: mov esp, ebp
; pop ebp
ret ; return
(很抱歉將其更改為Intel表示法,但是AT&T語法對我來說似乎是一團難以理解的混亂,尤其是用於取消引用和偏移量的丑陋表示法1 )
要理解這一點,可以在函數序言之后的x86上的cdecl函數調用中, 輕松查看堆棧通常的樣子:
並記住括號中的表達式是指針解引用操作。
從本質上講,這是(非常幼稚)的翻譯
int multiply(int a, int b) {
// \ \ &b == ebp+12
// \ &a == ebp+8
int c = a*b;
// \ \ multiplication performed in eax
// \ &c == ebp-4
return c;
// \ return value left in eax
}
(使用cdecl調用約定,由調用者負責從堆棧中清除參數)
可能是由禁用優化的編譯器生成的。 一個更緊湊的版本是:
mov eax, [esp+4]
imul eax, [esp+8]
ret
(由於所有操作都可以在沒有局部變量的情況下完成,因此甚至不需要設置堆棧框架)
編輯
剛剛檢查,您的代碼與gcc在-O0
生成的代碼完全匹配,而我的代碼與-O3
處生成的代碼幾乎相同。
記錄:當您看到
displacement(%register, %offset_register, multiplier)
(除了%register
以外的每個組件都是可選的)在AT&T語法中,實際上意味着
[register + displacement + offset_register*multiplier]
括號中的意思是“取存儲在此處的值”。
另外,幾乎所有參數都以AT&T語法交換(在Intel語法中,目標操作數在左側,即mov
讀取類似於賦值mov ebp, esp
=> ebp = esp
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.