PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/tcpcli.asm

http://github.com/ece291/ece291-pmodelib
Assembly | 88 lines | 56 code | 20 blank | 12 comment | 0 complexity | 963ce02f5a016d57a4823bf566f02eda MD5 | raw file
  1. ; A simple TCP client (connects to tcpsrv example)
  2. ; By Peter Johnson, 2001
  3. ;
  4. ; $Id: tcpcli.asm,v 1.2 2001/04/11 21:10:23 pete Exp $
  5. %include "lib291.inc"
  6. BITS 32
  7. GLOBAL _main
  8. SECTION .data
  9. _message db "Hello World!",13,10,0 ; message to send to server
  10. message_len equ $-_message
  11. _port dw 12345 ; port number (host order)
  12. SECTION .bss
  13. _socket resd 1
  14. _address resb SOCKADDR_size ; SOCKADDR structure
  15. buf_len equ 16*1024
  16. _buf resb buf_len
  17. SECTION .text
  18. proc _main
  19. .argc arg 4
  20. .argv arg 4
  21. call _LibInit
  22. ; Initialize the socket library
  23. call _InitSocket
  24. test eax, eax
  25. jnz near .done
  26. ; Create a socket
  27. invoke _Socket_create, dword SOCK_STREAM
  28. test eax, eax
  29. js near .exit
  30. mov [_socket], eax
  31. ; Set up address structure:
  32. ; First the port
  33. invoke _Socket_htons, word [_port]
  34. mov [_address+SOCKADDR.Port], ax
  35. ; Then the address: if commandline argument, connect to that hostname,
  36. ; otherwise connect to localhost (lookback address)
  37. cmp dword [ebp+.argc], 1
  38. jbe .uselocalhost
  39. mov eax, [ebp+.argv] ; Get pointer to argv
  40. invoke _Socket_gethostbyname, dword [eax+4] ; use argv[1]
  41. test eax, eax
  42. jz near .close
  43. mov eax, [eax+HOSTENT.AddrList] ; Get pointer to address list
  44. mov eax, [eax] ; Get pointer to first address
  45. test eax, eax ; Valid pointer?
  46. jz near .close
  47. mov eax, [eax] ; Get first address
  48. jmp short .doconnect
  49. .uselocalhost:
  50. invoke _Socket_htonl, dword INADDR_LOOPBACK
  51. .doconnect:
  52. mov [_address+SOCKADDR.Address], eax
  53. ; Connect to the remote host
  54. invoke _Socket_connect, dword [_socket], dword _address
  55. test eax, eax
  56. jnz near .close
  57. ; Send a little message (the server will print it out)
  58. invoke _Socket_send, dword [_socket], dword _message, dword message_len, dword 0
  59. test eax, eax
  60. js .close ; error on send
  61. .close:
  62. invoke _Socket_close, dword [_socket]
  63. .exit:
  64. call _ExitSocket
  65. .done:
  66. call _LibExit
  67. ret
  68. endproc