Function return values

Reply

Join Date: Jun 2009
Posts: 1
Reputation: BlackJackVr is an unknown quantity at this point 
Solved Threads: 0
BlackJackVr BlackJackVr is offline Offline
Newbie Poster

Function return values

 
0
  #1
Jun 30th, 2009
Hello, i'm developing a simple assembly (AT&T) program as exercise in preparation of an exam and i have a problem:

I need to pass 4 return values of a function using the stack. I know that after the "call", %esp points to the return address that will be used from the "ret". I've tried to pop that value from the stack, push my return values and then push the return address again just before the "ret" but it doesn't work.

Can i have an help please? Where do i need to put those values in the stack? And how do i have to manipulate the %esp or %ebp?

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Function return values

 
0
  #2
Jun 30th, 2009
val1 = func( &apple, &bat, &cat, &dog )

The address of apple, bat, cat, and dog are now on the stack.
here's one method!

  1. ZZ equ ???? ; I forget the actual number to use try 8?
  2. Check your address of those arguments, then in debugger
  3. to align them. Dependent upon memory model, method of stack protection etc. that ZZ value has to be set accordingly!
  4.  
  5. mov ebx, [ebp + ZZ + 0] ; Save apple
  6. mov [ebx], eax
  7.  
  8. mov ebx, [ ebp + ZZ + 4]
  9. mov [ebx], eax
  10.  
  11. mov ebx, [ ebp + ZZ + 8]
  12. mov [ebx], eax
  13.  
  14. mov ebx, [ ebp + ZZ + 12]
  15. mov [ebx], eax
  16.  
  17. return eax
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 122
Reputation: NotNull is an unknown quantity at this point 
Solved Threads: 12
NotNull's Avatar
NotNull NotNull is offline Offline
Junior Poster

Re: Function return values

 
0
  #3
Jul 11th, 2009
ESP points to the current top of stack,
PUSH & POP implicity decrement and increment ESP.
Right upon entering your function ESP points to a
DWORD containing the return address (address
of the instruction past the CALL).
RET will pop the DWORD off the stack (according
to the value of ESP so make sure ESP points
to the return address) to implicitly
change the contents of IP.
If you reserve space on the stack for a couple
of DWORDs (if you need to return two values)
before you CALL your function, to do so
simply subtract 8 from ESP before calling your function.
[ ] <-------Bottom of stack | Higher Address
[ ] Space 2
[ ] Space 1
[ ] Return Address | Lower Address
Then copy ESP into EBP to index into the stack,
once in your function to place the return values on
the stack.
To copy into Space 1 use movl src,4(%ebp)
To copy into Space 2 use movl src,8(%ebp)
----------------------------------------------------------
To control a mind violates a man, and all it has been used for is
hurting and afflicting. Nowonder I progam in assembly...
--->Now available http://dotcoding.netai.net/
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 134
Reputation: mathematician is an unknown quantity at this point 
Solved Threads: 3
mathematician mathematician is offline Offline
Junior Poster

Re: Function return values

 
0
  #4
Jul 11th, 2009
  1. push bp
  2. mov bp,sp
  3. ..............
  4. ..............
  5. ..............
  6. mov [bp-2], ax ;first return valuse
  7. mov [bp-4], dx ;second return value
  8. pop bp
  9. ret
  10.  
  11. After return
  12. mov bp,sp
  13. mov ax, [bp-6]
  14. mov dx, [bp-8] ;these need adjusting if you push more than
  15. ;just bp, or if you are using eip rather than ip

Of course it would be a whole lot easier to use registers to return
a value; or do as Wildgoose says, and push the address of variables onto the stack if you want to return more than one.
Last edited by mathematician; Jul 11th, 2009 at 10:17 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 122
Reputation: NotNull is an unknown quantity at this point 
Solved Threads: 12
NotNull's Avatar
NotNull NotNull is offline Offline
Junior Poster

Re: Function return values

 
0
  #5
Jul 11th, 2009
mathematician your right you can just place the address
of the memory variables on the stack, this would be
a good method.
AT&T syntax goes: mnemonic src,dest

  1. myfunc:
  2. pushl %ebp
  3. movl %esp,%ebp
  4. movl 8(%ebp),%ebx ; move first address on stack into %ebx
  5. movl 0x122,(%ebx) ; alter data pointed to

16-bit example:
  1. myfunc:
  2. push bp
  3. mov bp,sp
  4. mov bx,[bp+4]
  5. mov word [bx],0x122
Last edited by NotNull; Jul 11th, 2009 at 11:08 pm.
----------------------------------------------------------
To control a mind violates a man, and all it has been used for is
hurting and afflicting. Nowonder I progam in assembly...
--->Now available http://dotcoding.netai.net/
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC