Assembly Socket Example

Connects to towel.blinkenlights.nl on 666 and outputs data received.

socktest.s:

.text
.globl _start
_start:
pushl %ebp
movl %esp,%ebp
subl $20,%esp           #Space for sockaddr_in (16 bytes) and socket descriptor (4 bytes)
pushl $host
call gethostbyname      #Return hostent of "towel.blinkenlights.nl"
movl 16(%eax),%eax      #Get hostent.h_addr_list (array of pointers to in_addr's)
movl (%eax),%eax        #Get h_addr_list[0]
movl (%eax),%eax        #Dereference this pointer to the in_addr
movl %eax,-12(%ebp)     #Put it in our sockaddr_in.sin_addr
movw $2,-16(%ebp)       #sockaddr_in.sin_family = AF_INET
movl $0,%ecx				
movw $666,%cx           #Zero-extended ushort port number
movl %ecx,(%esp)
call htons              #Convert to network-byte order
movw %ax,-14(%ebp)      #Place it in sockaddr_in.sin_port
movl $0,-8(%ebp)			
movl $0,-4(%ebp)        #Zero out sockaddr_in.sin_zero
subl $8,%esp            #More arg space for socket() 
movl $2,(%esp)          #AF_INET
movl $1,4(%esp)         #SOCK_STREAM
movl $6,8(%esp)         #IPPROTO_TCP
call socket             #Create socket
cmpl $-1,%eax
je fail                 #Returns -1 on fail
movl %eax,-20(%ebp)     #Otherwise save socket descriptor.
movl %eax,(%esp)        #It's already in eax, load it as arg for connect
leal -16(%ebp),%ebx     #Address of our sockaddr_in
movl %ebx,4(%esp)       #2nd arg for connect
movl $16,8(%esp)        #Size of sockaddr_in
call connect
cmpl $0,%eax
jnz fail                #If not 0, fail
subl $244,%esp          #16 for sockaddr, 4 for sockfd, 12 for temp func args plus 244 for
movl %esp,%esi          #a 256 byte buffer
movl -20(%ebp),%ebx     #Load socket descriptor
subl $16,%esp           #More arg space on top of 256 byte buffer
movl %ebx,(%esp)        #sockfd
movl %esi,4(%esp)       #buffer pointer
movl $256,8(%esp)       #Length of buffer
movl $0,12(%esp)        #Socket flags
call recv               #recv data 2 times since
call recv               #first data sent by server is not the ascii message
addl $4,%esp            #Pop off sockfd arg to reveal buffer pointer arg on top
call puts               #Print it
movl -20(%ebp),%ebx
movl %ebx,(%esp)        #Load and push sockfd as arg
movl $2,4(%esp)         #Shutdown both send and recv
call shutdown
call close              #sockfd already on top, close this sockfd
movl $0,(%esp)
call exit

fail:
pushl $errmsg
call puts
pushl $1
call exit

.data
host: .asciz "towel.blinkenlights.nl"
errmsg: .asciz "Something bad happened."

Assemble on GNU Linux with:

as -o socktest.o socktest.s
ld -I /lib/ld-linux.so.2 -o socktest socktest.o

5 thoughts on “Assembly Socket Example

  1. hello Jakash3
    plz sorry for my ugly english -)
    i have some simple project for you.
    can we disscuss about project and how mutch money do you wont for it?
    plz contac me for disscuss.
    P.S. i did find ur email -(
    plz, contact me: email: reactorx@gmail.com

  2. I tried to link the socktest.o but I do not know how to link them properly with “standard libraries”.
    The following is the errors I got when run “ld -I /lib/ld-linux.so.2 -o socktest socktest.o”

    $ ld -I /lib/ld-linux.so.2 -o socktest socktest.o
    socktest.o: In function `_start’:
    (.text+0xc): undefined reference to `gethostbyname’
    socktest.o: In function `_start’:
    (.text+0x2d): undefined reference to `htons’
    socktest.o: In function `_start’:
    (.text+0x5e): undefined reference to `socket’
    socktest.o: In function `_start’:
    (.text+0x7d): undefined reference to `connect’
    socktest.o: In function `_start’:
    (.text+0xac): undefined reference to `recv’
    socktest.o: In function `_start’:
    (.text+0xb1): undefined reference to `recv’
    socktest.o: In function `_start’:
    (.text+0xb9): undefined reference to `puts’
    socktest.o: In function `_start’:
    (.text+0xcc): undefined reference to `shutdown’
    socktest.o: In function `_start’:
    (.text+0xd1): undefined reference to `close’
    socktest.o: In function `_start’:
    (.text+0xdd): undefined reference to `exit’
    socktest.o: In function `fail’:
    (.text+0xe7): undefined reference to `puts’
    socktest.o: In function `fail’:
    (.text+0xee): undefined reference to `exit’

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: