flat assembler
Message board for the users of flat assembler.

Index > Linux > struct tm, time_t and other C stdlib types

Thread Post new topic Reply to topic

Joined: 27 Mar 2011
Posts: 1
Ploe 25 Apr 2011, 20:17
I'm so new to Assembly it hurts.

I'm rewriting a program in FASM that I initially wrote in C. I'm aware that I need to use the C std library to use the time.h functions but I'm not sure how to use the types like time_t and struct tm. Do I define them myself? If so how?

Please let me know...

Post 25 Apr 2011, 20:17
View user's profile Send private message AIM Address MSN Messenger Reply with quote

Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
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.
Post 28 Apr 2011, 02:31
View user's profile Send private message Visit poster's website Reply with quote

Joined: 29 Dec 2003
Posts: 215
Location: Budapest, Hungary
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:

#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)));

        .intel_syntax noprefix
        .globl 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 Smile */
        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

        .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:
gcc -masm=intel main.S globals.c -o test    

or with
gcc -masm=intel -fdata-sections -ffunction-sections  main.S globals.c -o test -Wl,--gc-sections    
to have the linker remove that unused global_symbols function.
Post 01 May 2011, 19:02
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum

Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.