flat assembler
Message board for the users of flat assembler.
Index
> Linux > struct tm, time_t and other C stdlib types |
Author |
|
rugxulo 28 Apr 2011, 02:31
I know this is a dumb reply (esp. from a Linux noob), but you could take a look at FreePascal's Linux runtime and/or output of whatever are their equivalents of the C's time.h functions. They avoid libc completely, so that should be more useful to you, IMHO.
Oops, or check AsmUtils, that's probably easier, heh. |
|||
28 Apr 2011, 02:31 |
|
Endre 01 May 2011, 19:02
I guess, you've just encountered the most serious problem of assembly programming. My short answer: there is no perfect way. In C projects where I also have to deploy some assembly code I use the following (little bit illegal) trick:
globals.c: Code: #include <time.h> #define offsetof(type, member) __builtin_offsetof(type, member) /* global variables */ struct tm star_date; time_t my_time; void global_symbols(void) { /* sizeof */ asm(".globl tm_sizeof;" "tm_sizeof = %0" :: "i" (sizeof(struct tm))); /* for member offset */ asm(".globl tm.tm_year;" "tm.tm_year = %0" :: "i" (offsetof(struct tm, tm_year))); } main.S: Code: .intel_syntax noprefix .text .globl main main: /* get epoch time */ mov rdi, OFFSET my_time xor eax, eax call time /* get local time in star_date */ mov rdi, OFFSET my_time mov rsi, OFFSET star_date xor eax, eax call localtime_r /* change the date a bit */ mov rax, OFFSET star_date add dword ptr [rax + tm.tm_year], 1000 /* convert it to string */ mov rdi, OFFSET star_date xor eax, eax call asctime /* print the result */ mov rdi, OFFSET format_string mov rsi, rax xor eax, eax call printf /* exit */ xor eax, eax ret format_string: .asciz "Bender's date: %s\n" In this way, if the struct changes your build system will re-link (symbols are always assumed to be addresses by the assembler, so no re-compilation is needed) your assembly module properly. Sorry, the code above is for gnu-as, but you can easily change it to fasm, but as I remember then you will have to declare all extern symbols. By the way this kind of trick also works with other c-compilers/assemblers as for instance wind-river or green-hills. you can build the program with: Code: gcc -masm=intel main.S globals.c -o test or with Code: gcc -masm=intel -fdata-sections -ffunction-sections main.S globals.c -o test -Wl,--gc-sections |
|||
01 May 2011, 19:02 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.