C and Asm

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
PlayOS

C and Asm

Post 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.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:C and Asm

Post 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)
PlayOS

Re:C and Asm

Post 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?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:C and Asm

Post by Pype.Clicker »

absolutely ;)
willow

Re:C and Asm

Post 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]
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:C and Asm

Post 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.
Willow

Re:C and Asm

Post 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]
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:C and Asm

Post 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...
Post Reply