Jump to content
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

Sign in to follow this  
NCS_One

[RESOLVIDO][assembly] Dúvida em código.

Recommended Posts

NCS_One

Boas.

Estou a ler o livro "Programming from the Ground Up" e tenho uma dúvida em relação a um dos exemplos.

#PURPOSE - Given a number, this program computes the
#          factorial. For example, the factorial of
#          3 is 3 * 2 * 1, or 6. The factorial of
#          4 is 4 * 3 * 2 * 1, or 24, and so on.
#
#This program shows how to call a function recursively.
.section .data
#This program has no global data
.section .text
.globl _start
.globl factorial #this is unneeded unless we want to share
                  #this function among other programs
_start:
pushl $4         #The factorial takes one argument - the
                  #number we want a factorial of. So, it
                  #gets pushed
call factorial #run the factorial function
addl $4, %esp    #Scrubs the parameter that was pushed on
                  #the stack
movl %eax, %ebx #factorial returns the answer in %eax, but
                  #we want it in %ebx to send it as our exit
                  #status
movl  $1, %eax   #call the kernel’s exit function
int   $0x80
#This is the actual function definition
.type factorial,@function
factorial:
pushl %ebp       #standard function stuff - we have to
                  #restore %ebp to its prior state before
                  #returning, so we have to push it
movl %esp, %ebp #This is because we don’t want to modify
                  #the stack pointer, so we use %ebp.
movl  8(%ebp), %eax #This moves the first argument to %eax
                  #4(%ebp) holds the return address, and
                  #8(%ebp) holds the first parameter
cmpl $1, %eax       #If the number is 1, that is our base
                     #case, and we simply return (1 is
                     #already in %eax as the return value)
je end_factorial
decl %eax           #otherwise, decrease the value
pushl %eax          #push it for our call to factorial
call factorial      #call factorial
movl 8(%ebp), %ebx #%eax has the return value, so we
                     #reload our parameter into %ebx
imull %ebx, %eax    #multiply that by the result of the
                     #last call to factorial (in %eax)
                     #the answer is stored in %eax, which
                     #is good since that’s where return
                     #values go.
end_factorial:
movl %ebp, %esp #standard function return stuff - we
popl %ebp       #have to restore %ebp and %esp to where
                #they were before the function started
ret             #return to the function (this pops the
#return value, too)

Não estou a perceber as duas linhas a seguir ao segundo "call factorial" :

movl 8(%ebp), %ebx
imull %ebx, %eax

Estas são alguma vez executadas ?

Está uma condição antes que se for verdadeira vai para "end_factorial", se for falsa um pouco à frente é feita a "call factorial". Não estou a perceber como estas duas linhas são executadas.

Obrigado.


Se a vida te voltar as costas aproveita e apalpa-lhe o cu.

Share this post


Link to post
Share on other sites
NCS_One

Esqueçam, tinha analisado mal o código.


Se a vida te voltar as costas aproveita e apalpa-lhe o cu.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.