Created
December 6, 2022 20:30
-
-
Save Zelakolase/f69562bc020ce9b922ffc366be0bb371 to your computer and use it in GitHub Desktop.
static web server in assembly by ChatGPT
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
his program is a simple web server written in assembly language. | |
; It will listen for incoming connections on port 80 and serve | |
; static web pages. | |
section .text | |
global _start | |
_start: | |
; Set up the socket | |
mov eax, 0x66 ; socketcall | |
mov ebx, 0x1 ; socket | |
mov ecx, 0x2 ; AF_INET | |
mov edx, 0x1 ; SOCK_STREAM | |
int 0x80 ; call socket | |
; Save the socket descriptor | |
mov esi, eax ; save socket descriptor | |
; Set up the address | |
mov eax, 0x66 ; socketcall | |
mov ebx, 0x4 ; bind | |
mov ecx, esi ; socket descriptor | |
push word 0x5c11 ; port 80 | |
push word 0x2 ; AF_INET | |
mov edx, esp ; address | |
int 0x80 ; call bind | |
; Listen for connections | |
mov eax, 0x66 ; socketcall | |
mov ebx, 0x5 ; listen | |
mov ecx, esi ; socket descriptor | |
mov edx, 0x10 ; backlog | |
int 0x80 ; call listen | |
; Accept connections | |
mov eax, 0x66 ; socketcall | |
mov ebx, 0x3 ; accept | |
mov ecx, esi ; socket descriptor | |
mov edx, 0x0 ; address | |
int 0x80 ; call accept | |
; Save the connection descriptor | |
mov edi, eax ; save connection descriptor | |
; Read the request | |
mov eax, 0x3 ; read | |
mov ebx, edi ; connection descriptor | |
mov ecx, esp ; buffer | |
mov edx, 0x100 ; length | |
int 0x80 ; call read | |
; Parse the request | |
; ... | |
; Open the file | |
mov eax, 0x5 ; open | |
mov ebx, esp ; filename | |
mov ecx, 0x0 ; flags | |
mov edx, 0x0 ; mode | |
int 0x80 ; call open | |
; Read the file | |
mov eax, 0x3 ; read | |
mov ebx, eax ; file descriptor | |
mov ecx, esp ; buffer | |
mov edx, 0x100 ; length | |
int 0x80 ; call read | |
; Send the response | |
mov eax, 0x4 ; write | |
mov ebx, edi ; connection descriptor | |
mov ecx, esp ; buffer | |
mov edx, 0x100 ; length | |
int 0x80 ; call write | |
; Close the connection | |
mov eax, 0x6 ; close | |
mov ebx, edi ; connection descriptor | |
int 0x80 ; call close | |
; Exit | |
mov eax, 0x1 ; exit | |
mov ebx, 0x0 ; return code | |
int 0x80 ; call exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment