PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/nasm/socket.asm

https://android.googlesource.com/toolchain/yasm
Assembly | 1513 lines | 613 code | 900 blank | 0 comment | 0 complexity | ceddce4c2cacb1e82c505ffd0263414b MD5 | raw file
Possible License(s): LGPL-2.0, BSD-3-Clause, GPL-2.0
  1. INADDR_ANY equ 0
  2. INADDR_LOOPBACK equ 7F000001h
  3. INADDR_BROADCAST equ 0FFFFFFFFh
  4. INADDR_NONE equ 0FFFFFFFFh
  5. SOCK_STREAM equ 1
  6. SOCK_DGRAM equ 2
  7. SOCKEVENT_READ equ 01h
  8. SOCKEVENT_WRITE equ 02h
  9. SOCKEVENT_OOB equ 04h
  10. SOCKEVENT_ACCEPT equ 08h
  11. SOCKEVENT_CONNECT equ 10h
  12. SOCKEVENT_CLOSE equ 20h
  13. [absolute 0]
  14. SOCKADDR:
  15. .Port resw 1
  16. .Address resd 1
  17. SOCKADDR_size:
  18. [section .text]
  19. [absolute 0]
  20. HOSTENT:
  21. .Name resd 1
  22. .Aliases resd 1
  23. .AddrList resd 1
  24. HOSTENT_size:
  25. [section .text]
  26. [extern DPMI_Int]
  27. [extern _AllocTransferBuf]
  28. [extern _FreeTransferBuf]
  29. [extern DPMI_Regs]
  30. [extern DPMI_EDI]
  31. [extern DPMI_ESI]
  32. [extern DPMI_EBP]
  33. [extern DPMI_EBX]
  34. [extern DPMI_EDX]
  35. [extern DPMI_ECX]
  36. [extern DPMI_EAX]
  37. [extern DPMI_FLAGS]
  38. [extern DPMI_ES]
  39. [extern DPMI_DS]
  40. [extern DPMI_FS]
  41. [extern DPMI_GS]
  42. [extern DPMI_SP]
  43. [extern DPMI_SS]
  44. [extern _Transfer_Buf]
  45. [extern _Transfer_Buf_Seg]
  46. [extern _Transfer_Buf_Size]
  47. DPMI_EDI_off equ 00h
  48. DPMI_ESI_off equ 04h
  49. DPMI_EBP_off equ 08h
  50. DPMI_EBX_off equ 10h
  51. DPMI_EDX_off equ 14h
  52. DPMI_ECX_off equ 18h
  53. DPMI_EAX_off equ 1Ch
  54. DPMI_FLAGS_off equ 20h
  55. DPMI_ES_off equ 22h
  56. DPMI_DS_off equ 24h
  57. DPMI_FS_off equ 26h
  58. DPMI_GS_off equ 28h
  59. DPMI_IP_off equ 2Ah
  60. DPMI_CS_off equ 2Ch
  61. DPMI_SP_off equ 2Eh
  62. DPMI_SS_off equ 30h
  63. [extern _AllocMem]
  64. [extern _AllocSelector]
  65. [extern _FreeSelector]
  66. [extern _GetPhysicalMapping]
  67. [extern _FreePhysicalMapping]
  68. [extern _LockArea]
  69. _AllocMem_arglen equ 4
  70. _AllocSelector_arglen equ 4
  71. _FreeSelector_arglen equ 2
  72. _GetPhysicalMapping_arglen equ 16
  73. _FreePhysicalMapping_arglen equ 8
  74. _LockArea_arglen equ 10
  75. [extern _Install_Int]
  76. [extern _Remove_Int]
  77. [extern _Exit_IRQ]
  78. [extern _Init_IRQ]
  79. [extern _Restore_IRQ]
  80. [extern _Enable_IRQ]
  81. [extern _Disable_IRQ]
  82. _Install_Int_arglen equ 8
  83. _Remove_Int_arglen equ 4
  84. _Restore_IRQ_arglen equ 4
  85. _Enable_IRQ_arglen equ 4
  86. _Disable_IRQ_arglen equ 4
  87. [bits 32]
  88. SOCKET_INIT equ 5001h
  89. SOCKET_EXIT equ 5002h
  90. SOCKET_ACCEPT equ 5003h
  91. SOCKET_BIND equ 5004h
  92. SOCKET_CLOSE equ 5005h
  93. SOCKET_CONNECT equ 5006h
  94. SOCKET_GETPEERNAME equ 5007h
  95. SOCKET_GETSOCKNAME equ 5008h
  96. SOCKET_INETADDR equ 5009h
  97. SOCKET_INETNTOA equ 500Ah
  98. SOCKET_LISTEN equ 500Bh
  99. SOCKET_RECV equ 500Ch
  100. SOCKET_RECVFROM equ 500Dh
  101. SOCKET_SEND equ 500Eh
  102. SOCKET_SENDTO equ 500Fh
  103. SOCKET_SHUTDOWN equ 5010h
  104. SOCKET_CREATE equ 5011h
  105. SOCKET_GETHOSTBYADDR equ 5012h
  106. SOCKET_GETHOSTBYNAME equ 5013h
  107. SOCKET_GETHOSTNAME equ 5014h
  108. SOCKET_INSTALLCALLBACK equ 5015h
  109. SOCKET_REMOVECALLBACK equ 5016h
  110. SOCKET_ADDCALLBACK equ 5017h
  111. SOCKET_GETCALLBACKINFO equ 5018h
  112. STRING_MAX equ 256
  113. HOSTENT_ALIASES_MAX equ 16
  114. HOSTENT_ADDRLIST_MAX equ 16
  115. [section .bss]
  116. Callback_Address resd 1
  117. Callback_Int resb 1
  118. Multiplex_Handle resw 1
  119. VDD_Handle resw 1
  120. LastError resd 1
  121. NetAddr_static resb STRING_MAX
  122. HostEnt_Name_static resb STRING_MAX
  123. HostEnt_Aliases_static resd HOSTENT_ALIASES_MAX
  124. HostEnt_AddrList_static resd HOSTENT_ADDRLIST_MAX
  125. HostEnt_Aliases_data resb STRING_MAX*HOSTENT_ALIASES_MAX
  126. HostEnt_AddrList_data resd HOSTENT_ADDRLIST_MAX
  127. [section .data]
  128. rcsid db '$Id: socket.asm,v 1.1 2001/11/21 08:41:53 peter Exp $',0
  129. ALTMPX_Signature db 'ECE291 ', 'EX291 ', 0
  130. ALTMPX_MinVersion equ 0100h
  131. SOCKET_Version equ 0100h
  132. HostEnt_static :
  133. ..@44.strucstart:
  134. times HOSTENT.Name-($-..@44.strucstart) db 0
  135. dd HostEnt_Name_static
  136. times HOSTENT.Aliases-($-..@44.strucstart) db 0
  137. dd HostEnt_Aliases_static
  138. times HOSTENT.AddrList-($-..@44.strucstart) db 0
  139. dd HostEnt_AddrList_static
  140. times HOSTENT_size-($-..@44.strucstart) db 0
  141. VDD_InitData
  142. dw SOCKET_Version
  143. dd STRING_MAX
  144. dd HOSTENT_ALIASES_MAX
  145. dd HOSTENT_ADDRLIST_MAX
  146. dd LastError
  147. dd NetAddr_static
  148. dd HostEnt_Name_static
  149. dd HostEnt_Aliases_static
  150. dd HostEnt_AddrList_static
  151. dd HostEnt_Aliases_data
  152. dd HostEnt_AddrList_data
  153. [section .text]
  154. [global _InitSocket]
  155. _InitSocket
  156. push esi
  157. push edi
  158. push es
  159. cld
  160. xor eax, eax
  161. mov cx, 1
  162. int 31h
  163. jc near .error
  164. mov bx, ax
  165. mov ax, 0008h
  166. xor ecx, ecx
  167. mov dx, 0FFFFh
  168. int 31h
  169. jc near .errorfree
  170. xor eax, eax
  171. .loop:
  172. mov esi, eax
  173. mov [DPMI_EAX], eax
  174. push bx
  175. mov bx, 2Dh
  176. call DPMI_Int
  177. pop bx
  178. cmp byte [DPMI_EAX], 0FFh
  179. jne .next
  180. cmp word [DPMI_ECX], ALTMPX_MinVersion
  181. jb .next
  182. mov ax, 0007h
  183. movzx edx, word [DPMI_EDX]
  184. shl edx, 4
  185. mov ecx, edx
  186. shr ecx, 16
  187. int 31h
  188. jc .errorfree
  189. mov es, ebx
  190. movzx edi, word [DPMI_EDI]
  191. mov ecx, 16/2
  192. mov esi, ALTMPX_Signature
  193. rep cmpsw
  194. jz .foundmpx
  195. .next:
  196. inc ah
  197. jnz .loop
  198. jmp short .errorfree
  199. .foundmpx:
  200. mov ax, 0001h
  201. int 31h
  202. mov eax, [DPMI_EAX]
  203. mov [Multiplex_Handle], ax
  204. mov al, 10h
  205. mov [DPMI_EAX], ax
  206. push bx
  207. mov bx, 2Dh
  208. call DPMI_Int
  209. pop bx
  210. cmp byte [DPMI_EAX], 1
  211. jne .error
  212. mov dx, [DPMI_EDX]
  213. mov [VDD_Handle], dx
  214. mov ecx, VDD_InitData
  215. clc
  216. movzx eax, word [VDD_Handle]
  217. or eax, SOCKET_INIT<<16
  218. db 0C4h, 0C4h, 058h, 002h
  219. jc .error
  220. xor eax, eax
  221. jmp short .exit
  222. .errorfree:
  223. mov ax, 0001h
  224. int 31h
  225. .error:
  226. xor eax, eax
  227. inc eax
  228. .exit:
  229. pop es
  230. pop edi
  231. pop esi
  232. ret
  233. [global _ExitSocket]
  234. _ExitSocket
  235. clc
  236. movzx eax, word [VDD_Handle]
  237. or eax, SOCKET_EXIT<<16
  238. db 0C4h, 0C4h, 058h, 002h
  239. ret
  240. [global _Socket_SetCallback]
  241. _Socket_SetCallback: push ebp
  242. mov ebp, esp
  243. .HandlerAddress equ 8
  244. mov ax, [Multiplex_Handle]
  245. mov al, 11h
  246. mov [DPMI_EAX], ax
  247. mov bx, 2Dh
  248. call DPMI_Int
  249. cmp byte [DPMI_EAX], 1
  250. jne near .error
  251. movzx edx, byte [DPMI_EDX]
  252. mov [Callback_Int], dl
  253. mov ecx, [Callback_Address]
  254. mov eax, [ebp+.HandlerAddress]
  255. mov [Callback_Address], eax
  256. test eax, eax
  257. jz near .deinstall
  258. test ecx, ecx
  259. jnz near .ok
  260. push dword 4
  261. push dword Callback_Address
  262. o16 push ds
  263. call _LockArea
  264. add esp, byte _LockArea_arglen
  265. push dword 1
  266. push dword Callback_Int
  267. o16 push ds
  268. call _LockArea
  269. add esp, byte _LockArea_arglen
  270. push dword Socket_InterruptHandler_end-Socket_InterruptHandler
  271. push dword Socket_InterruptHandler
  272. o16 push cs
  273. call _LockArea
  274. add esp, byte _LockArea_arglen
  275. movzx edx, byte [DPMI_EDX]
  276. push dword Socket_InterruptHandler
  277. push edx
  278. call _Install_Int
  279. add esp, byte _Install_Int_arglen
  280. test eax, eax
  281. jnz .error
  282. movzx edx, byte [Callback_Int]
  283. clc
  284. movzx eax, word [VDD_Handle]
  285. or eax, SOCKET_INSTALLCALLBACK<<16
  286. db 0C4h, 0C4h, 058h, 002h
  287. test eax, eax
  288. jz .end
  289. mov dword [Callback_Address], 0
  290. push edx
  291. call _Remove_Int
  292. add esp, byte _Remove_Int_arglen
  293. jmp short .error
  294. .deinstall:
  295. clc
  296. movzx eax, word [VDD_Handle]
  297. or eax, SOCKET_REMOVECALLBACK<<16
  298. db 0C4h, 0C4h, 058h, 002h
  299. push edx
  300. call _Remove_Int
  301. add esp, byte _Remove_Int_arglen
  302. xor eax, eax
  303. jmp short .end
  304. .ok:
  305. xor eax, eax
  306. mov esp, ebp
  307. pop ebp
  308. ret
  309. .error:
  310. xor eax, eax
  311. inc eax
  312. .end:
  313. mov esp, ebp
  314. pop ebp
  315. ret
  316. [global _Socket_AddCallback]
  317. _Socket_AddCallback: push ebp
  318. mov ebp, esp
  319. .Socket equ 8
  320. .EventMask equ 12
  321. clc
  322. movzx eax, word [VDD_Handle]
  323. or eax, SOCKET_ADDCALLBACK<<16
  324. db 0C4h, 0C4h, 058h, 002h
  325. mov esp, ebp
  326. pop ebp
  327. ret
  328. Socket_InterruptHandler
  329. mov eax, [Callback_Address]
  330. test eax, eax
  331. jz .chain
  332. clc
  333. movzx eax, word [VDD_Handle]
  334. or eax, SOCKET_GETCALLBACKINFO<<16
  335. db 0C4h, 0C4h, 058h, 002h
  336. test eax, eax
  337. jnz .chain
  338. push edx
  339. push ecx
  340. call dword [Callback_Address]
  341. add esp, byte 8
  342. .done:
  343. mov al, 20h
  344. cmp byte [Callback_Int], 50h
  345. jb .lowirq
  346. out 0A0h, al
  347. .lowirq:
  348. out 20h, al
  349. xor eax, eax
  350. ret
  351. .chain:
  352. mov eax, 1
  353. ret
  354. Socket_InterruptHandler_end
  355. [global _Socket_accept]
  356. _Socket_accept: push ebp
  357. mov ebp, esp
  358. .Socket equ 8
  359. .Name equ 12
  360. clc
  361. movzx eax, word [VDD_Handle]
  362. or eax, SOCKET_ACCEPT<<16
  363. db 0C4h, 0C4h, 058h, 002h
  364. mov esp, ebp
  365. pop ebp
  366. ret
  367. [global _Socket_bind]
  368. _Socket_bind: push ebp
  369. mov ebp, esp
  370. .Socket equ 8
  371. .Name equ 12
  372. clc
  373. movzx eax, word [VDD_Handle]
  374. or eax, SOCKET_BIND<<16
  375. db 0C4h, 0C4h, 058h, 002h
  376. mov esp, ebp
  377. pop ebp
  378. ret
  379. [global _Socket_close]
  380. _Socket_close: push ebp
  381. mov ebp, esp
  382. .Socket equ 8
  383. clc
  384. movzx eax, word [VDD_Handle]
  385. or eax, SOCKET_CLOSE<<16
  386. db 0C4h, 0C4h, 058h, 002h
  387. mov esp, ebp
  388. pop ebp
  389. ret
  390. [global _Socket_connect]
  391. _Socket_connect: push ebp
  392. mov ebp, esp
  393. .Socket equ 8
  394. .Name equ 12
  395. clc
  396. movzx eax, word [VDD_Handle]
  397. or eax, SOCKET_CONNECT<<16
  398. db 0C4h, 0C4h, 058h, 002h
  399. mov esp, ebp
  400. pop ebp
  401. ret
  402. [global _Socket_getpeername]
  403. _Socket_getpeername: push ebp
  404. mov ebp, esp
  405. .Socket equ 8
  406. .Name equ 12
  407. clc
  408. movzx eax, word [VDD_Handle]
  409. or eax, SOCKET_GETPEERNAME<<16
  410. db 0C4h, 0C4h, 058h, 002h
  411. mov esp, ebp
  412. pop ebp
  413. ret
  414. [global _Socket_getsockname]
  415. _Socket_getsockname: push ebp
  416. mov ebp, esp
  417. .Socket equ 8
  418. .Name equ 12
  419. clc
  420. movzx eax, word [VDD_Handle]
  421. or eax, SOCKET_GETSOCKNAME<<16
  422. db 0C4h, 0C4h, 058h, 002h
  423. mov esp, ebp
  424. pop ebp
  425. ret
  426. [global _Socket_ntohl]
  427. _Socket_ntohl
  428. [global _Socket_htonl]
  429. _Socket_htonl: push ebp
  430. mov ebp, esp
  431. .HostVal equ 8
  432. mov eax, [ebp+.HostVal]
  433. bswap eax
  434. mov esp, ebp
  435. pop ebp
  436. ret
  437. [global _Socket_ntohs]
  438. _Socket_ntohs
  439. [global _Socket_htons]
  440. _Socket_htons: push ebp
  441. mov ebp, esp
  442. .HostVal equ 8
  443. mov ax, [ebp+.HostVal]
  444. xchg al, ah
  445. mov esp, ebp
  446. pop ebp
  447. ret
  448. [global _Socket_inet_addr]
  449. _Socket_inet_addr: push ebp
  450. mov ebp, esp
  451. .DottedAddress equ 8
  452. clc
  453. movzx eax, word [VDD_Handle]
  454. or eax, SOCKET_INETADDR<<16
  455. db 0C4h, 0C4h, 058h, 002h
  456. mov esp, ebp
  457. pop ebp
  458. ret
  459. [global _Socket_inet_ntoa]
  460. _Socket_inet_ntoa: push ebp
  461. mov ebp, esp
  462. .Address equ 8
  463. clc
  464. movzx eax, word [VDD_Handle]
  465. or eax, SOCKET_INETNTOA<<16
  466. db 0C4h, 0C4h, 058h, 002h
  467. mov eax, NetAddr_static
  468. mov esp, ebp
  469. pop ebp
  470. ret
  471. [global _Socket_listen]
  472. _Socket_listen: push ebp
  473. mov ebp, esp
  474. .Socket equ 8
  475. .BackLog equ 12
  476. clc
  477. movzx eax, word [VDD_Handle]
  478. or eax, SOCKET_LISTEN<<16
  479. db 0C4h, 0C4h, 058h, 002h
  480. mov esp, ebp
  481. pop ebp
  482. ret
  483. [global _Socket_recv]
  484. _Socket_recv: push ebp
  485. mov ebp, esp
  486. .Socket equ 8
  487. .Buf equ 12
  488. .MaxLen equ 16
  489. .Flags equ 20
  490. clc
  491. movzx eax, word [VDD_Handle]
  492. or eax, SOCKET_RECV<<16
  493. db 0C4h, 0C4h, 058h, 002h
  494. mov esp, ebp
  495. pop ebp
  496. ret
  497. [global _Socket_recvfrom]
  498. _Socket_recvfrom: push ebp
  499. mov ebp, esp
  500. .Socket equ 8
  501. .Buf equ 12
  502. .MaxLen equ 16
  503. .Flags equ 20
  504. .From equ 24
  505. clc
  506. movzx eax, word [VDD_Handle]
  507. or eax, SOCKET_RECVFROM<<16
  508. db 0C4h, 0C4h, 058h, 002h
  509. mov esp, ebp
  510. pop ebp
  511. ret
  512. [global _Socket_send]
  513. _Socket_send: push ebp
  514. mov ebp, esp
  515. .Socket equ 8
  516. .Buf equ 12
  517. .Len equ 16
  518. .Flags equ 20
  519. clc
  520. movzx eax, word [VDD_Handle]
  521. or eax, SOCKET_SEND<<16
  522. db 0C4h, 0C4h, 058h, 002h
  523. mov esp, ebp
  524. pop ebp
  525. ret
  526. [global _Socket_sendto]
  527. _Socket_sendto: push ebp
  528. mov ebp, esp
  529. .Socket equ 8
  530. .Buf equ 12
  531. .Len equ 16
  532. .Flags equ 20
  533. .To equ 24
  534. clc
  535. movzx eax, word [VDD_Handle]
  536. or eax, SOCKET_SENDTO<<16
  537. db 0C4h, 0C4h, 058h, 002h
  538. mov esp, ebp
  539. pop ebp
  540. ret
  541. [global _Socket_shutdown]
  542. _Socket_shutdown: push ebp
  543. mov ebp, esp
  544. .Socket equ 8
  545. .Flags equ 12
  546. mov eax, [ebp+.Flags]
  547. test eax, eax
  548. jz .done
  549. clc
  550. movzx eax, word [VDD_Handle]
  551. or eax, SOCKET_SHUTDOWN<<16
  552. db 0C4h, 0C4h, 058h, 002h
  553. .done:
  554. mov esp, ebp
  555. pop ebp
  556. ret
  557. [global _Socket_create]
  558. _Socket_create: push ebp
  559. mov ebp, esp
  560. .Type equ 8
  561. clc
  562. movzx eax, word [VDD_Handle]
  563. or eax, SOCKET_CREATE<<16
  564. db 0C4h, 0C4h, 058h, 002h
  565. mov esp, ebp
  566. pop ebp
  567. ret
  568. [global _Socket_gethostbyaddr]
  569. _Socket_gethostbyaddr: push ebp
  570. mov ebp, esp
  571. .Address equ 8
  572. clc
  573. movzx eax, word [VDD_Handle]
  574. or eax, SOCKET_GETHOSTBYADDR<<16
  575. db 0C4h, 0C4h, 058h, 002h
  576. test eax, eax
  577. jz .done
  578. mov eax, HostEnt_static
  579. .done:
  580. mov esp, ebp
  581. pop ebp
  582. ret
  583. [global _Socket_gethostbyname]
  584. _Socket_gethostbyname: push ebp
  585. mov ebp, esp
  586. .Name equ 8
  587. clc
  588. movzx eax, word [VDD_Handle]
  589. or eax, SOCKET_GETHOSTBYNAME<<16
  590. db 0C4h, 0C4h, 058h, 002h
  591. test eax, eax
  592. jz .done
  593. mov eax, HostEnt_static
  594. .done:
  595. mov esp, ebp
  596. pop ebp
  597. ret
  598. [global _Socket_gethostname]
  599. _Socket_gethostname: push ebp
  600. mov ebp, esp
  601. .Name equ 8
  602. .NameLen equ 12
  603. clc
  604. movzx eax, word [VDD_Handle]
  605. or eax, SOCKET_GETHOSTNAME<<16
  606. db 0C4h, 0C4h, 058h, 002h
  607. mov esp, ebp
  608. pop ebp
  609. ret
  610. [global _Socket_GetLastError]
  611. _Socket_GetLastError
  612. mov eax, [LastError]
  613. ret