Answer To: Title : Project 3 Point Value: 100 Deadline : 4/18 at 11.59pm Topics Covered: Introduction to NASM,...
Gaurav answered on May 04 2021
Encryption.asm
global main
extern displayMessage
extern readMessage
extern shiftEncryption
extern jumpEncryption
extern square_root
extern clearMemory
%define __STD_IN_ 0
%define __STD_OUT_ 1
%define __NR_read 0
%define __NR_write 1
%define __NR_open 2
%define __NR_close 3
%define __NR_exit 60
%macro read_input 2 ; macro to read user input
mov rbx, 0 ; number character read
%%getLine:
mov rax, __NR_read ; syscall number
mov rdi, __STD_IN_
lea rsi, [%1 + rbx] ; buffer address + offset
mov rdx, 1 ; read only one character
syscall
cmp rbx, %2+1 ; check if number of character read is greater than buffer size - 1
ja %%invalid_option ; if so stop incrementing the offset
add rbx, rax
%%invalid_option:
mov al, BYTE [rsi] ; check the byte for newline character
cmp al, 10
jne %%getLine ; exit the loop if newline character is received
mov rax, rbx ; total number character read
%endmacro
section .data
initial_string db "This is the original message.", 0
initial_string_end:
menu db 10, "Encryption menu options:", 10
db "d - display current messages", 10
db "r - read new message", 10
db "s - shift encrypt", 10
db "j - jump encrypt", 10
db "q - quit program", 10
db "enter option letter -> ", 0
menu_end:
error db "Invalid Option, Try again", 10, 0
error_end:
invalidMsgIdx db "Invalid Message Index [0 - 9]", 10, 0
invalidMsgIdx_end:
easterEggFound db "EASTER EGG FOUND", 10, 0
easterEggFound_end:
getLocStr db "Enter string location : ", 0
getLocStr_end:
goodbye db "Goodbye!", 10, 0
goodbye_end:
c_pressed dq 0
index dq 0
message times 10 dq 0
section .bss
buffer resb 4
section .text
main:
mov rdi, message
mov rax, initial_string
mov rcx, 10
fill_initial_string_address:
mov [rdi + rcx * 8 - 8], rax
loop fill_initial_string_address ; fill the message array with pointer of default string
main_loop:
mov rax, __NR_write
mov rdi, __STD_OUT_
mov rsi, menu
mov rdx, menu_end - menu
syscall ; display menu
read_input buffer, 1 ; read only one character, buffer size should be number of desired character + 1
cmp rax, 2 ; check if only desired character received, 1 + 1(newline character)
je valid_option
mov rax, __NR_write ; else print error
mov rdi, __STD_OUT_
mov rsi, error
mov rdx, error_end - error
syscall
jmp main_loop
valid_option:
mov al, BYTE [buffer] ; load the character
cmp al, 'c'
je extra_credit ; extra credit part
mov QWORD[c_pressed], 0 ; reset the counter if anything less pressed
cmp al, 'd' ; execute menu
je display_message
cmp al, 'r'
je read_message
cmp al, 's'
je shift_message
cmp al, 'j'
je jump_message
cmp al, 'q'
je terminate
invalid_option: ; print error for other keys
mov rax, __NR_write
mov rdi, __STD_OUT_
mov rsi, error
mov rdx, error_end - error
syscall
jmp main_loop
display_message:
mov rdi, message ; print all the string in message array
call displayMessage
jmp main_loop
read_message:
call readMessage ; read new message
test rax, rax ; if new message is valid
je main_loop
mov rdi, message
mov rcx, [index]
mov [rdi + rcx * 8], rax ; save th pointer in message array
inc rcx ; increment index
mov [index], rcx
cmp rcx, 10 ; if index > 10 then index = 0
jl main_loop
mov QWORD[index], 0
jmp main_loop
shift_message:
mov rax, __NR_write
mov rdi, __STD_OUT_
mov rsi, getLocStr
mov rdx, getLocStr_end - getLocStr
syscall
read_input buffer, 1 ; get the string location
cmp rax, 2
je valid_shift_msg_idx
invalid_shift_msg_idx: ; print error if invalid character received
mov rax, __NR_write
mov rdi, __STD_OUT_
mov rsi, invalidMsgIdx
mov rdx, invalidMsgIdx_end - invalidMsgIdx
syscall
jmp main_loop
valid_shift_msg_idx:
movzx rax, BYTE[buffer] ; load the byte
sub rax, 0x30 ; check if its digit
cmp rax, 9
ja invalid_shift_msg_idx
mov rdi, [message + rax * 8] ; load the address of string of given index
call shiftEncryption
jmp main_loop
jump_message:
mov rax, __NR_write
mov rdi, __STD_OUT_
mov rsi, getLocStr
mov rdx, getLocStr_end - getLocStr
syscall
read_input buffer, 1 ; get the string location
cmp rax, 2
je valid_jump_msg_idx
invalid_jump_msg_idx:
mov rax, __NR_write ; print error if invalid character received
mov rdi, __STD_OUT_
mov rsi, invalidMsgIdx
mov rdx, invalidMsgIdx_end - invalidMsgIdx
syscall
valid_jump_msg_idx:
movzx rax, BYTE[buffer] ; load the byte
sub rax, 0x30
cmp rax, 9
ja invalid_jump_msg_idx ; check if its digit
mov rdi, [message + rax * 8] ; load the address of string of given index
call...