0

I want to get current date and time in NASM, in some usable format. I know how to get timestamp (with system call sys_time), but there is a lot of work to get usable date and time from timestamp. You would need to calculate every year, month, day and take in account every leap year, leap seconds (how many they were so far? and I cant predict future leap seconds), so I think, there should be a way OS can handle this.

So my question is: Is there a way to get current date and time in usable way so I don't have to calculate it from timestamp?

OS I am using: CentOS 7

According to some, my question is same as: How can I access system time using NASM? well, that's not really true. My question is more specific (that I don't want timestamp) and answers from the above won't help me, because either give me timestamp or don't work at all. Most rated answer count on instruction OUT, which leads to Sefmentation fault on my system.

Community
  • 1
  • 1
Ján Stibila
  • 619
  • 3
  • 12
  • If there is a way OS can handle it, we should know about yours, please tell us. – axelduch Mar 30 '15 at 07:56
  • I edited question and added OS version – Ján Stibila Mar 30 '15 at 07:59
  • possible duplicate of [How can I access system time using NASM?](http://stackoverflow.com/questions/1465927/how-can-i-access-system-time-using-nasm) – David C. Rankin Mar 30 '15 at 08:20
  • Well, one answer from there will get you timestamp (which is what I don't want), other one count on OUT instruction which leads to segmentation fault (with sudo it just end program with no error and exit code 139) – Ján Stibila Mar 30 '15 at 08:57

1 Answers1

0

You could use the time and localtime functions in libc to get a tm struct, and then do whatever you want with the fields in that struct.

Here's an example of how you'd do that (it's in GAS/AT&T syntax because that's the tool that I happened to have available):

.equ tm_sec, 0
.equ tm_min, 4
.equ tm_hour, 8
.equ tm_mday, 12
.equ tm_mon, 16
.equ tm_year, 20
.equ tm_wday, 24
.equ tm_yday, 28
.equ tm_isdst, 32

.bss
now: .space 4

.data
fmt:     .ascii  "%d-%02d-%02d"
.byte 0

.text
.global _main
_main: 

    # Get current time (see 'man time(2)')
    pushl   $now
    call    _time 
    addl    $4,%esp

    # Convert to a tm struct (see 'man localtime(3)')
    pushl   $now
    call    _localtime
    addl    $4,%esp

    # Print the year, month, and day of month fields
    pushl   tm_mday(%eax)
    movl    tm_mon(%eax),%ebx
    incl    %ebx
    pushl   %ebx
    movl    tm_year(%eax),%ebx
    addl    $1900,%ebx
    pushl   %ebx
    pushl   $fmt
    call    _printf
    addl    $16,%esp

    call    _exit
Michael
  • 57,169
  • 9
  • 80
  • 125
  • I'm quite unfamiliar with the syntax (I am new to assembler). Also where did you get _time, _localtime? I'm getting symbol undefined error on those. – Ján Stibila Mar 30 '15 at 13:02
  • Those functions are in `libc`. I'm building with `gcc -o test -lc test.s` – Michael Mar 30 '15 at 13:03
  • `.equ` is like `%define`. `.space` is like `resb`. `pushl tm_mday(%eax)` is like `push dword [eax + tm_mday]`. Oh, and the order of the operands are reversed (in AT&T syntax you write `src, dest` instead of `dest, src`). – Michael Mar 30 '15 at 13:05