From a7253bacdedf2fb1b72d8f760e1f469e6c0b96bf Mon Sep 17 00:00:00 2001 From: Tijmen van Nesselrooij Date: Tue, 26 Mar 2019 19:52:31 +0100 Subject: [PATCH] rot13 assignment added --- ...{3_homework.asm => 3_homework_highest.asm} | 0 x86_64/3_homework_rot13.asm | 79 +++++++++++++++++++ 2 files changed, 79 insertions(+) rename x86_64/{3_homework.asm => 3_homework_highest.asm} (100%) create mode 100644 x86_64/3_homework_rot13.asm diff --git a/x86_64/3_homework.asm b/x86_64/3_homework_highest.asm similarity index 100% rename from x86_64/3_homework.asm rename to x86_64/3_homework_highest.asm diff --git a/x86_64/3_homework_rot13.asm b/x86_64/3_homework_rot13.asm new file mode 100644 index 0000000..cd83be7 --- /dev/null +++ b/x86_64/3_homework_rot13.asm @@ -0,0 +1,79 @@ +global _start + +section .data + str: db "Please give me a string: " ; \n = 0xA + strSize: equ $ - str + + stdin: equ 0 ; POSIX + stdout: equ 1 ; POSIX + +section .bss + strBuffer: resb 64 ; reserve 64 bytes + strBufferSize: equ 64 ; same as above + +section .text +_start: + mov rax, 1 ; Print str to screen + mov rdi, stdout + mov rsi, str + mov rdx, strSize + syscall + + xor rax, rax ; Read user input + mov rdi, stdin + mov rsi, strBuffer + mov rdx, strBufferSize + syscall + + mov rbx, rax ; store bytes read + dec rbx ; subtract the newline + xor rcx, rcx ; make register C our loop var +start_loop: + cmp rcx, rbx + jge print + + ; Calculate the position + mov rdx, strBuffer + add rdx, rcx + ; Load the char at the position + xor rax, rax + mov al, byte [rdx] + mov r8, rdx + ; Check if we're upper or lower case + mov rdx, 95 + cmp rax, rdx + jge lower_case +upper_case: + mov r9, 91 ; 90 is the highest valid upper case value + jmp algorithm +lower_case: + mov r9, 123 ; 122 is the highest lower case value +algorithm: + ; Shift it 13 positions + mov rdx, 13 + add rax, rdx + ; Check if we should wrap around + cmp rax, r9 + jl loop_end + jmp ascii_wrap +ascii_wrap: + ; Wrap around + mov rdx, 26 + sub rax, rdx +loop_end: + ; Store the result + mov byte [r8], al + inc rcx + jmp start_loop + +print: + inc rbx ; Add the newline back to it + mov rax, 1 ; Print str to screen + mov rdi, stdout + mov rsi, strBuffer + mov rdx, rbx + syscall + + mov rax, 60 + xor rdi, rdi + syscall