943,879 Members | Top Members by Rank

Ad:
  • Assembly Discussion Thread
  • Unsolved
  • Views: 2957
  • Assembly RSS
Jun 30th, 2009
0

Function return values

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BlackJackVr is offline Offline
1 posts
since Jun 2009
Jun 30th, 2009
0

Re: Function return values

val1 = func( &apple, &bat, &cat, &dog )

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

Assembly Syntax (Toggle Plain Text)
  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
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 11th, 2009
0

Re: Function return values

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)
Reputation Points: 36
Solved Threads: 19
Junior Poster
NotNull is offline Offline
198 posts
since Oct 2008
Jul 11th, 2009
0

Re: Function return values

Assembly Syntax (Toggle Plain Text)
  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.
Reputation Points: 14
Solved Threads: 4
Junior Poster
mathematician is offline Offline
149 posts
since Nov 2006
Jul 11th, 2009
0

Re: Function return values

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

Assembly Syntax (Toggle Plain Text)
  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:
Assembly Syntax (Toggle Plain Text)
  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.
Reputation Points: 36
Solved Threads: 19
Junior Poster
NotNull is offline Offline
198 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Assembly Forum Timeline: help to make this code work
Next Thread in Assembly Forum Timeline: Assembly Error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC