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 )
Calling From Nasm
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Calling From Nasm
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.
Re:Calling From Nasm
...and it's exactly the same in AT&T.
Every good solution is obvious once you've found it.
Re:Calling From Nasm
[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
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
Re:Calling From Nasm
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.cenan wrote:push 5 ; parameters are pushed from right to left
push msg
call printf
add esp,4 ; removing msg from stack