Click to See Complete Forum and Search --> : Pass by reference


blinksumgreen
March 17th, 2009, 12:57 AM
I have an assignment in which I'm not allowed to use global variables and have to implement the code using 'pass by reference'. I was wondering how, given that the number I need to use is already in eAx, could I push it's address onto the stack? I know that you can only use the [ ] with edi, esi, ebx, and ebp, so it would involve moving the value into one of those... I would assume.

All the examples I've seen involve global variables and using the 'offset' command. Is there an equivalent command to 'offset' that works with registers?

Any help would be greatly appreciated!

rxbagain
March 17th, 2009, 02:05 AM
If you want to pass it by reference, you have to store it into the memory and get that memory's addess to be passed to the function. If you are not allowed to use global variables, you can declare it as local.

In practice, parameters are passed into the stack. For example in your problem, your program would be something like this

.386
.model flat, stdcall

.code

ExitProcess PROTO :DWORD
myfunc PROTO :DWORD

myfunc PROC intptr:DWORD
mov eax, [intptr] ; get the address passed
mov eax, [eax] ; from the address, get its content

; process the value of eax here

ret
myfunc ENDP

Main PROC
Local ival:DWORD

mov eax, 112233h ; we assume eax is 0x112233
mov [ival], eax ; place the value to our local variable
lea eax, [ival] ; get the variable address
push eax ; push it to the stack
call myfunc ; call myfunc
; you can also use Invoke myfunc, ADDR ival for simpler code

Invoke ExitProcess, 0
Main ENDP

END


Note that when the variable is in local storage, we use LEA instead of offset to get the address.

Hope it will help you

blinksumgreen
March 17th, 2009, 09:53 PM
Thank you for the help!