2011-03-21 62 views
0

可能重複:
assembly function flow裝配功能流

彙編函數流

你好

我 「從地上規劃了」 讀一

如果你不知道這本書是什麼,你仍然可以幫助我。

在這本書(第4章)中有兩件事我不明白。

問:我不明白

  1. 什麼"movl %ebx, -4(%ebp) #store當前結果」的。

  2. 又是什麼 「在標記部分current result」 是指

下面的代碼

稍微向上,在那裏我小號

movl 8(%ebp), %ebx」,這意味着節省8(%ebp) to %ebx

但爲什麼我不明白的是

如果程序員想8(%EBP)保存到-4(%EBP)的原因,

爲什麼要8(%ebp)通過%ebx

是「movl 8(%ebp), -4(%ebp)」有什麼不對?

或者是否有任何輸入錯誤「movl 8(%ebp), %ebx%eax #put first argument」? (我覺得%EBX應該是%eax中或反之亦然)

#PURPOSE: Program to illustrate how functions work 

# This program will compute the value of 

# 2^3 + 5^2 

# 

#Everything in the main program is stored in registers, 

#so the data section doesn’t have anything. 

.section .data 

.section .text 

.globl _start 

_start: 

pushl $3 #push second argument 

pushl $2 #push first argument 

call power #call the function 

addl $8, %esp #move the stack pointer back 

pushl %eax #save the first answer before 

#calling the next function 

pushl $2 #push second argument 

pushl $5 #push first argument 



call power #call the function 

addl $8, %esp #move the stack pointer back 

popl %ebx #The second answer is already 

#in %eax. We saved the 

#first answer onto the stack, 

#so now we can just pop it 

#out into %ebx 

addl %eax, %ebx #add them together 

#the result is in %ebx 

movl $1, %eax #exit (%ebx is returned) 

int $0x80 

#PURPOSE: This function is used to compute 

# the value of a number raised to 

# a power. 

# 

#INPUT: First argument - the base number 

# Second argument - the power to 

# raise it to 

# 

#OUTPUT: Will give the result as a return value 

# 

#NOTES: The power must be 1 or greater 

# 

#VARIABLES: 

# %ebx - holds the base number 

# %ecx - holds the power 

# 

# -4(%ebp) - holds the current result 

# 

# %eax is used for temporary storage 

# 

.type power, @function 

power: 

pushl %ebp #save old base pointer 

movl %esp, %ebp #make stack pointer the base pointer 

subl $4, %esp #get room for our local storage 

########################################## 

movl 8(%ebp), %ebx #put first argument in %eax 

movl 12(%ebp), %ecx #put second argument in %ecx 

movl %ebx, -4(%ebp) #store current result 

########################################## 

power_loop_start: 

cmpl $1, %ecx #if the power is 1, we are done 

je end_power 

movl -4(%ebp), %eax #move the current result into %eax 

imull %ebx, %eax #multiply the current result by 

#the base number 

movl %eax, -4(%ebp) #store the current result 

decl %ecx #decrease the power 

jmp power_loop_start #run for the next power 

end_power: 

movl -4(%ebp), %eax #return value goes in %eax 

movl %ebp, %esp #restore the stack pointer 

popl %ebp #restore the base pointer 

ret 
+0

看起來是一個交叉帖子? HTTP://計算器。com/questions/5373134/assembly-function-flow – 2011-03-21 02:41:35

+0

哦對不起我不是故意做調皮的東西 我以爲每個站點都有不同的訪問者 – 2011-03-21 02:59:22

回答

0

是 「MOVL 8(%EBP)-4(%EBP)」 akward的

是的。事實上,處理器無法做到這一點。處理器沒有足夠的內存讀取/寫入組件來執行相同命令中的讀取和寫入操作。因此,爲了將一個值從一個內存點移動到另一個內存點,必須先將其複製到一個寄存器中。

在x86世界中,我知道它有可能以push的值來源,即pop它到達目的地。這將有效地將內存複製到內存中,但這是一種特殊情況,並且很可能比列出的內存慢。

+0

這個問題是一個確切的重複。不要打擾。 – karlphillip 2011-03-22 16:34:32