Code: Select all
#most of the code here is borrowed from:
#http://jakash3.wordpress.com/2011/01/15/assembly-socket-example/
#minor changes by me
#but man figuring this shit out took a while hahah
#and the debugging I learned
.data
host:
.asciz "www.google.ca"
message:
.string "HELLO MOTHERFUCKING WORLD"
size:
.int 30
unable_to_connect:
.asciz "Unable to connect!"
create_socket_fail:
.asciz "Unable to create socket!"
.text
.globl main
main:
nop
pushl %ebp
movl %esp,%ebp
subl $20,%esp
pushl $host
call gethostbyname
movl 16(%eax), %eax
movl (%eax), %eax
movl (%eax), %eax
movl %eax, -12(%ebp)
movw $2, -16(%ebp)
movl $0, %ecx
movw $80, %cx
movl %ecx, (%esp)
call htons
movw %ax,-14(%ebp)
movl $0,-8(%ebp)
movl $0,-4(%ebp)
subl $8,%esp
#socket
movl $2, (%esp) #First Value of stack pointer AF_INET
movl $1, 4(%esp) #SOCK_STREAM
movl $6, 8(%esp) #IPPROTO_TCP
call socket #call socket
cmpl $-1,%eax
je create_fail #return -1 on fail
# save socket file descriptor
movl %eax, -20(%ebp)
movl %eax, (%esp) #mov sockfd into first value of our new stack pointer
leal -16(%ebp),%ebx
movl %ebx, 4(%esp) #mov host as second arg into esp
movl $16, 8(%esp) #mov sock_len
call connect
cmpl $0, %eax
jnz connect_fail #return 0 if fail
#send
movl -20(%ebp), %ebx
movl %ebx, (%esp)
movl $message, 4(%esp)
movl $size, 8(%esp)
movl $0, 12(%esp)
call send
movl $0, %ebx
movl $1, %eax #exit
int $0x80
connect_fail:
pushl $unable_to_connect
call puts
pushl $1
call exit
create_fail:
pushl $create_socket_fail
call puts
pushl $1
call exit