PageRenderTime 27ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/tcpweb.asm

http://github.com/ece291/ece291-pmodelib
Assembly | 93 lines | 61 code | 19 blank | 13 comment | 0 complexity | 93b7272f49a93bc079244fdda384f0fe MD5 | raw file
  1. ; Test program to get a webpage from a TCP server
  2. ; By Peter Johnson, 2001
  3. ;
  4. ; $Id: tcpweb.asm,v 1.3 2001/04/11 21:09:24 pete Exp $
  5. %include "lib291.inc"
  6. BITS 32
  7. GLOBAL _main
  8. SECTION .data
  9. _website db "courses.ece.uiuc.edu",0
  10. _getstring db "GET /ece291/",13,10,0
  11. getstring_len equ $-_getstring
  12. SECTION .bss
  13. _socket resd 1
  14. _address resb SOCKADDR_size ; SOCKADDR structure
  15. recvbuf_len equ 16*1024
  16. _recvbuf resb recvbuf_len
  17. SECTION .text
  18. _main:
  19. call _LibInit
  20. ; Initialize the socket library
  21. call _InitSocket
  22. test eax, eax
  23. jnz near .done
  24. ; Create a socket
  25. invoke _Socket_create, dword SOCK_STREAM
  26. test eax, eax
  27. js near .exit
  28. mov [_socket], eax
  29. ; Set up address structure:
  30. ; First the port
  31. invoke _Socket_htons, word 80 ; HTTP port
  32. mov [_address+SOCKADDR.Port], ax
  33. ; Then the address
  34. invoke _Socket_gethostbyname, dword _website
  35. test eax, eax
  36. jz near .close
  37. mov eax, [eax+HOSTENT.AddrList] ; Get pointer to address list
  38. mov eax, [eax] ; Get pointer to first address
  39. test eax, eax ; Valid pointer?
  40. jz near .close
  41. mov eax, [eax] ; Get first address
  42. mov [_address+SOCKADDR.Address], eax
  43. ; Connect to the remote host
  44. invoke _Socket_connect, dword [_socket], dword _address
  45. test eax, eax
  46. jnz near .close
  47. ; Send GET string
  48. invoke _Socket_send, dword [_socket], dword _getstring, dword getstring_len, dword 0
  49. test eax, eax
  50. js .close
  51. ; Receive data until no data left
  52. .getmore:
  53. invoke _Socket_recv, dword [_socket], dword _recvbuf, dword recvbuf_len-1, dword 0
  54. test eax, eax
  55. js .close
  56. jz .close
  57. ; Print out the data
  58. mov ecx, eax
  59. xor ebx, ebx
  60. .printloop:
  61. mov ah, 06h
  62. mov dl, [_recvbuf+ebx]
  63. int 21h
  64. inc ebx
  65. cmp ebx, ecx
  66. jb .printloop
  67. jmp short .getmore
  68. .close:
  69. invoke _Socket_close, dword [_socket]
  70. .exit:
  71. call _ExitSocket
  72. .done:
  73. call _LibExit
  74. ret