rot13 assignment added
This commit is contained in:
79
x86_64/3_homework_rot13.asm
Normal file
79
x86_64/3_homework_rot13.asm
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user