#=================================== # initialize setup directives #=================================== #store in data portion of memory .data input_file: .asciiz "Users/User/Documents/ECE 331/Project1/Input.txt" cipher_file: .asciiz "Users/User/Documents/ECE 331/Project1/Ciphertext.txt" decoded_file: .asciiz "Users/User/Documents/ECE 331/Project1/Decoded.txt" password: .byte 's' 'c' 'h' 'a' outputprompt: .asciiz "Output from compare is: " plaintext: .space 200 cyphertext: .space 200 decrypted: .space 200 #store in text portion of memory .text MAIN: jal ra, LOAD mv s0, a0 mv s1, a1 # save copy of plaintext address in s1 mv s2, a2 # save copy of cyphertext address in s2 mv s3, a3 # save copy of decrypted address in s3 mv s4, a4 # number of written items jal ra, ENCRYPT # all arguments are already where they should be mv a0, s0 # load arguments for encrypt mv a1, s2 # here we load the cyphertext as plaintext to decode it mv a2, s3 jal ra, ENCRYPT # takes a0 key, a1 plaintext a2 output location mv a0, s1 mv a1, s3 jal ra, COMPARE # load plaintext and decoded to compare them mv t0, a0 addi a0, x0, 4 # print output of compare la a1, outputprompt ecall mv a1, t0 addi a0, x0, 1 ecall addi a0, x0, 11 li a1, 10 ecall la a1, cipher_file mv a2, s2 jal ra, COMPAREREAD # print output of ciphertext j ENDP LOAD: addi a0, x0, 13 # open file la a1, input_file ecall mv a1, a0 mv t1, a0 addi a0, x0, 14 # read plaintext la a2, plaintext addi a3, x0, 199 ecall mv t2, a0 addi a0, x0, 16 add a1, x0, t1 # close file ecall lw a0, password # set outputs la a1, plaintext la a2, cyphertext la a3, decrypted mv a4, t2 jalr x0, x1, 0 # return to main ENCRYPT: # Arguments: # a0 = password # a1 = &plaintext # a2 = &cyphertext # encrypts a null terminated string # returns nothing, stores encrypted data in *a2 # save ra and saved registers addi sp, sp, -16 sw ra, 12(sp) sw s0, 8(sp) sw s1, 4(sp) sw s2, 0(sp) mv s0, a0 # save arguments mv s1, a1 mv s2, a2 L0: mv a0, s0 # orrigianl passowrd into a0 mv a1, s1 mv a2, s2 lw a1, 0(s1) # load new plaintext block into a1 jal ra, BLOCKSWAP # swaps password bytes jal ra, ALGO # generates encrypted text sw a0, 0(s2) # stores encrypted text in ciphertext addi s1, s1, 4 # itterates to next index addi s2, s2, 4 beq a2, x0, L0 # if a2 flag for null character=0 loop back lw ra, 12(sp) # restores registers and clears stack lw s0, 8(sp) lw s1, 4(sp) lw s2, 0(sp) addi sp, sp, 16 jalr x0, ra, 0 BLOCKSWAP: # Arguments: # a0 = pswd # a1 = char[4] # variables # s0, bit mask for current byte # s1, bytes to shift byte to 0-7 # a2, password byte # a3, char byte # resultants: # a0 = new pswd # a1 = same char # a2 = 1 if end 0 else # swaps the block of pswd based on char block addi sp, sp, -16 sw ra, 8(sp) sw s0, 4(sp) sw s1, 0(sp) #======== sets variables======== add a4, x0, x0 # storage of result add s1, x0, x0 # shift amount addi s0, x0, 0xff # right most byte #======================================================================== L1: #addi s1, s1, 8 and a2, s0, a0 # isolate byte from pswd and a3, s0, a1 # isolate byte from char srl a2, a2, s1 srl a3, a3, s1 # shift byte into right field beq a3, x0, ENDSTR # if null character end jal ra, CHECKSWAP # adds flags if swap bne a5, x0, SKIP # skips swapchar if flagged jal ra, SWAPCHAR # swaps upper 4 bits w/ lower 4 bits SKIP: sll a2, a2, s1 # shifts a2 back to proper field or a4, a4, a2 # stores swapped pswd into a4 slli s0, s0, 8 # gets mask ready for next char addi s1, s1, 8 bne s0, x0, L1 # if on last char dont loop mv a2, x0 # sets flag to 0 jal x0, EXIT ENDSTR: addi a2, x0, 1 # set a2 flag to 1 EXIT: # restores registers and resets stack lw ra, 8(sp) lw s0, 4(sp) lw s1, 0(sp) addi sp, sp, 16 mv a0, a4 # moves results into a0 jalr, x0, ra, 0 CHECKSWAP: # checks to see if last byte of a2 has two even or two odd bits in bits 0,4 # a5 will be 1 if different, 0 if same srli t2, a3, 4 # align bit 4 with bit 0 xor t2, t2, a3 # xor bit 0 and bit 4 of a3 andi t2, t2, 1 # only show bit 1, mv a5, t2 # move result into flag field jalr x0, ra, 0 SWAPCHAR: # Arguments: # a2 = pswd char # # will swap the nibbles in first byte of a2 andi t2, a2, 0xff # isolate byte srli a2, t2, 4 # shift by a nibble slli t2, t2, 4 # lines up lower nibble to where upper nibble would be andi t2, t2, 0xf0 # remove upper nibble or a2, t2, a2 # combine nibbles jalr x0, ra, 0 ALGO: # encrypts one word with password using rules defined # arguments: # a0 = pswrd # a1 = text block # Results: # a0 = encrypted text li t0, 0xeeeeeeee # loads constant and a0, a0, t0 # sets bits 0,4 of every byte to 0 xor a0, a0, a1 # encryptes plaintext jalr x0, ra, 0 COMPARE: # Arguments: # a0, &plaintext # a1, &decrypted # Returns: # a0, 1 if decryption error, 0 if none addi sp, sp, -4 # save return address sw ra, 0(sp) mv t0, a0 mv t1, a1 la a1, input_file mv a2, a0 jal ra, COMPAREREAD # print input la a1, decoded_file mv a2, t1 jal ra, COMPAREREAD # print output lw ra, 0(sp) addi sp, sp, 4 # restore stack and register LC: lw a0, 0(t0) lw a1, 0(t1) andi t2, a0, 0xff # check if end of string xor t3, a0, a1 # if any bits a0,a1 differ t3!=0 bne t3, x0, FALSECOMPARE # if there are different bytes end bne t2, x0, ENDCOMPARE # if string is over, end addi t0, t0, 4 # get next block addi t1, t1, 4 beq x0, x0, LC # loop FALSECOMPARE: add a0, x0, x0 jalr x0, ra, 0 ENDCOMPARE: addi a0, x0, 1 jalr x0, ra, 0 COMPAREREAD: # prints string in format # path is a1, pointer to data is a2 addi a0, x0, 4 ecall addi a0, x0, 11 li a1, 0x3A ecall addi a0, x0, 11 li a1, 0x20 ecall addi a0, x0, 4 mv a1, a2 ecall addi a0, x0, 11 li a1, 10 ecall jalr x0, ra, 0 ENDP: # ends program li a0,10 ecall