-
May 21st, 2009, 03:30 PM
#1
Linux assembly: Printing date and time
Hello, I'm trying to print date and time in linux usign GAS (GNU assembler). I started from a hello world code and I modified to show system date and time.
I'm stuck in two very basic errors I can't seem to find solution. Here's the code:
Code:
##############################################################################
## getdatetime.s ##
## ##
## Compile with: ##
## as -o getdatetime.o getdatetime.s && ld -o getdatetime -O0 getdatetime.o ##
## ##
##############################################################################
.section .data
.section .text
.globl _start
_start:
# call function to print date and time
jmp print_date_time
## terminate program via _exit () system call
xorl %eax, %eax # %eax = 0
incl %eax # %eax = 1 system call _exit ()
xorl %ebx, %ebx # %ebx = 0 normal program return code
int $0x80 # execute system call _exit ()
## get system date and time and push it in stack: sec/min/hour/day/mon/year
print_date_time:
movl $78, %eax # getdatetime () system call
int $0x80 # execute system call getdatetime ()
addl $0x48, %eax # convert to ascii value
mov %al, %ecx # print %al to see if we have something interesting in it (sec, min, hour, day, mon, year) or something
movl $1, %edx # %edx = 1 = print length
movl $4, %eax # write () system call
int $0x80 # execute write () system call
ret
When I compile with
Code:
as -o getdatetime.o getdatetime.s && ld -o getdatetime -O0 getdatetime.o
It gives me an error:
Code:
getdatetime.s: Assembler messages:
getdatetime.s:27: Error: suffix or operands invalid for `mov'
Line 27 is:
Code:
mov %al, %ecx # print %al to see if we have something interesting in it (sec, min, hour, day, mon, year) or something
Thanks for your attention.
Last edited by bubu; May 21st, 2009 at 03:42 PM.
All consequences are eternal in some way.
-
May 21st, 2009, 03:49 PM
#2
Re: Linux assembly: Printing date and time
I changed line 27 to use eax instead. Now it compiles. But I get segmentation fault when I run the program!
Code currently is:
Code:
.section .data
.section .text
.globl _start
_start:
# call function to print date and time
jmp print_date_time
## terminate program via _exit () system call
xorl %eax, %eax # %eax = 0
incl %eax # %eax = 1 system call _exit ()
xorl %ebx, %ebx # %ebx = 0 normal program return code
int $0x80 # execute system call _exit ()
## get system date and time and push it in stack: sec/min/hour/day/mon/year
print_date_time:
movl $0x78, %eax # getdatetime () system call
int $0x80 # execute system call getdatetime ()
addl $0x48, %eax # convert to ascii value
movl %eax, %ecx # print %eax to see if we have something interesting in it (sec, min, hour, day, mon, year) or something
movl $4, %edx # %edx = 1 = print length
movl $4, %eax # write () system call
int $0x80 # execute write () system call
ret
All consequences are eternal in some way.
-
May 22nd, 2009, 04:23 AM
#3
Re: Linux assembly: Printing date and time
mov is Intel syntax and movl is AT&T syntax
See http://asm.sourceforge.net/articles/linasm.html
With AT&T syntax, the fourth letter, just after mov can be b for a byte (8 bits), w for a word (16 bits), or l for a long word (32 bits).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|