Page 1 of 1
Passing Variables
Posted: Tue Jun 24, 2008 12:20 am
by Omega
Here is a nasm/djgpp question that shouldn't be too difficult to answer. What I am trying to do is this:
Code: Select all
[BITS 32]
[SECTION .data]
GLOBAL _cpuid
[SECTION .text]
_cpuid:
mov eax,0
cpuid
mov [vendor_id],ebx
mov [vendor_id+4],edx
mov [vendor_id+8],ecx
mov edx,12
mov ecx,vendor_id
mov ebx,1
mov eax,4
ret
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
Re: Passing Variables
Posted: Tue Jun 24, 2008 12:40 am
by Korona
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.
Re: Passing Variables
Posted: Tue Jun 24, 2008 1:04 am
by Omega
Thanks a lot it was a great resource:
http://forum.osdev.org/viewtopic.php?t=11998.
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
Re: Passing Variables
Posted: Tue Jun 24, 2008 1:16 am
by Korona
It is possible without inline assembly. Here is a short example:
We want to call this assembly function from C:
Code: Select all
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:
Code: Select all
unsigned int add_numbers(unsigned int number1, unsigned int number2);
void main() {
printf("5 + 10 = %u", add_numbers(5, 10));
}
Re: Passing Variables
Posted: Tue Jun 24, 2008 2:08 pm
by Omega
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.
Re: Passing Variables
Posted: Tue Jun 24, 2008 9:44 pm
by Omega
Problem: My code prints garabage.
Assemble: nasm -f coff cpu.asm -o cpu.o
cpu.asm (NASM):
Code: Select all
[BITS 32]
[SECTION .data]
global _cpuid
[SECTION .text]
_cpuid:
pusha
xor eax,eax
cpuid
mov eax, [esp+36]
mov [eax], ebx
mov [eax+4], edx
mov [eax+8], ecx
mov byte [eax+12],0
popa
xor eax,eax
ret
main.c (DJGPP):
Code: Select all
void main()
{
extern char *cpuid(void);
printf(0x07,cpuid,1,1);
for(;;);
}
The output is: `1À¢‹D$$‰‰P‰HÆ@
Help? thanks.
Re: Passing Variables
Posted: Wed Jun 25, 2008 12:32 am
by Combuster
the idea is that you *call* the function, not print its contents

Re: Passing Variables
Posted: Wed Jun 25, 2008 1:13 am
by Omega
@Combuster: How did you hold back? That was a dumb mistake, my bad. For others in need, my working code:
Assemble: nasm -f coff cpu.asm -o cpu.o
cpu.asm: (NASM)
Code: Select all
[BITS 32]
[SECTION .data]
global _cpuid
[SECTION .text]
_cpuid:
pusha
xor eax,eax
cpuid
mov eax, [esp+36]
mov [eax], ebx
mov [eax+4], edx
mov [eax+8], ecx
mov byte [eax+12],0
popa
xor eax,eax
ret
Compile: gcc -c main.c -o main.o
main.c: (DJGPP)
Code: Select all
void main()
{
extern char *cpuid(void);
printf(0x07, cpuid(), 1, 1); //you'll need your own printf function; I do too ;)
for(;;);
}
Output: AuthenticAMD