Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first problem here is that you need to understand the sys_time sys call. There is a handy chart at <a href="http://syscalls.kernelgrok.com/" rel="noreferrer">http://syscalls.kernelgrok.com/</a> that tells you what the various sys calls require as input in registers.</p> <p>sys_time is system call 13, so</p> <pre><code>mov eax,13 </code></pre> <p>is good</p> <p>However sys_time also requires a memory address to be passed in ebx, where it writes the actual time.</p> <p>A quick way would be to allocate some space on the stack (we can push anything on the stack, the sys_time value will overwrite it, why not stick the value of eax onto it).</p> <pre><code>push eax </code></pre> <p>Then feed the stack pointer into ebx</p> <pre><code>mov ebx, esp </code></pre> <p>Now make the system call</p> <pre><code>int 80h </code></pre> <p>Now we can pop the time off the stack (into e.g. eax)</p> <pre><code>pop eax </code></pre> <p>Now eax contains the current unix time (i.e. number of seconds since Jan 01 1970.)</p> <p>To avoid the trickyness of printing numbers directly to the unix console I will cheat and provide a complete example that compiles in nasm and links with the c library via gcc and uses printf</p> <pre><code>[SECTION .data] PrintNum db "%d",10,0 ;this is a c string so is null terminated [SECTION .text] extern printf global main main: push ebp mov ebp,esp push ebx push esi push edi ; stuff before this for glibc compatibility mov eax, 13 push eax mov ebx, esp int 0x80 pop eax push eax ; push eax onto stack then the format string, then call printf to write eax to console, unwind stack pointer push PrintNum call printf add esp,8 pop edi ; stuff after this for glibc compatibility pop esi pop ebx mov esp,ebp pop ebp ret </code></pre> <p>Compile with</p> <pre><code>nasm -f elf sys_time.asm gcc sys-time.o -o sys-time </code></pre> <p>Though if you are on 64-bit linux you might have to do (and have the relevant multilib gcc and glibc). You can't compile this program as a native 64-bit executable because it uses push and pop and you can't push a 32 bit register onto a 64 bit stack.</p> <pre><code>nasm -f elf32 sys_time.asm gcc -m32 sys-time.o -o sys-time </code></pre> <p>Then you should get</p> <pre><code>$ ./systime 1310190574 </code></pre> <p>I've tested this on 32 bit and 64 bit linux and managed to get the above code compiled. Let me know if you have any problems.</p> <p>To answer your question regarded nasm tutorials, I have been learning recently from "Assembly Language Step By Step, Third Edition" by Jeff Duntemann. See <a href="http://www.duntemann.com/assembly.html" rel="noreferrer">http://www.duntemann.com/assembly.html</a> for details and sample chapters.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload