Page 1 of 1
Passing a var from c to asm
Posted: Sat Jul 22, 2006 12:12 pm
by Tk
I need to pass a var ( or vars ) from c to asm.
ie:
_SetGMode:
mov ah,0x00
mov al,[var]
int 0x10
ret
and c:
extern void SetGMode( unsigned char var );
SetGmode(0x13);
Thanks
Re:Passing a var from c to asm
Posted: Sat Jul 22, 2006 12:27 pm
by mhaggag
When your asm function is called, esp will point to the return address. esp + 4 will contain the variable in question. So, off the top of my head:
Code: Select all
push ecx ; Save this because we'll modify it
mov ecx, [esp + 8] ; + 8 because we have pushed ecx, so 4 + 4 = 8
mov ah,0x00
mov al, cl
int 0x10
pop ecx
ret
Or something similar.
Re:Passing a var from c to asm
Posted: Sat Jul 22, 2006 12:40 pm
by Tk
Thanks its working, now the reverse....i need to pass unsigned char back to the c function.
Re:Passing a var from c to asm
Posted: Sat Jul 22, 2006 1:27 pm
by mhaggag
Thanks its working, now the reverse....i need to pass unsigned char back to the c function.
It's the same concept, really--you push the values on the stack. Something like (assuming nasm here):
Code: Select all
extern some_c_function
...
some_assembly_routine:
push eax ; Push this as a parameter to the function
call some_c_function
add esp, 4 ; Pop the parameter off the stack
; but since we don't need its value
; we just add 4 to the stack pointer (instead of popping)
The convention with return values, in case you need them, is to put the return value (if it's a primitive) or its address (if it's not a primitive) in eax. You can do that to return a value from asm to C or vice versa.
Re:Passing a var from c to asm
Posted: Sat Jul 22, 2006 2:04 pm
by Tk
Thanks again, thats perfect.