Passing Variables

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.
Post Reply
User avatar
Omega
Member
Member
Posts: 250
Joined: Sun May 25, 2008 2:04 am
Location: United States
Contact:

Passing Variables

Post 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
Free energy is indeed evil for it absorbs the light.
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Re: Passing Variables

Post 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.
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].
User avatar
Omega
Member
Member
Posts: 250
Joined: Sun May 25, 2008 2:04 am
Location: United States
Contact:

Re: Passing Variables

Post 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
Free energy is indeed evil for it absorbs the light.
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Re: Passing Variables

Post 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));
}
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].
User avatar
Omega
Member
Member
Posts: 250
Joined: Sun May 25, 2008 2:04 am
Location: United States
Contact:

Re: Passing Variables

Post 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.
Free energy is indeed evil for it absorbs the light.
User avatar
Omega
Member
Member
Posts: 250
Joined: Sun May 25, 2008 2:04 am
Location: United States
Contact:

Re: Passing Variables

Post 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.
Free energy is indeed evil for it absorbs the light.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Passing Variables

Post by Combuster »

the idea is that you *call* the function, not print its contents :roll:
"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 ]
User avatar
Omega
Member
Member
Posts: 250
Joined: Sun May 25, 2008 2:04 am
Location: United States
Contact:

Re: Passing Variables

Post 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
Free energy is indeed evil for it absorbs the light.
Post Reply