Flusso di funzioni di assemblaggio
Domanda
Sto leggendo una "programmazione da zero", se non sai cosa sia questo libro, puoi ancora aiutarmi.
In questo libro (capitolo 4) Ci sono 2 cose che non capisco:
- che cosa
movl %ebx, -4(%ebp) #store current result
per. - e cosa significa "risultato attuale"
Nella sezione contrassegnata nel codice seguente, c'è:
movl 8(%ebp), %ebx
il che significa salvare 8(%ebp)
a %ebx
, ma il motivo per cui non capisco è, se il programmatore vuole 8(%ebp)
per salvare a -4(%ebp)
, perché dovrebbe 8(%ebp)
essere passato attraverso %ebx
? È "movl 8(%ebp), -4(%ebp)
"Akward? O c'è qualche errore di battitura movl 8(%ebp), %ebx #put first argument in %eax
? (Penso %ebx
dovrebbe essere %eax
o 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
Soluzione
Credo che questo:
movl 8(%ebp), %ebx #put first argument in %eax
era un errore di battitura e dovrebbe davvero essere:
movl 8(%ebp), %ebx #put first argument in %ebx
E se l'hai notato, in seguito il codice è corretto:
movl %ebx, -4(%ebp) #store current result
Alla fine, l'autore avrebbe potuto utilizzare %eax
Anche per questa operazione (anziché %ebx
), non c'è motivo per cui non dovrebbe, dato che non cambierebbe affatto il programma.
Ma il commento potrebbe essere molto più chiaro e credo che anche questo sia un errore di battitura. A questo punto, sarebbe meglio se dicesse: #storing 1st argument on the local stack frame
.
Etichetta power_loop_start usa quella variabile e la memorizza temporaneamente %eax
Per operazioni rapide e poi rimetterlo nella stessa posizione sullo stack per il loop successivo:
movl %eax, -4(%ebp) #store the current result
decl %ecx #decrease the power
jmp power_loop_start #run for the next power
Altri suggerimenti
Molti op -codici di assemblaggio accettano solo uno Memoria operand (origine o destinazione). Questo probabilmente spiega perché una mossa dalla memoria alla memoria viene eseguita tramite %EBX.
Come Greg Hint, X86, come la maggior parte delle architetture tradizionali, non ha un'istruzione che copia i dati dalla memoria alla memoria [1]. Pertanto, è necessario copiare i dati utilizzando un separato carico e negozio. Prima si carica i dati dalla memoria di origine in un registro, quindi si archiviano i dati da quel registro alla memoria di destinazione. Questo è tutto ciò che sta accadendo qui.
1] Lo so, lo so, ma andiamo rep movs
fuori da questo e mantieni le cose semplici.