Sleep program in Linux Gas asm

This little program uses nanosleep syscall to sleep a specified amount of seconds. It accepts 2 arguments; 1st argument is how many seconds, and 2nd arg is how many nanoseconds.

Sleep - by Jakash3
For Linux; Usage: sleep sec nanosec

It also uses atoi from C library to convert string arguments into integers.

.globl _start
pushl %ebp
movl %esp,%ebp          #New stack frame
movl 4(%ebp),%ecx       #Get argc
cmpl $3,%ecx
jne help                #Show help if argc!=3
movl 16(%ebp),%eax      #Get argv[2]
pushl %eax
call atoi               #Convert to int
movl %eax,(%esp)        #Save it to stack
movl 12(%ebp),%eax      #Get argv[1]
pushl %eax
call atoi               #Convert to int
movl %eax,(%esp)        #Save it to stack on top of converted argv[2]
                        #Now we have our timespec struct on the
leal (%esp),%ebx        #Address of timespec struct in ebx
movl $162,%eax          #nanosleep syscall function
movl $0,%ecx            #Useless argument set to null
int $0x80               #EXECUTE CALL
leave                   #Pop off stack frame
jmp endstart
movl $4,%eax            #Write syscall function
movl $1,%ebx            #Write to stdout
movl $helpmsg,%ecx      #Address of data
movl $55,%edx           #Number of bytes to write
int $0x80               #EXECUTE CALL
movl $1,%eax            #Exit syscall function
movl $0,%ebx            #Return status 0
int $0x80               #EXECUTE CALL

helpmsg: .ascii "Sleep - by Jakash3\nFor Linux; Usage: sleep sec nanosec\n"

To assemble run these:

as -o sleep.o sleep.asm
ld -I /lib/ -o sleep -lc sleep.o

Example usage:

./sleep 3 0

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: