; ROT-13 encoder/decoder for Linux/IA-32 ; ; (C) 2007 Gergo ERDI ; http://cactus.rulez.org/ ; ; Compiling: ; ; nasm -felf -o rot13.o rot13.asm ; ld -o rot13 rot13.o ; ; The resulting executable 'rot13' is a filter from stdin to stdout. section .text global _start _start: mov eax, 3 mov ebx, STDIN mov ecx, BUF mov edx, BUFLEN int 0x80 ; read (stdin, BUF, BUFLEN) cmp eax, 0 ; 0 bytes read marks the end of the input jz end mov ebx, eax ; ebx is the loop variable, ecx is the input pointer processloop: cmp ebx, 0 jz print lowercase: cmp byte [ecx], 'a' ; 'a' <= [ecx] <= 'z' jb uppercase cmp byte [ecx], 'z' ja uppercase sub byte [ecx], 13 cmp byte [ecx], 'a' ; When underflow => fix by rotating upwards 13 (to restore) and 13 again (to rotate) jnb processnext add byte [ecx], 26 jmp processnext uppercase: cmp byte [ecx], 'A' ; see lowercase branch jb processnext cmp byte [ecx], 'Z' ja processnext sub byte [ecx], 13 cmp byte [ecx], 'A' jnb processnext add byte [ecx], 26 processnext: inc ecx dec ebx jmp processloop print: mov edx, eax ; We've been smart enough to save read()'s return value in eax mov ecx, BUF mov ebx, STDOUT mov eax, 4 int 0x80 ; write (stdout, BUF, len) jmp _start end: mov eax, 1 mov ebx, 0 int 0x80 ; exit (0) section .bss BUFLEN equ 1024 BUF resb BUFLEN STDIN equ 0 STDOUT equ 1