In the following code eax was decremented before calling the function inside the function(recursion) in calculating the factorial.However my understanding of the code makes me think the function takes its parameter from 8(%ebp) where the parameter 4(the number we are finding the factorial for) was stored.I think the value in 8(%ebp)should be decremented not eax .Can anyone explain?Thanks - #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)
-
*****************************************************************
-
3 4941
If you decremented 8(%ebp) instead of %eax, you might still get the same answer...I'm not entirely sure. Why not try it?
i.e. write a short C program containing a main and a factorial(int a) function declaration, compile it into assembler code, insert your factorial assembler code, and see if it works.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Peri |
last post by:
I'm trying to create Python parser/interpreter using ANTLR.
Reading grammar from language refference I found:
or_expr::= xor_expr | or_expr "|" xor_expr
For me it looks like infinite recursion....
|
by: Joh |
last post by:
hello,
i'm trying to understand how i could build following consecutive sets
from a root one using generator :
l =
would like to produce :
, , , ,
|
by: SJD |
last post by:
I've just read Christoph Schittko's article on XmlSerializer:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxmlnet/html/trblshtxsd.asp
. . . and very informative it is too....
|
by: makko |
last post by:
Hello,
anyone know how to writre a program that take a commandline formula
and prints the calculated result? example;
$program 1+(2x3(3/2))-8
reagrds;
Makkko
|
by: paulw |
last post by:
Hi
Please give problems that "HAS TO" to use recursion (recursive calls to
itself.)
Preferrably real world examples, not knights tour.
I'm thinking about eliminating the use of stack...
...
|
by: Thomee Wright |
last post by:
I'm having a problem with a pair of applications I'm developing. I have a
server application which interacts with several instruments, and a client app
which connects to the server and provides a...
|
by: MTD |
last post by:
Hello all,
I've been messing about for fun creating a trial division factorizing
function and I'm naturally interested in optimising it as much as
possible.
I've been told that iteration in...
|
by: NOO Recursion |
last post by:
Hi everyone! I am trying to write a program that will search a 12x12 for a
thing called a "blob". A blob in the grid is made up of asterisks. A blob
contains at least one asterisk. If an...
|
by: Muzammil |
last post by:
int harmonic(int n) {
if (n=1) {
return 1;
}
else {
return harmonic(n-1)+1/n;
}
}
can any help me ??
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |