Calling From Nasm

Programming, for all ages and all languages.
Post Reply
Tolga

Calling From Nasm

Post by Tolga »

Hi.

In my C kernel, i want to call nasm functions from an assembly file. How can i do this? Show an example. ::)

( I don't want to use AT&T in C. Because i don't know it. ;D )
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:Calling From Nasm

Post by Pype.Clicker »

export the name of your asm function, declare the function name in C and then invoke it. Function arguments will be on stack and return value should be placed in EAX before your function returns.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Calling From Nasm

Post by Solar »

...and it's exactly the same in AT&T. ;-)
Every good solution is obvious once you've found it.
Tolga

Re:Calling From Nasm

Post by Tolga »

Can somebody show an example? I'm new at C. A small example. ::)
cenan

Re:Calling From Nasm

Post by cenan »

[extern printf] ; c function

push 5 ; parameters are pushed from right to left
push msg
call printf
add esp,4 ; removing msg from stack
; this is same as printf("hodohodo %d", 5);
; now eax contains 8 (number of chars written)

[section .data]
msg db "hodohodo %d",0
Ytinasni

Re:Calling From Nasm

Post by Ytinasni »

cenan wrote:push 5 ; parameters are pushed from right to left
push msg
call printf
add esp,4 ; removing msg from stack
That'll need to be add esp, 8 if you're in protected-mode (which includes Windows, Linux, BSD, most of our hobby OS', etc...), as you need to remove both arguments ("%d", 5 ) from the stack.
Schol-R-LEA

Re:Calling From Nasm

Post by Schol-R-LEA »

This page ansd the ones it references might help.
Post Reply