473,386 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

problem understanding recursion in assembly language

51
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
Expand|Select|Wrap|Line Numbers
  1. #PURPOSE - Given a number, this program computes the
  2. # factorial. For example, the factorial of
  3. # 3 is 3 * 2 * 1, or 6. The factorial of
  4. # 4 is 4 * 3 * 2 * 1, or 24, and so on.
  5. #
  6. #This program shows how to call a function recursively.
  7. .section .data
  8. #This program has no global data
  9. .section .text
  10. .globl _start
  11. .globl factorial #this is unneeded unless we want to share
  12. #this function among other programs
  13. _start:
  14. pushl $4 #The factorial takes one argument - the
  15. #number we want a factorial of. So, it
  16. #gets pushed
  17. call factorial #run the factorial function
  18. addl $4, %esp #Scrubs the parameter that was pushed on
  19. #the stack
  20. movl %eax, %ebx #factorial returns the answer in %eax, but
  21. #we want it in %ebx to send it as our exit
  22. #status
  23. movl $1, %eax #call the kernel’s exit function
  24. int $0x80
  25. #This is the actual function definition
  26. .type factorial,@function
  27. factorial:
  28. pushl %ebp #standard function stuff - we have to
  29. #restore %ebp to its prior state before
  30. #returning, so we have to push it
  31. movl %esp, %ebp #This is because we don’t want to modify
  32. #the stack pointer, so we use %ebp.
  33. movl 8(%ebp), %eax #This moves the first argument to %eax
  34. #4(%ebp) holds the return address, and
  35. #8(%ebp) holds the first parameter
  36. cmpl $1, %eax #If the number is 1, that is our base
  37. #case, and we simply return (1 is
  38. #already in %eax as the return value)
  39. je end_factorial
  40. decl %eax #otherwise, decrease the value
  41. pushl %eax #push it for our call to factorial
  42. call factorial #call factorial
  43. movl 8(%ebp), %ebx #%eax has the return value, so we
  44. #reload our parameter into %ebx
  45. imull %ebx, %eax #multiply that by the result of the
  46. #last call to factorial (in %eax)
  47. #the answer is stored in %eax, which
  48. #is good since that’s where return
  49. #values go.
  50. end_factorial:
  51. movl %ebp, %esp #standard function return stuff - we
  52. popl %ebp #have to restore %ebp and %esp to where
  53. #they were before the function started
  54. ret #return to the function (this pops the
  55. #return value, too)
  56. *****************************************************************
  57.  
Oct 14 '08 #1
3 4941
Ganon11
3,652 Expert 2GB
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.
Oct 14 '08 #2
asedt
125 100+
I think it's like this:

(%ebp)

Is an indirect references to memory and we move the content of what its pointing on to a register -> %eax befor working with it. Itself it's just a number/adress of were the thing is stored in memory.

The 8(%ebp) adds 8 to the adress.

Readings:

http://www.ibm.com/developerworks/li...rary/l-ia.html


http://209.85.135.104/search?q=cache...lnk&cd=2&gl=se
Oct 15 '08 #3
dynamo
51
thanx for the help guys.
Oct 17 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
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....
45
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 : , , , ,
12
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....
16
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
10
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... ...
1
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...
18
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...
12
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...
35
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 ??
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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...
0
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
0
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...
0
marktang
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,...
0
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...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.