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'm trying to take that code and print its return using C. I would be calling this asm function from C and would like to print the return, so how would I do that? thanks
Free energy is indeed evil for it absorbs the light.
What is the result of that function? edx, ecx, ebx and eax? C functions cannot return multiple values. By default gcc (on x86) stores return values in eax. Function arguments must be pushed onto the stack. The caller pops them from the stack after the function was called. See the wiki page.
When your function returns more than one value you can pass pointers to it and let the function write it's return values to the memory location that is referenced by the pointer. You can also use inline assembly. http://wiki.osdev.org/Inline_Assembly/Examples has an example of calling CPUID from inline assembly.
If you want to call an assembly function from C you just have to add a C function prototype to your C files and then simply call the function as it was a C function. GCC will then take eax as the return value.
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
In terms of returning asm'd values from C, I could define my prototype to return an unsigned char and it would on its own or I would need some inline assembly for that as well? A bit confused right now; it just doesn't seem possible without inline. thanks
Free energy is indeed evil for it absorbs the light.
section .text
global add_numbers:
push ebp ; standard function prologue (can be omitted if you want to access arguments directly through esp)
mov ebp, esp
mov eax, [ebp + 12] ; load first argument (they are pushed in reverse order)
add eax, [ebp + 8] ; add second one
; eax is the return value
mov esp, ebp ; standard function epilogue
pop ebp
ret
Here is the C code that calls the assembly function:
unsigned int add_numbers(unsigned int number1, unsigned int number2);
void main() {
printf("5 + 10 = %u", add_numbers(5, 10));
}
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
Wow, that is really cool that you can do that. I suppose the linker takes care of that then. It's cool that I can use asm as a DLL now, might write the majority of the 'thinking' code in asm for the speed influx. Thanks a lot for the example.
Free energy is indeed evil for it absorbs the light.
the idea is that you *call* the function, not print its contents
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]