# TODO: PUT YOUR NAME AND STUDENT NUMBER HERE!!!
# TODO: ADD OTHER COMMENTS YOU HAVE HERE AT THE TOP OF THIS FILE
# TODO: SEE LABELS FOR PROCEDURES YOU MUST IMPLEMENT AT THE BOTTOM OF THIS FILE
# TODO: NOTICE THE TODO IN THE .DATA SEGMENT
# TODO: RENAME THIS FILE AS PER THE SUBMISSION REQUIREMENTS
# Menu options
# r – read text buffer from file
# p – print text buffer
# e – encrypt text buffer
# d – decrypt text buffer
# w – write text buffer to file
# g – guess the key
# q – quit
.data
MENU: .asciiz “Commands (read, print, encrypt, decrypt, write, guess, quit):”
REQUEST_FILENAME: .asciiz “Enter file name:”
REQUEST_KEY: .asciiz “Enter key (upper case letters only):”
REQUEST_KEYLENGTH: .asciiz “Enter a number (the key length) for guessing:”
REQUEST_LETTER: .asciiz “Enter guess of most common letter:”
ERROR: .asciiz “There was an error.\n”
FILE_NAME: .space 256 # maximum file name length, should not be exceeded
KEY_STRING: .space 256 # maximum key length, should not be exceeded
.align 2 # ensure word alignment in memory for text buffer (not important)
TEXT_BUFFER: .space 10000
.align 2 # ensure word alignment in memory for other data (probably important)
# TODO: define any other spaces you need, for instance, an array for letter frequencies
##############################################################
.text
move $s1 $0 # Keep track of the buffer length (starts at zero)
MainLoop: li $v0 4 # print string
la $a0 MENU
syscall
li $v0 12 # read char into $v0
syscall
move $s0 $v0 # store command in $s0
jal PrintNewLine
beq $s0 ‘r’ read
beq $s0 ‘p’ print
beq $s0 ‘w’ write
beq $s0 ‘e’ encrypt
beq $s0 ‘d’ decrypt
beq $s0 ‘g’ guess
beq $s0 ‘q’ exit
b MainLoop
read: jal GetFileName
li $v0 13 # open file
la $a0 FILE_NAME
li $a1 0 # flags (read)
li $a2 0 # mode (set to zero)
syscall
move $s0 $v0
bge $s0 0 read2 # negative means error
li $v0 4 # print string
la $a0 ERROR
syscall
b MainLoop
read2: li $v0 14 # read file
move $a0 $s0
la $a1 TEXT_BUFFER
li $a2 9999
syscall
move $s1 $v0 # save the input buffer length
bge $s0 0 read3 # negative means error
li $v0 4 # print string
la $a0 ERROR
syscall
move $s1 $0 # set buffer length to zero
la $t0 TEXT_BUFFER
sb $0 ($t0) # null terminate the buffer
b MainLoop
read3: la $t0 TEXT_BUFFER
add $t0 $t0 $s1
sb $0 ($t0) # null terminate the buffer that was read
li $v0 16 # close file
move $a0 $s0
syscall
la $a0 TEXT_BUFFER
jal ToUpperCase
print: la $a0 TEXT_BUFFER
jal PrintBuffer
b MainLoop
write: jal GetFileName
li $v0 13 # open file
la $a0 FILE_NAME
li $a1 1 # flags (write)
li $a2 0 # mode (set to zero)
syscall
move $s0 $v0
bge $s0 0 write2 # negative means error
li $v0 4 # print string
la $a0 ERROR
syscall
b MainLoop
write2: li $v0 15 # write file
move $a0 $s0
la $a1 TEXT_BUFFER
move $a2 $s1 # set number of bytes to write
syscall
bge $v0 0 write3 # negative means error
li $v0 4 # print string
la $a0 ERROR
syscall
b MainLoop
write3:
li $v0 16 # close file
move $a0 $s0
syscall
b MainLoop
encrypt: jal GetKey
la $a0 TEXT_BUFFER
la $a1 KEY_STRING
jal EncryptBuffer
la $a0 TEXT_BUFFER
jal PrintBuffer
b MainLoop
decrypt: jal GetKey
la $a0 TEXT_BUFFER
la $a1 KEY_STRING
jal DecryptBuffer
la $a0 TEXT_BUFFER
jal PrintBuffer
b MainLoop
guess: li $v0 4 # print string
la $a0 REQUEST_KEYLENGTH
syscall
li $v0 5 # read an integer
syscall
move $s2 $v0
li $v0 4 # print string
la $a0 REQUEST_LETTER
syscall
li $v0 12 # read char into $v0
syscall
move $s3 $v0 # store command in $s0
jal PrintNewLine
move $a0 $s2
la $a1 TEXT_BUFFER
la $a2 KEY_STRING
move $a3 $s3
jal GuessKey
li $v0 4 # print String
la $a0 KEY_STRING
syscall
jal PrintNewLine
b MainLoop
exit: li $v0 10 # exit
syscall
###########################################################
PrintBuffer: li $v0 4 # print contents of a0
syscall
li $v0 11 # print newline character
li $a0 ‘\n’
syscall
jr $ra
###########################################################
PrintNewLine: li $v0 11 # print char
li $a0 ‘\n’
syscall
jr $ra
###########################################################
PrintSpace: li $v0 11 # print char
li $a0 ‘ ‘
syscall
jr $ra
#######################################################
GetFileName: addi $sp $sp -4
sw $ra ($sp)
li $v0 4 # print string
la $a0 REQUEST_FILENAME
syscall
li $v0 8 # read string
la $a0 FILE_NAME # up to 256 characters into this memory
li $a1 256
syscall
la $a0 FILE_NAME
jal TrimNewline
lw $ra ($sp)
addi $sp $sp 4
jr $ra
###########################################################
GetKey: addi $sp $sp -4
sw $ra ($sp)
li $v0 4 # print string
la $a0 REQUEST_KEY
syscall
li $v0 8 # read string
la $a0 KEY_STRING # up to 256 characters into this memory
li $a1 256
syscall
la $a0 KEY_STRING
jal TrimNewline
la $a0 KEY_STRING
jal ToUpperCase
lw $ra ($sp)
addi $sp $sp 4
jr $ra
###########################################################
# Given a null terminated text string pointer in $a0, if it contains a newline
# then the buffer will instead be terminated at the first newline
TrimNewline: lb $t0 ($a0)
beq $t0 ‘\n’ TNLExit
beq $t0 $0 TNLExit # also exit if find null termination
addi $a0 $a0 1
b TrimNewline
TNLExit: sb $0 ($a0)
jr $ra
##################################################
# converts the provided null terminated buffer to upper case
# $a0 buffer pointer
ToUpperCase: lb $t0 ($a0)
beq $t0 $zero TUCExit
blt $t0 ‘a’ TUCSkip
bgt $t0 ‘z’ TUCSkip
addi $t0 $t0 -32 # difference between ‘A’ and ‘a’ in ASCII
sb $t0 ($a0)
TUCSkip: addi $a0 $a0 1
b ToUpperCase
TUCExit: jr $ra
###################################################
# END OF PROVIDED CODE…
# TODO: use this space below to implement required procedures
###################################################
##################################################
# null terminated buffer is in $a0
# null terminated key is in $a1
EncryptBuffer: # TODO: Implement this function!
jr $ra
##################################################
# null terminated buffer is in $a0
# null terminated key is in $a1
DecryptBuffer: # TODO: Implement this function!
jr $ra
###########################################################
# a0 keySize – size of key length to guess
# a1 Buffer – pointer to null terminated buffer to work with
# a2 KeyString – on return will contain null terminated string with guess
# a3 common letter guess – for instance ‘E’
GuessKey: # TODO: Implement this function!
jr $ra