Implement, in LC-3 assembly language, an encryption/decryption program that meet
ID: 3761950 • Letter: I
Question
Implement, in LC-3 assembly language, an encryption/decryption program that meets the following requirements:
Your program should prompt the user for three separate inputs from the keyboard, as follows:
Prompt 1: Enter E)ncrypt or D)ecrypt: Validate the user input. If neither ‘E’ or ‘D’ are entered, redisplay the prompt. Prompt 2: Enter Encryption Key (1-9): Validate the user input. If the entered key is not between 1 and 9, redisplay the prompt.
Prompt 3: Enter Message (<20 char, press when done):
The user will input a character string from the keyboard, terminating the message with the key.
Your program will store the message in memory.
One constraint: entered messages must be less than or equal to 20 characters (no need to range check for this exercise, but make sure you do not enter more than 20 characters).
Your program should output the encrypted or decrypted message to the screen using the following format:
Encrypted Output: Encrypted Message: {the encrypted message}
Decrypted Output: Decrypted Message: {the decrypted message}
Your program must loop and run correctly multiple times in a row
Explanation / Answer
in ; Read in a single character to R0.
add r5, r0, #-10 ; Subtract 10 because enter key is 10.
BRz checkChar ; If zero, branch to checkChar routine.
; Else continue the loop.
str r0, r4, #0 ; Store char in array.
add r4, r4, #1 ; Increment index of array.
add r1, r1, #1 ; Increment input counter.
BR input ; Unconditional branch to input.
// decrypt if not encrypt.
checkChar:
lea r6, neg69 ; Load address of neg69 into R6.
ldr r6, r6, #0 ; Load contents of neg69 into R6 (R6 now holds -69).
add r0, r3, r6 ; Add -69 to the value in R3, to check if it's 'E'.
BRz encrypt ; If zero, branch to encrypt.
; Else check if it's 'D'.
lea r6, neg68 ; Load address of neg68 into R6.
ldr r6, r6, #0 ; Load contents of neg68 into R6 (R6 now holds -68).
add r0, r3, r6 ; Add -68 to the value in R3, to check if it's 'D'.
BRz decrypt ; If zero, branch to decrypt.
lea r4, array ; Load (starting) address of array into R4.
add r5, r1, #0 ; Copy # of characters in message to R5, to use as counter.
; lea r5, pos20 ; Load address of pos20 into R5.
; ldr r5, r5, #0 ; Load contents of pos20 into R5 (used as counter).
encryptLoop:
ldr r0, r4, #0 ; Load contents at array index into R0.
jsr flipBit ; Jump to flip bit routine and jump back when done.
add r0, r6, r2 ; Add the key to the char and store encrypted char in R0.
str r0, r4, #0 ; Store the encrypted char in the array (overwrite) FIXME: Why not store it here
add r4, r4, #1 ; Increment array index.
add r5, r5, #-1 ; Decrement counter.
BRp encryptLoop ; If positive, loop.
BR output ; Else done. Branch to output.
decrypt:
lea r4, array ; Load (starting) address of array into R4.
add r5, r1, #0 ; Copy # of characters in message to R5, to use as counter.
; lea r5, pos20 ; Load address of pos20 into R5.
; ldr r5, r5, #0 ; Load contents of pos20 into R5 (used as counter)
;
decryptLoop:
ldr r0, r4, #0 ; Load contents at array index into R0.
not r2, r2 ; Invert key.
add r2, r2, #1 ; Add 1, key is now negative.
add r0, r0, r2 ; Subtract key from char and store in R0.
jsr flipBit ; Jump to flip bit routine and jump back when done.
str r6, r4, #0 ; Store the decrypted char in the array (overwrite).
add r4, r4, #1 ; Increment array index.
add r5, r5, #-1 ; Decrement counter.
BRp decryptLoop ; If positive, loop.
BR output ; Else done. Branch to output.
flipBit:
not r3, r0
and r3, r3, #1 ; Note: 1 = 000 0001
and r0, r0, #-2 ; Note: -2 = 111 1110 (two's complement, signed representation)
add r6, r0, r3
ret ; Return to the instruction whos address is stored in R7.
output:
lea r4, array ; Load (starting) address of array.
outputLoop:
ldr r0, r4, #0 ; Load contents of address at array index into R0.
out; ; Print character.
add r4, r4, #1 ; Increment array index.
add r1, r1, #-1 ; Decrease counter.
BRp outputLoop ; If positive, loop.
halt