Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 17th, 2012, 2:44 AM   #1
codemylife
Programmer
 
Join Date: Oct 2011
Posts: 63
Rep Power: 3 codemylife is on a distinguished road
[16 bit tasm] The underbelly of recursion

I need help understanding how recursion works. Take a a quick look at the code below before reading on.

I want to transfer the parameters to my recursive procedure using the stack
so i push 'em. My stack looks like this :
STACK: "please" "help" "me" "Instruction pointer" (IP is on top, I know this is saved into the stack when i call the procedure in main and its a pointer to the next instruction in the main program)

So now I do something with the data in the procedure and I call the procedure again... this is where I am stuck... what the holy stdlib... happens now? What happens in the stack now?

asm Syntax (Toggle Plain Text)
  1. .model small
  2. .stack
  3. .data
  4.  
  5. please dw 5 ; some random data
  6. help dw 6
  7. me dw ?
  8.  
  9. .code
  10. PROC rec_for_dummies
  11.  
  12. ;do something with data
  13.  
  14. call rec_for_dummies
  15.  
  16. ret
  17. endp rec_for_dummies
  18. start:
  19. mov ax,@data
  20. mov ds,ax
  21.  
  22. push please
  23. push help
  24. push me
  25.  
  26. call rec_for_dummies
  27.  
  28. mov ax,4c00h
  29. int 21h
  30. end start
codemylife is offline   Reply With Quote
Old Jan 17th, 2012, 6:40 AM   #2
Benoit
Security Specialist
 
Benoit's Avatar
 
Join Date: Sep 2004
Posts: 1,053
Rep Power: 11 Benoit will become famous soon enough
Re: [16 bit tasm] The underbelly of recursion

What do you want it to do? Currently it does nothing, and will just keep calling itself (pushing the return address onto the stack then jumping to the beginning of the function) until you run out of stack space.

You need to set some condition to make it stop.
Benoit is offline   Reply With Quote
Old Jan 17th, 2012, 11:36 AM   #3
codemylife
Programmer
 
Join Date: Oct 2011
Posts: 63
Rep Power: 3 codemylife is on a distinguished road
Re: [16 bit tasm] The underbelly of recursion

Quote:
Originally Posted by Benoit View Post
What do you want it to do? Currently it does nothing, and will just keep calling itself (pushing the return address onto the stack then jumping to the beginning of the function) until you run out of stack space.

You need to set some condition to make it stop.
I am not interested in solving a particular problem here. I am interested in how recursion works in assembly.

Lets assume I have a stopping condition in that program. What happens when I do line 14 ? What is placed into the stack? If you need to use an example to explain use the classic n factorial.
codemylife is offline   Reply With Quote
Old Jan 17th, 2012, 9:36 PM   #4
Benoit
Security Specialist
 
Benoit's Avatar
 
Join Date: Sep 2004
Posts: 1,053
Rep Power: 11 Benoit will become famous soon enough
Re: [16 bit tasm] The underbelly of recursion

The address of the next instruction is pushed onto the stack.

0x0000  rec_for_dummies
0s0002  call rec_for_dummies
0x0004  ret

Suppose those numbers above are the addresses of those instructions

Your stack is going to look like this:

0x0004
0x0004
0x0004
0x0004
0x0004
0x0004
...
Until you run out of stack space

Because that's what the next instruction is after call rec_for_dummies. Every time you execute the instruction call rec_for_dummies, it pushes that address on to the stack and jumps to the beginning of the rec_for_dummies function.
Benoit is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bit manipulation aka madness codemylife C 6 Nov 24th, 2011 2:42 PM
limits of int64? laukejas Delphi 16 Sep 13th, 2010 11:06 AM
64 bit to 32 bit windows cyb3rl0rd1867 Assembly 5 Aug 25th, 2009 11:11 PM
Reverse bit order in MIPS/SPIM theduff Assembly 5 Apr 27th, 2009 3:30 PM
Need help with LC-3 rotate bit pattern program lilfindley Assembly 0 Mar 17th, 2009 10:32 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:09 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC