Skip to content

Instantly share code, notes, and snippets.

@sum-catnip
Created August 12, 2020 10:07
Show Gist options
  • Save sum-catnip/f6d5ff19eb91793e9761a9833b33e036 to your computer and use it in GitHub Desktop.
Save sum-catnip/f6d5ff19eb91793e9761a9833b33e036 to your computer and use it in GitHub Desktop.
x86-64asm - linux Caesar Cipher in 93bytes
; challange:
; --------------------
; Challenge #3
; --------------------
; Caesar Cipher
;
; You may know also this as ROT N.
; Your solution should input an arbitrary ROT cipher encoded string and output all shifts.
; The allowed alphabet is [a-zA-Z] any other characters are not shifted and output as is
;
; Optional bonus challenge for bowling:
; Add some frequency analysis to find the correct shift and output only that.
; https://www.101computing.net/frequency-analysis/
;
; Notes:
; As with last week, the string wont be included in your code char length.
; You cannot just print the solution. Your code must solve it.
; Some examples of expected input/output https://hasteb.in/ipagohoz.yaml
; The output should either contain 25 shifts or 25 shifts + the original, both are valid
; The outputs should each be on a new line
; \x41\xb5\x19\x41\xb4\x1a\x4c\x8b\x44\x24\x10\x45\x8a\x08\x45\x20\xc9\x75\x0d\x49\xff\xcd\xc6\x04\x24\x0a\x0f\x05\x74\x3c\xeb\xe6\x41\x8a\x00\x41\x80\xc9\x20\x4d\x8d\x51\x9f\x41\x80\xfa\x1a\x73\x15\x4d\x01\xea\x4c\x89\xd0\x41\xf6\xfc\x48\xc1\xe8\x08\x04\x61\x45\x2a\x08\x4c\x29\xc8\x88\x04\x24\x48\x89\xe6\xb0\x01\x40\xb7\x01\xb2\x01\x0f\x05\x49\xff\xc0\xeb\xb1\xb0\x3c\x0f\x05
global _start
_start: mov r13b, 25
mov r12b, 'Z' - 'A' +1
loop1: mov r8, [rsp + 8 * 2]
loop: mov r9b, [r8]
; null check
and r9b, r9b
jnz continue
dec r13
mov byte [rsp], `\n`
syscall
jz bye
jmp loop1
continue: mov al, byte [r8]
; tolower
or r9b, 0x20
; normalize (a = 0)
lea r10, [r9 - 'a']
; greater than normalized 'z' (unsigned !important)?
cmp r10b, 26
jae skip
stuff: add r10, r13
mov rax, r10
idiv r12b
shr rax, 8
; restore case
add al, 'a'
sub r9b, byte [r8]
sub rax, r9
skip: mov [rsp], al
mov rsi, rsp
mov al, 1
mov dil, 1
mov dl, 1
syscall
inc r8
jmp loop
bye: mov al, 60
syscall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment