Skip to content

Instantly share code, notes, and snippets.

@dnmfarrell
Last active November 22, 2022 16:13
Show Gist options
  • Save dnmfarrell/55b311495f602161035e14aa2128f773 to your computer and use it in GitHub Desktop.
Save dnmfarrell/55b311495f602161035e14aa2128f773 to your computer and use it in GitHub Desktop.
URI encoder of non-reserved chars written in x86 NASM
; To compile:
; nasm -f elf32 -o uri-enc.o uri-enc.asm
; ld -m elf_i386 -o uri-enc uri-enc.o
section .bss
LenInp equ 1024
BufInp: resb LenInp
LenOut equ 4096
BufOut: resb LenOut
section .data
%define NULL 0x00,0x00
lookup: db "00","01","02","03","04","05","06","07","08","09",NULL,"0B","0C","0D","0E","0F"
db "10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F"
db "20","21","22","23","24","25","26","27","28","29","2A","2B","2C",NULL,NULL,"2F"
db NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,"3A","3B","3C","3D","3E","3F"
db "40",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
db NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,"5B","5C","5D","5E",NULL
db "60",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
db NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,"7B","7C","7D",NULL,"7F"
db "80","81","82","83","84","85","86","87","88","89","8A","8B","8C","8D","8E","8F"
db "90","91","92","93","94","95","96","97","98","99","9A","9B","9C","9D","9E","9F"
db "A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","AA","AB","AC","AD","AE","AF"
db "B0","B1","B2","B3","B4","B5","B6","B7","B8","B9","BA","BB","BC","BD","BE","BF"
db "C0","C1","C2","C3","C4","C5","C6","C7","C8","C9","CA","CB","CC","CD","CE","CF"
db "D0","D1","D2","D3","D4","D5","D6","D7","D8","D9","DA","DB","DC","DD","DE","DF"
db "E0","E1","E2","E3","E4","E5","E6","E7","E8","E9","EA","EB","EC","ED","EE","EF"
db "F0","F1","F2","F3","F4","F5","F6","F7","F8","F9","FA","FB","FC","FD","FE","FF"
section .text
global _start
_start:
Read:
mov eax,3 ; sys_read
mov ebx,0 ; fd 0
mov ecx,BufInp ; read into buffer
mov edx,LenInp ; read LenInp chars
int 0x80 ; syscall
cmp eax,0 ; compare sys_read rv
je Exit ; Exit if EOF
mov esi,eax ; store return value length
mov eax,0 ; reset to idx counter
mov edi,0 ; reset output idx
Scan:
xor ebx,ebx ; zero ebx
mov bl,[BufInp+eax] ; copy input byte
inc eax ; inc input index
xor ecx,ecx
xor edx,edx
mov cl,bl ; dup input byte
shl ecx,1 ; double it
mov dx,[lookup+ecx] ; lookup byte
cmp dl,0x00 ; if null ...
je Copy
mov byte [BufOut+edi],0x25 ; else copy %
inc edi
mov word [BufOut+edi],dx ; copy hex
add edi,2
jmp Next
Copy: ; copy byte to output
mov byte [BufOut+edi],bl
inc edi
Next: ; loop condition
cmp eax,esi
jne Scan
Write:
mov eax,4 ; sys_write
mov ebx,1 ; fd 1
mov ecx,BufOut ; addr of string pointer to write
mov edx,edi ; length to write
int 0x80 ; syscall
jmp Read
Exit:
mov eax,1 ; exit syscall
mov ebx,0 ; exit return code
int 0x80 ; interrupt syscall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment