1

私は「ゼロからのプログラミング」を読んでいます。この本が何であるかわからない場合でも、あなたは私を助けることができます。

この本(第4章)には、私が理解していないことが2つあります。

  1. movl %ebx, -4(%ebp) #store current resultのために。
  2. 「現在の結果」とはどういう意味ですか

以下のコードのマークされたセクションには、次のものがあります。

movl 8(%ebp), %ebx

これはに保存8(%ebp)することを意味します%ebxが、私が理解できない理由は、プログラマーが8(%ebp)に保存したい場合-4(%ebp)、なぜ8(%ebp)パススルーする必要があるの%ebxですか?「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
4

3 に答える 3

2

多くのアセンブリオペコードは、1つのメモリオペランド(ソースまたは宛先)のみを受け入れます。これはおそらく、メモリからメモリへの移動が%ebxを介して行われる理由を説明しています。

于 2011-03-21T02:10:02.710 に答える
1

グレッグが示唆したように、x86には、ほとんどの主流アーキテクチャと同様に、データをメモリからメモリにコピーする命令がありません[1]。したがって、別のロードストアを使用してデータをコピーする必要があります。最初にソースメモリからレジスタにデータをロードし、次にそのレジスタからデスティネーションメモリにデータを保存します。ここで起こっているのはそれだけです。

rep movs[1]わかっています、わかっていますが、これを省いて、物事を単純に保ちましょう。

于 2011-03-21T02:25:11.287 に答える
1

私はこれを信じています:

 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)この操作にも使用できたはずですが、プログラムをまったく変更しないので、作成すべきではない理由はありません。

しかし、コメントはもっと明確になる可能性があり、これもタイプミスだと思います。この時点で、次のように言った方がよいでしょう#storing 1st argument on the local stack frame

label power_loop_startはその変数を使用し、迅速な操作のために一時的に格納して%eaxから、次のループのためにスタックの同じ場所に戻します。

 movl %eax, -4(%ebp)   #store the current result
 decl %ecx             #decrease the power
 jmp  power_loop_start #run for the next power
于 2011-03-21T02:36:30.040 に答える