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

/examples/udpsrv.asm

http://github.com/ece291/ece291-pmodelib
Assembly | 127 lines | 84 code | 26 blank | 17 comment | 0 complexity | fe17005112b14f1fa93b59b1bf0bf05c MD5 | raw file
  1. ; A simple UDP server (udpcli connects to this). Runs until a key is pressed
  2. ; By Peter Johnson, 2001
  3. ;
  4. ; $Id: udpsrv.asm,v 1.1 2001/04/12 02:03:18 pete Exp $
  5. %include "lib291.inc"
  6. BITS 32
  7. GLOBAL _main
  8. SECTION .data
  9. _port dw 12345 ; port number (host order)
  10. SECTION .bss
  11. _socket resd 1
  12. _gotdatagram resb 1
  13. _address resb SOCKADDR_size ; SOCKADDR structure
  14. buf_len equ 16*1024
  15. _buf resb buf_len
  16. SECTION .text
  17. _main:
  18. call _LibInit
  19. ; Initialize the socket library
  20. call _InitSocket
  21. test eax, eax
  22. jnz near .done
  23. ; Create a datagram socket
  24. invoke _Socket_create, dword SOCK_DGRAM
  25. test eax, eax
  26. js near .exit
  27. mov [_socket], eax
  28. ; Set up address structure:
  29. ; First the port
  30. invoke _Socket_htons, word [_port]
  31. mov [_address+SOCKADDR.Port], ax
  32. ; Then the address - INADDR_ANY means any address (don't care)
  33. mov dword [_address+SOCKADDR.Address], INADDR_ANY
  34. ; Bind socket to that address and port
  35. invoke _Socket_bind, dword [_socket], dword _address
  36. test eax, eax
  37. jnz near .close
  38. ; Install a callback for incoming datagrams on the socket
  39. invoke _LockArea, ds, dword _gotdatagram, dword 1
  40. invoke _LockArea, cs, dword _SocketHandler, dword _SocketHandler_end-_SocketHandler
  41. invoke _Socket_SetCallback, dword _SocketHandler
  42. test eax, eax
  43. jnz near .close
  44. invoke _Socket_AddCallback, dword [_socket], dword SOCKEVENT_READ
  45. test eax, eax
  46. jnz near .close
  47. .notdone:
  48. ; loop until keypress (exit) or incoming datagram (process it)
  49. mov ah, 1 ; [BIOS] check for keystroke
  50. int 16h
  51. jnz .quit
  52. cmp byte [_gotdatagram], 1
  53. je .gotconn
  54. jmp short .notdone
  55. .gotconn:
  56. ; clear data ready flag
  57. mov byte [_gotdatagram], 0
  58. ; Get the datagram; we don't care about the source address
  59. invoke _Socket_recvfrom, dword [_socket], dword _buf, dword buf_len-1, dword 0, dword 0
  60. test eax, eax
  61. js .notdone ; error on receive
  62. jz .notdone ; no data?
  63. ; Print out the data
  64. mov ecx, eax
  65. xor ebx, ebx
  66. .printloop:
  67. mov ah, 06h
  68. mov dl, [_buf+ebx]
  69. int 21h
  70. inc ebx
  71. cmp ebx, ecx
  72. jb .printloop
  73. jmp short .notdone
  74. .quit:
  75. ; grab the keypress so it doesn't print out after the program exits
  76. xor eax, eax
  77. int 16h
  78. .deinstall:
  79. invoke _Socket_SetCallback, dword 0
  80. .close:
  81. invoke _Socket_close, dword [_socket]
  82. .exit:
  83. call _ExitSocket
  84. .done:
  85. call _LibExit
  86. ret
  87. proc _SocketHandler
  88. .Socket arg 4
  89. .Event arg 4
  90. ; make sure we're getting an event on the socket we're interested in!
  91. mov eax, [ebp+.Socket]
  92. cmp eax, [_socket]
  93. jne .done
  94. mov eax, [ebp+.Event]
  95. cmp eax, SOCKEVENT_READ
  96. jne .done
  97. mov byte [_gotdatagram], 1
  98. .done:
  99. ret
  100. endproc
  101. _SocketHandler_end