Function 装配功能流程

Function 装配功能流程,function,assembly,flow,Function,Assembly,Flow,我正在从头读一本编程书,如果你不知道这本书是什么,你仍然可以帮助我 在本书第4章中,有两件事我不明白: 什么movl%ebx,-4%ebp存储当前结果。 目前的结果意味着什么 在下面代码中标记的部分中,有: movl 8(%ebp), %ebx 这意味着将8%ebp保存到%ebx,但我不明白的原因是,如果程序员希望将8%ebp保存到-4%ebp,为什么要将8%ebp传递到%ebx?movl是否为8%ebp,-4%ebp?或者在movl 8%ebp,%ebx中是否有任何输入错误,请将第一个参数放

我正在从头读一本编程书,如果你不知道这本书是什么,你仍然可以帮助我

在本书第4章中,有两件事我不明白:

什么movl%ebx,-4%ebp存储当前结果。 目前的结果意味着什么 在下面代码中标记的部分中,有:

movl 8(%ebp), %ebx
这意味着将8%ebp保存到%ebx,但我不明白的原因是,如果程序员希望将8%ebp保存到-4%ebp,为什么要将8%ebp传递到%ebx?movl是否为8%ebp,-4%ebp?或者在movl 8%ebp,%ebx中是否有任何输入错误,请将第一个参数放在%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

许多汇编操作码只接受一个源或目标内存操作数。这可能解释了为什么从内存到内存的移动是通过%ebx完成的。

许多汇编操作码只接受一个源或目标内存操作数。这可能解释了为什么从内存到内存的移动是通过%ebx完成的。

正如Greg所暗示的,x86与大多数主流体系结构一样,没有将数据从内存复制到内存的指令[1]。因此,必须使用单独的加载和存储来复制数据。首先将源内存中的数据加载到寄存器中,然后将该寄存器中的数据存储到目标内存中。这就是这里发生的一切

[1] 我知道,我知道,但让我们把rep-mov排除在外,让事情变得简单。

正如格雷格暗示的那样,x86与大多数主流体系结构一样,没有将数据从内存复制到内存的指令[1]。因此,必须使用单独的加载和存储来复制数据。首先将源内存中的数据加载到寄存器中,然后将该寄存器中的数据存储到目标内存中。这就是这里发生的一切

[1] 我知道,我知道,但让我们把rep Mov排除在外,让事情变得简单。

我相信:

 movl 8(%ebp), %ebx #put first argument in %eax  
是个打字错误,应该是:

 movl 8(%ebp), %ebx #put first argument in %ebx  
如果您注意到,稍后代码是正确的:

 movl %ebx, -4(%ebp) #store current result
最后,作者本可以在这个操作中使用%eax而不是%ebx,他没有理由不这样做,因为它根本不会改变程序

但评论可能会更清楚,我相信这也是一个打字错误。在这一点上,最好是这样说:在本地堆栈帧上存储第一个参数

标签power\u loop\u start使用该变量并将其临时存储在%eax中以进行快速操作,然后将其放回堆栈上的同一位置以进行下一个循环:

 movl %eax, -4(%ebp)   #store the current result
 decl %ecx             #decrease the power
 jmp  power_loop_start #run for the next power
我认为:

 movl 8(%ebp), %ebx #put first argument in %eax  
是个打字错误,应该是:

 movl 8(%ebp), %ebx #put first argument in %ebx  
如果您注意到,稍后代码是正确的:

 movl %ebx, -4(%ebp) #store current result
最后,作者本可以在这个操作中使用%eax而不是%ebx,他没有理由不这样做,因为它根本不会改变程序

但评论可能会更清楚,我相信这也是一个打字错误。在这一点上,最好是这样说:在本地堆栈帧上存储第一个参数

标签power\u loop\u start使用该变量并将其临时存储在%eax中以进行快速操作,然后将其放回堆栈上的同一位置以进行下一个循环:

 movl %eax, -4(%ebp)   #store the current result
 decl %ecx             #decrease the power
 jmp  power_loop_start #run for the next power

我发现这是x86/x86-64的一个很好的指令参考。您将看到,正如Greg和Stephen所回答的那样,没有任何形式的mov指令会在两个内存操作数之间移动。我发现这是x86/x86-64的一个很好的指令参考。您将看到,正如Greg和Stephen所回答的那样,没有任何形式的mov指令会在两个内存操作数之间移动;pop def;Too只需添加到备注1,就可以推送abc;pop def;也