Last active
December 26, 2017 23:28
-
-
Save zafercavdar/68bcdf1b1d7a7ed0e5329490bf8bb6c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##################################################################### | |
# # | |
# Names: Zafer Çavdar # | |
# KUSIS IDs: 0049995 # | |
##################################################################### | |
# Use this template and develop your program for Assignment 2 | |
# MIPS assembly code for Affine cipher | |
# Developed by Najeeb Ahmad and Pirah Noor Smooro | |
#==================================================================== | |
# Variable definitions | |
# You may define new variables in this section | |
#==================================================================== | |
.eqv EMUL 5 | |
.eqv EADD 8 | |
.eqv DMUL 21 | |
.eqv DSUB 8 | |
.eqv MOD 26 | |
.eqv ASCII_A 97 | |
.eqv ASCII_E 101 | |
.eqv ASCII_D 100 | |
.eqv ASCII_Z 122 | |
.data | |
err_msg: .asciiz "Error message" | |
err_argument: .asciiz "Error: Invalid number of arguments." | |
err_flag: .asciiz "Error: Invalid flags. Use -e or -d" | |
err_char: .asciiz "Error: Invalid character encountered, string should contain lower letters only." | |
encrypted_data: .space 100 | |
decrypted_data: .space 100 | |
#==================================================================== | |
# Program start | |
#==================================================================== | |
.text | |
.globl main | |
main: | |
# Main program entry | |
# Main body | |
# Check for command line arguments count and validity | |
bne $a0, 2, Error_Argument # if arg count != 2, then go to error label | |
checkFlag: | |
lw $t0, 0($a1) # t0 = -e or -d (expected) | |
lb $t2, 0($t0) # t2 = - (expected) | |
lb $t3, 1($t0) # t3 = e or d (expected) | |
bne $t2, 45, Error_Flag # ASCII of - = 45 | |
# If -e flag is found, execute Encrypt_Cmd | |
CheckE: | |
bne $t3, ASCII_E, CheckD # if t3 is not equal to e, then check if it's d or not | |
jal Encrypt_Cmd | |
j Exit | |
# If -d flag is found, execute Decrypt_Cmd | |
CheckD: | |
bne $t3, ASCII_D, Error_Flag # if t3 is not equal to d, then flag is not valid, go to error label | |
jal Decrypt_Cmd | |
j Exit | |
Encrypt_Cmd: | |
# Setup argument registers and call Encrypt_Data function | |
lw $t6, 4($a1) # t6 = input string | |
la $s0, ($t6) # $s0 will store address of t6 (copy of input string) | |
la $t1, encrypted_data # t1 stores address of encrypted_data variable | |
# Call encryption function here | |
j Encrypt_Data | |
j Exit | |
Decrypt_Cmd: | |
# Setup argument registers and call Decrypt_Data function | |
lw $t6, 4($a1) # t6 = input string | |
la $s0, ($t6) # $s0 will store address of t6 (copy of input string) | |
la $t1, decrypted_data # t1 stores address of decrypted_data variable | |
# Call decryption function here | |
j Decrypt_Data | |
j Exit | |
# Function to encrypt data | |
Encrypt_Data: | |
# Add your encryption logic here | |
li $t0, 0 # t0 = 0, iteration variable | |
loopE: | |
add $s1, $s0, $t0 # $s1 stores the address of current character | |
lb $s2, 0($s1) # s2 stores current character | |
beqz $s2, PrintE # if s2 is $zero, then break the loop, go to PrintE tag | |
slti $t4, $s2, ASCII_A # set $t4 to 1 if ascii of char is less than 97 | |
beq $t4, 1, Error_Char # if t4== 1, go to error | |
sgt $t4, $s2, ASCII_Z # set $t4 to 1 if ascii of char is greater than 122 | |
beq $t4, 1, Error_Char # if t4== 1, go to error | |
# E(x) = (5x +8) mod 26 | |
addi $t2, $zero, EMUL # $t2 = 5 | |
addi $t3, $zero, MOD # $t3 = 26 | |
subi $s2, $s2, ASCII_A # substract 97 from $s2 so that s2 becomes between 0-25 | |
mul $s2, $s2, $t2 # multiply $s2 with 5 | |
addi $s2, $s2, EADD # add 8 | |
rem $s2, $s2, $t3 # take modulo 26 | |
addi $s2, $s2, ASCII_A # add 97 to generate a lower case char between 97-122 | |
sb $s2, ($t1) # store result in $t1. (updates encrypted_data) | |
addi $t0, $t0, 1 # increment loop variable by 1 | |
addi $t1, $t1, 1 # increment t1 by 1 - update address | |
j loopE # return to loop | |
# Print the encrypted string | |
PrintE: | |
li $v0, 4 # set v0 to 4 -> print string | |
la $a0, encrypted_data # a0 stores address of starting address of encrypted_data | |
syscall # print a0 | |
# Return from function | |
j Exit | |
# Function to decrypt data | |
Decrypt_Data: | |
# Add your decryption logic here | |
li $t0, 0 # t0 = 0, iteration variable | |
loopD: | |
add $s1, $s0, $t0 # $s1 stores the address of current character | |
lb $s2, 0($s1) # s2 stores current character | |
beqz $s2, PrintD # if s2 is $zero, then break the loop, go to PrintE tag | |
slti $t4, $s2, ASCII_A # set $t4 to 1 if ascii of char is less than 97 | |
beq $t4, 1, Error_Char # if t4== 1, go to error | |
sgt $t4, $s2, ASCII_Z # set $t4 to 1 if ascii of char is greater than 122 | |
beq $t4, 1, Error_Char # if t4== 1, go to error | |
# D(x) = 21(y-8) mod 26 | |
addi $t2, $zero, DMUL # $t2 = 21 | |
addi $t3, $zero, MOD # $t3 = 26 | |
subi $s2, $s2, ASCII_A # substract 97 from $s2 so that value is between 0-26 | |
subi $s2, $s2, DSUB # substract 8 | |
addi $s2, $s2, MOD # add 26 so that s2 will never be negative before calculating remainder | |
mul $s2, $s2, $t2 # multiply with 21 | |
rem $s2, $s2, $t3 # take modulo 26 | |
addi $s2, $s2, ASCII_A # add 97 to generate a lower case char between 97-122 | |
sb $s2, ($t1) # store result in $t1. (updates decrypted_data ) | |
addi $t0, $t0, 1 # increment loop variable by 1 | |
addi $t1, $t1, 1 # increment t1 by 1 - update address | |
j loopD # return to loop | |
# Print the decrypted string | |
PrintD: | |
li $v0, 4 # set v0 to 4 -> print string | |
la $a0, decrypted_data # a0 stores address of starting address of decrypted_data | |
syscall # print a0 | |
# Return from function | |
j Exit | |
Exit: | |
# Exit program | |
li $v0, 10 | |
syscall | |
j Finish # jump to finish to escape displaying error message | |
Error: | |
li $v0, 4 | |
la $a0, err_msg | |
syscall | |
j Finish | |
Error_Argument: | |
li $v0, 4 | |
la $a0, err_argument | |
syscall | |
j Finish | |
Error_Flag: | |
li $v0, 4 | |
la $a0, err_flag | |
syscall | |
j Finish | |
Error_Char: | |
li $v0, 4 | |
la $a0, err_char | |
syscall | |
j Finish | |
Finish: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment