Page 1 of 1

C and Asm

Posted: Fri Sep 06, 2002 4:57 am
by PlayOS
Hi,

I just have a question about mixing asm and C.

If I have an ASM function that takes three args say a WORD, BYTE and DWORD, C prototype like this

SomeFunc( WORD, BYTE, DWORD );

If I call it from C like this

SomeFunc( 32546, 200, 20059958 );

I know that C passes these args from right to left.

If I have a 32Bit stack would I access the args like this

push ebp
mov ebp, esp
mov eax, word [ebp + 4] ; the word arg
mov ebx, byte [ebp + 6] ; the byte arg
mov ecx, dword [ebp + 7] ; the dword arg
.....
pop ebp
ret

I guess what I am asking is that if a stack has 32Bit pushes/pops by default, will a WORD on the stack still only be 2 bytes or will it be padded to 4 bytes?


thanks.

Re:C and Asm

Posted: Fri Sep 06, 2002 5:23 am
by Pype.Clicker
the stack has 32bits pushes only. thus pushing 3 bytes requires the same stack amount as pushing 3 ints. This is for memory alignment purposes (too slow to read the return address at a non-multiple of four address because it could be on a cache line barrier and issue 2 memory read cycles instead of 1)

Re:C and Asm

Posted: Fri Sep 06, 2002 5:42 am
by PlayOS
So then to get at my args I will have to do this:

push ebp
mov ebp, esp
mov eax, [ebp + 4] ; the word arg
mov ebx, [ebp + 8] ; the byte arg
mov ecx, [ebp + 12] ; the dword arg

; ax = word arg
; bl = byte arg
; ecx = dword arg

.....
pop ebp
ret

would this be correct?

Re:C and Asm

Posted: Fri Sep 06, 2002 6:52 am
by Pype.Clicker
absolutely ;)

Re:C and Asm

Posted: Wed Sep 18, 2002 6:01 pm
by willow
I'm searching for that, what is the command for calling a fonction made in ASM from a C file ?!!! please help me
can we put the asm code in the c file or we link ik ???

i don't know help me

email me at : [email protected]

Re:C and Asm

Posted: Thu Sep 19, 2002 1:52 am
by Pype.Clicker
basically,
1) write your ASM function in a separate .asm file and declare it to be global

Code: Select all

    global nope
    nope:
        nop
        ret
2) declare your asm function in C code (and beware bad arguments)

Code: Select all

void nope();
3) just call your function as if it was a C function.

If you need more infos about how to pass arguments (trough the stack ;) ) or how to receive a return value (through %eax), i suggest you study a disasembled version of some of your C functions.

Re:C and Asm

Posted: Thu Sep 19, 2002 6:27 am
by Willow
You are my hero!!! byt when i compile my file do a make the same thing ?? And if i take TASM is it the same thing

thanck
email me: [email protected]

Re:C and Asm

Posted: Thu Sep 19, 2002 8:03 am
by Pype.Clicker
oh, well, yes ...
the asm directive "global" might have another name in your assembler (just check your manual ;) ...
Also, under MsDos environment, the compiler prepends a "_" before any C name, so the nope() C function call looks for the _nope assembler function...