Is it possible to reference C enums from an assembly file?

1k Views Asked by At

syscalls.h

enum Syscall {
  OPEN_FILE,
  READ_FILE,
  CLOSE_FILE
};

syscalls.s

extern WRITE_TO_SCREEN
global write_to_screen
write_to_screen:
  mov eax, WRITE_TO_SCREEN
  mov ebx, [esp+4]
  int 0x80
  ret

Gives me this error:

stdlib/syscalls.o: In function `write_to_screen':
stdlib/syscalls.s:(.text+0x1): undefined reference to `WRITE_TO_SCREEN'
make: *** [kernel.elf] Error 1
1

There are 1 best solutions below

0
On

No there isn't.
You could do something like:

enum_support.h

#ifdef __ASSEMBLER__
#define ENUM_START
#define ENUM_VALUE(key,value) .equ key,value
#define ENUM_END(typename)
#else
#define ENUM_START typedef enum{
#define ENUM_VALUE(key,value) key=value,
#define ENUM_END(typename)} typename;
#endif

syscalls.h

#include "enum_support.h"

ENUM_START
ENUM_VALUE(OPEN_FILE,0)
ENUM_VALUE(READ_FILE,1)
ENUM_VALUE(CLOSE_FILE,2)
ENUM_END(Syscall)

And then have test.c and test.s include syscalls.h

gcc -c test.c
gcc -c test.s

Not sure it's very helpful, but I don't know any other way to share enums (which are non existent in assembly).
You could do cpp test.c or cpp -D__ASSEMBLER__ test.s to see the emitted code.