BBC BASIC for Windows
Programming >> Assembly Language Programming >> Returning values from ASM functions
http://bb4w.conforums.com/index.cgi?board=assembler&action=display&num=1279396578

Returning values from ASM functions
Post by David Williams on Jul 17th, 2010, 7:56pm

I'm a bit embarrassed to have to ask, but...

I quite often - nearly always, in fact - push all registers onto the stack at the beginning of a routine:

Code:
.routine
pushad ; preserve registers

(do stuff)

popad ; restore registers
ret 


But I find that when I need to return a value in EAX, I do this:

Code:
.routine
pushad
(do some stuff...)
(put some value in EAX)
mov [^temp], eax
popad
mov eax, [^temp]
ret
 



Is there a neater way to return a value in EAX without stashing the return value in a temporary memory location first?


Thanks.


David.

Re: Returning values from ASM functions
Post by admin on Jul 17th, 2010, 9:53pm

on Jul 17th, 2010, 7:56pm, David Williams wrote:
Is there a neater way to return a value in EAX without stashing the return value in a temporary memory location first?

I don't know whether you would consider it 'neater', but this is the code I invariably use:

Code:
      mov [esp+28],eax
      popad
      ret 

Richard.

Re: Returning values from ASM functions
Post by David Williams on Jul 18th, 2010, 7:36pm

on Jul 17th, 2010, 9:53pm, Richard Russell wrote:
I don't know whether you would consider it 'neater', but this is the code I invariably use:

Code:
      mov [esp+28],eax
      popad
      ret 

Richard.


Thank you!


David.