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.
I've got two .o files, one is compiled from c code using gcc and the other is asm code compiled with nasm. They are linked into a .bin file which is the kernel. How do I pass variables from the c to the asm?
What I mean is this:
Also, if I send the name of the function (as a string) to the asm function, how would I tell the asm func to run dothis()?
Right now I have to tell the asm what to run, like this:
1. calling a c-function from asm: i take NASM as reference:
in c: void do_sthg(void);
in nasm:
EXTERN do_sthg (or EXTERN _do_sthg or EXTERN __do_sthg)
call do_sthg (or _do_sthg or __do_sthg)
When you call an asm from c:
in c: extern int do_sthg(int c, int d);
in nasm:
[GLOBAL do_sthg]
do_sthg:
push ebp
mov ebp,esp
mov ebx,[ebp+8]
mov ecx,[ebp+12]
add ebx,ecx ;(say result is then in ebx)
mov eax,ebx ;(the c function fetches return value from eax usually)
pop ebp
ret
If I have built in some mistakes, sorry for that. But I hope, the principle is clear.
But..., let's say that I wanted to tell the asm to dynamically load funtions. How do I do that? I mean:
The user types "dothis" and the prgm runs "dothis()."
How do I do this?
Create a table with function names and pointers to the functions themselves. When a user types a command, search for the command name in the table. Then call the function through the pointer.
an alternative would be to read the symbol table that comes along with the code to find out the address of the function to be called ... however, it would be dangerous because it would mean that you couldn't decide what function can be called and which one can't ...
the system will exit the process automatically, since main() is not the actual entry point, but start, which looks something like this (very simplified):
further, when you say exit(n) -- is there really need for a return clause?
In test3() I just added the return() after exit() to avoid a compiler warning. Since the compiler would generate a return anyway there is no code size penalty for adding it.