0

i'm learning Assembler (Nasm, Linux, Ubuntu 16.4, x86_64) and getting Trouble using the sys_time call (mov eax, 13).

section .bss
    time: resb 30;

section .data
    msg: db "The Time is:";
    msgLen: equ $-msg;
    blank: db 0x0a;
    blankLen: equ $-blank;

section .text
global _start:
_start:

    mov eax, 13;
    mov ebx, time;
    int 80h;

    mov eax, 4;
    mov ebx, 1;
    mov ecx, msg;
    mov edx, msgLen;
    int 80h;

    mov eax, 4;
    mov ebx, 1;
    mov edx, time;
    mov ecx, 30;
    int 80;

    mov eax, 4;
    mov ebx, 1;
    mov ecx, blank;
    mov edx, blankLen;
    int 80h;

    mov eax, 1;
    mov ebx, 0;
    int 80h;

The error message is (translated with google) (Written dump ) segfaulting if someone knows german here the German error Message Speicherzugriffsfehler (Speicherabzug geschrieben)

my thougts: maybe resb reserves space for a string, but how can i convert the Integer to a string?? Or do i have to declare a Integer? or is my kernel broke?

Maximilian Wittmer
  • 173
  • 1
  • 3
  • 14

1 Answers1

3

You have int 80 instead of int 80h on one line. And ecx/edx have the values other way.

After printing msg the next code should be:

mov eax, 4
mov ebx, 1
mov ecx, time
mov edx, 30
int 80h

(It will not do, what you expect, anyway, as the sys_time does not return string, so you can't display it directly).


Checking the sys_time docs on internet... you should call it rather with xor ebx,ebx (NULL buffer pointer), and use the returned eax value. Giving it the buffer pointer is now obsolete way of call. It's number of seconds since Epoch (1970-01-01 00:00:00 +0000 (UTC)).

So at the beginning of your code you can do:

mov eax, 13
xor ebx,ebx
int 80h
mov [time],eax

It's still not solving how to display it (nor I will even try, depends what you really want to achieve, whether just linking against clib is enough for you, and then use the C functions to format the time, or you want to create the seconds_number->time_string formatter from scratch on your own).

Ped7g
  • 16,236
  • 3
  • 26
  • 63
  • @TheFrenchPlaysHdMicraftn http://stackoverflow.com/a/2538212/4271923 _"> The return value is in `%eax`."_ And sys_time description in [man pages](http://man7.org/linux/man-pages/man2/time.2.html). _"> On success, the value of time in seconds since the Epoch is returned."_ – Ped7g Sep 09 '16 at 14:07
  • @TheFrenchPlaysHdMicraftn: See [What are the return values of system calls in Assembly?](http://stackoverflow.com/a/38752895/224132). where I explain what it means to make system calls in asm, and how to understand the docs. See also the [x86 Linux Hello World example](http://stackoverflow.com/documentation/x86/1164/introduction-to-x86/19078/x86-linux-hello-world-example#t=201609091552385545012) on SO Docs. Let me know if it was helpful; I wrote it but haven't heard feedback from anyone that didn't already know that stuff. – Peter Cordes Sep 09 '16 at 16:01
  • so basycally sys_time stores the returned value in eax? – Maximilian Wittmer Sep 09 '16 at 18:26
  • Yes, as you can check in the man page: sys_time C prototype is `time_t time(time_t *tloc);`, which together with the int 80h ABI means the value is returned in `eax`. And the `time_t` for legacy 32b linux system looks to be 32b int, while in 64b linux it's 64b int. Mind you, the whole `int 80h` API is obsolete and not stable to be programmed against. To be super-correct, you should dynamically link with clib, and call sys calls trough the latest OS clib library (practically the current asm binaries you create with int 80h may work for decades, no need to panic). – Ped7g Sep 09 '16 at 18:39
  • @TheFrenchPlaysHdMicraftn I'm just making sure you understand you are learning something highly impractical (the `int 80h` API). Rather focus on the x86 machine code itself, I would mix ASM + C++, ASM for high level programming, C++ to call all the OS low level stuff. I mean, even x86 ASM knowledge will become obsolete over time (also it's extending massively with each new generation of x86), but it's IMHO knowledge with better usability in the long term. – Ped7g Sep 09 '16 at 18:42
  • so basicall you mean, i should use c++ to call the syscall on the stack, but isn't there a GOOD solution, doing it INSIDE the asm? – Maximilian Wittmer Sep 09 '16 at 19:06
  • and: other question: how can i display the time? i – Maximilian Wittmer Sep 09 '16 at 19:07
  • @TheFrenchPlaysHdMicraftn I wrote it already. Inside asm you "should" [dynamically load clib shared library from the linux](http://www.muppetlabs.com/~breadbox/software/tiny/somewhat.html) (don't really bother, keep using `int 80h` for learning purposes). How to display time: any way you wish? I don't know what you want to display, you have "seconds since Epoch" number, so you can calculate many values from it and then format them for displaying. Personally I don't find this interesting, it's tedious work to get it right, that's why there are so many time/date libraries. – Ped7g Sep 09 '16 at 19:41
  • ok. but if i just want to write the value returned by `mov eax, 13`. because, when i try to write it, nothing appears to the screen – Maximilian Wittmer Sep 10 '16 at 07:16