I'm trying to implement a C interface for processor's cpuid instruction. I'm aware that there are plenty of libraries for this and that using inline assembly for compilers that support it would be much simple, but I'm doing it this way because it just seems an interesting exercise to me. I'm assembling it using nasm in macho64 format:
; cpuid.nasm.x86_64.asm
global _cpuid
section .text
_cpuid:
push rax
push rbx
push rcx
push rdx
mov eax, [esp]
mov ebx, [esp+4]
mov ecx, [esp+8]
mov edx, [esp+12]
cpuid
pop rax
pop rbx
pop rcx
pop rdx
ret
section .data
// test.c
#include <stdio.h>
#include <stdint.h>
extern void
cpuid(uint32_t *registers);
int
main(int argc, char **argv)
{
uint32_t registers[4] = {0, 0, 0, 0};
cpuid(registers);
}
nasm -f macho64 cpuid.nasm.x86_64.asm
gcc test.c cpuid.nasm.x86_64.o
./a.out
However, when running the executable I get a segmentation fault. As far as I know seg. faults are caused by memory bad handling, but I cannot find any possible memory-related problem.