汇编函数流 [英] assembly function flow
问题描述
我读了从地上爬起来编程,如果你不知道这本书是什么,你仍然可以帮助我。
在这本书中(第4章)有2个东西,我不明白:
- 什么
MOVL%EBX,-4(%EBP)#store当前结果
为 - 和什么是当前结果的意思
在下面的code标记部分,有:
MOVL 8(EBP%),EBX%
这意味着保存 8(%EBP)
到%EBX
,但之所以我不明白是,如果程序员希望 8(%EBP)
保存到 -4(%EBP)
,何必 8(%EBP)
到%EBX
?是 MOVL 8(EBP%)-4(%EBP)
akward的?或者有没有在 MOVL 8(EBP%)的错字,EBX%在#put EAX%第一个参数
?
(我认为%EBX
应%EAX
或反之亦然)
#PURPOSE:程序来说明如何工作
#这一方案将计算的价值
#2 ^ 3 + 5 ^ 2
#Everything在主程序存储在寄存器
#so数据部分并没有什么。.section伪。数据
.section伪的.text
.globl _start_开始:pushl $ 3 #push第二个参数
pushl $ 2 #push第一个参数
调用功率#CALL功能
ADDL $ 8%ESP #move堆栈指针返回
pushl%eax中#save第一个答案之前#calling下一功能pushl $ 2 #push第二个参数
pushl $ 5 #push第一个参数调用功率#CALL功能
ADDL $ 8%ESP #move堆栈指针返回
popl%EBX#系统第二个答案已经#in%eax中。我们保存了
#first回答压入堆栈,
#so现在我们只是弹出它
#out到%EBXADDL%EAX,EBX%#将它们放在一起
#系统结果是EBX%MOVL $ 1,%eax中#exit(返回%EBX)
INT 0x80的$#PURPOSE:该函数用于计算
#数字的值升高到
#电源。#INPUT:第一个参数 - 基数
#第二个参数 - 电源
#提高到
#
#OUTPUT:会给结果作为返回值
#
#NOTES:功率必须大于或等于1
#
#VARIABLES:
#%EBX - 持有基数
#%ECX - 持有的力量
#
#-4(%EBP) - 保存当前结果
#
#%eax中用于临时存储
#.TYPE功率,@function
功率:
pushl%ebp的#save老基指针
MOVL%ESP,EBP%了#make堆栈指针的基址指针
subl $ 4%ESP#获取空间,我们的本地存储
##########################################MOVL 8(EBP%),EBX%在#put EAX%第一个参数
MOVL 12(%EBP),ECX%第二#put在ECX%论证
MOVL%EBX,-4(%EBP)#store当前结果##########################################power_loop_start:
CMPL $ 1,ECX%#如果功率为1,我们已经完成
JE end_power
MOVL -4(%EBP),%eax中#move当前结果到EAX%
imull%EBX,EAX%的#multiply当前的结果通过#系统基数
MOVL%EAX,-4(%EBP)#store当前结果
DECL%ECX #decrease电源
JMP power_loop_start #run下一个电源end_power:
MOVL -4(%EBP),%eax中#return价值远远在%eax中
MOVL%EBP,ESP%的#restore堆栈指针
popl%EBP #restore基指针
RET
我相信这一点:
MOVL 8(EBP%),EBX%在#put EAX%第一个参数
是一个错字,它确实应该:
MOVL 8(EBP%),EBX%在#put%ebx中第一个参数
和你注意到没有,以后code是正确的:
MOVL%EBX,-4(%EBP)#store当前结果
在最后,笔者也可以使用%EAX
此操作,以及(而不是%EBX
),没有理由他不应该,因为它不会改变程序的。
但是注释可以是更加清晰,我认为这是一个错字,以及。 本地堆栈帧
上#storing第一个参数:在这一点上,如果说这将是更好的。
标签power_loop_start 使用这个变量,并暂时将其存储在%EAX
快速操作,然后将其放回在同一位置堆栈上下一个循环:
MOVL%EAX,-4(%EBP)#store当前结果
DECL%ECX #decrease电源
JMP power_loop_start #run下一个电源
I am reading a "programming from the ground up", if you don't know what this book is, you still can help me.
In this book(chapter 4) there are 2 things that I don't understand:
- what
movl %ebx, -4(%ebp) #store current result
for. - and what does "current result" means
in marked section in the code below, there is:
movl 8(%ebp), %ebx
which means save 8(%ebp)
to %ebx
, but the reason why I don't understand is, if the programmer want 8(%ebp)
to save to -4(%ebp)
, why should 8(%ebp)
be passed through %ebx
? Is "movl 8(%ebp), -4(%ebp)
" akward? Or is there any typo in movl 8(%ebp), %ebx #put first argument in %eax
?
(I think %ebx
should be %eax
or vice versa)
#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
I believe that this:
movl 8(%ebp), %ebx #put first argument in %eax
was a typo, and it should really be:
movl 8(%ebp), %ebx #put first argument in %ebx
and if you noticed, later the code is correct:
movl %ebx, -4(%ebp) #store current result
In the end, the author could have used %eax
for this operation as well (instead of %ebx
), there's no reason why he shouldn't since it wouldn't change the program at all.
But the comment could be a lot clearer and I believe that this is a typo as well. At this point, it would be better if it said: #storing 1st argument on the local stack frame
.
label power_loop_start uses that variable and temporarily stores it in %eax
for quick operations and then place it back on the same location on the stack for the next loop:
movl %eax, -4(%ebp) #store the current result
decl %ecx #decrease the power
jmp power_loop_start #run for the next power
这篇关于汇编函数流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!