Last active
September 8, 2025 19:15
-
-
Save timosarkar/5ae0bbbf4a3313aacc251b6189cf8f74 to your computer and use it in GitHub Desktop.
x86_64 NASM HTTP1.1 client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; Fast HTTP client with minimal but essential error checking | |
| ; Target: Maintain 1.3ms speed while ensuring clean exit | |
| section .data | |
| align 64 | |
| ; Optimized minimal HTTP request | |
| http_req db "GET / HTTP/1.1",13,10,"Host: localhost",13,10,13,10 | |
| req_len equ $ - http_req | |
| ; Pre-calculated sockaddr_in as 32-bit values | |
| align 16 | |
| sockaddr dd 0x0002005C, 0x0100007F, 0, 0 ; family=2, port=80, IP=127.0.0.1 | |
| section .bss | |
| align 4096 | |
| buf resb 4096 | |
| section .text | |
| global _start | |
| _start: | |
| ; Create socket (32-bit registers for speed) | |
| mov eax, 41 | |
| mov edi, 2 | |
| mov esi, 1 | |
| xor edx, edx | |
| syscall | |
| ; Quick error check (just sign bit) | |
| test eax, eax | |
| js .clean_exit ; Jump to clean exit on error | |
| mov r12d, eax | |
| ; Connect | |
| mov eax, 42 | |
| mov edi, r12d | |
| mov rsi, sockaddr | |
| mov edx, 16 | |
| syscall | |
| ; Quick error check | |
| test eax, eax | |
| js .close_and_exit | |
| ; Send request | |
| mov eax, 44 | |
| mov edi, r12d | |
| mov rsi, http_req | |
| mov edx, req_len | |
| xor r10d, r10d | |
| syscall | |
| ; Continue even if send partially fails (for speed) | |
| ; Receive response | |
| mov eax, 45 | |
| mov edi, r12d | |
| mov rsi, buf | |
| mov edx, 4096 | |
| xor r10d, r10d | |
| syscall | |
| ; Optional: Quick success indicator | |
| ; test eax, eax | |
| ; jle .close_and_exit | |
| .close_and_exit: | |
| ; Close socket | |
| mov eax, 3 | |
| mov edi, r12d | |
| syscall | |
| .clean_exit: | |
| ; Always exit successfully (fixes the warning) | |
| mov eax, 60 | |
| xor edi, edi ; Exit code 0 | |
| syscall |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| python3 -m http.server 80 & | |
| nasm -f elf64 c.asm | |
| ld c.o | |
| ./c.out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment