装配函数流程
可能的重复:
汇编函数流程
汇编函数流程
你好,
我正在阅读“从头开始编程”,
如果你不这样做的话不知道这本书是什么,你仍然可以帮助我。
这本书(第4章)有两件事我不明白。
问:我不明白
“movl %ebx, -4(%ebp)
#store current result”的用途。“
当前结果
”是什么意思
在下面代码的标记部分中
,有
“ movl 8(%ebp), %ebx
" 这意味着将 8(%ebp) 保存到 %ebx
但我不明白的原因是
程序员是否想要 8 (%ebp) 保存到 -4(%ebp),
为什么要通过 %ebx
传递 8(%ebp)
?
“movl 8(%ebp), -4(%ebp)
”是否不当?
或者“movl 8(%ebp), %ebx
#put first argument in %eax
”中是否有拼写错误? (我认为%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
Possible Duplicate:
assembly function flow
assembly function flow
hello
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.
Q. 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
little upperside, 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。事实上,处理器无法做到这一点。处理器没有足够的内存读取/写入组件来在同一命令中执行读取和写入操作。因此,为了将值从一个存储点移动到另一个存储点,必须首先将其复制到寄存器中。
在 x86 世界中,我知道可以从源中
push
值,然后将其pop
到目的地。这将有效地将内存复制到内存到内存,但这是一种特殊情况,很可能比您列出的情况慢。Yes it is. In fact, the processor cannot do it. There are not enough memory fetch/write components to the processor to do both a read and a write in the same command. Due to this, in order to move a value from one memory spot to another, it must first be copied into a register.
In the x86 world, I know it would be possible to
push
the value from the source, thepop
it to the destination. This would effectively copy memory to memory to memory, but this is a special case and is most likely slower than the one you listed.