LAB 5 SUBROUTINE.ORIG X3000; Main programSTARTJSR INIT; Initialize variablesJSR GAME_LOOP; Run the game loopHALT; Subroutines; Initialize variablesINITLD R2, Ans; Load the answerNOT R2, R2; Calculate -AnsADD R2, R2, #1LD R1, Asc; Load ASCII offsetAND R3, R3, #0; Clear guess counterRET; Game loopGAME_LOOPAND R3, R3, #1; Increment guess counterADD R4, R3, #-10; Check if 10 guesses reachedBRz GAME_OVER; If so, go to Game OverTRAP x23; Read input characterADD R0, R0, R1; Adjust input to numeric valueBRn INVALID_INPUT; If negative, invalidADD R4, R0, #-9; If greater than 9, invalidBRp INVALID_INPUTADD R4, R0, R2; Compare input to answerBRz WIN; If correct, winBRp TOO_BIG; If greater, "Too Big"JSR TOO_SMALL; If less, "Too Small"BRnzp GAME_LOOP; Loop backINVALID_INPUTLEA R0, Err2; Display "Invalid Input"JSR DISPLAY_MSGBRnzp GAME_LOOP; Loop backTOO_BIGLEA R0, Big; Display "Too Big"JSR DISPLAY_MSGBRnzp GAME_LOOP; Loop backTOO_SMALLLEA R0, Small; Display "Too Small"JSR DISPLAY_MSGRETWINLEA R0, OK1; Display "Correct!"
JSR DISPLAY_MSGNOT R1, R1; Prepare message for guessesADD R1, R1, #1ADD R0, R3, R1TRAP x21LEA R0, OK2JSR DISPLAY_MSGRETGAME_OVERLEA R0, Err1; Display "Game Over"JSR DISPLAY_MSGRET; Display messageDISPLAY_MSGTRAP x22; Output stringRET; DataAns.FILL #6Asc.FILL x-30Big.STRINGZ "Too Big."Small.STRINGZ "Too Small."OK1.STRINGZ "Correct! You took "OK2.STRINGZ " guesses."Err1.STRINGZ "Game Over. Correct answer is 6"Err2.STRINGZ "Invalid Input.".ENDLab 4 Subroutine.ORIG x3000; Main ProgramLD R7, Cleartext; Pointer to CleartextLD R6, Mask; Load the mask x00FFLD R3, Key; Point to KeyMainLoopAND R4, R4, #0; Clear R4LDR R1, R7, #0; Read two ASCII codesADD R0, R1, #0; Separate the two ASCII codesJSR Encrypt; Call the encryption subroutineSTR R0, R7, #16; Store the encrypted result back to memoryADD R7, R7, #1; Point to the next ASCII codes
BRnzp MainLoop; Repeat for the next charactersDoneHALT; Encryption SubroutineEncryptST R5, SaveR5; Save R5ST R2, SaveR2; Save R2AND R2, R4, #8; Counter for right rotateEncryptLoopAND R3, R3, #0; Clear R3 for rotationADD R0, R0, #0; Rotate right routineBRp SkipADD R3, R4, #1; Adjust for carrySkipADD R0, R0, R3; Finalize rotationADD R2, R2, #-1; Decrement counterBRp EncryptLoop; Loop until counter reaches 0AND R0, R0, R6; Get 1st ASCIIADD R2, R0, #-4; If EOT?BRz ReturnNOT R2, R0AND R2, R2, #1; Toggle the LSBAND R0, R0, R5; Set LSB to 0ADD R0, R2, R0; Add toggled LSB backADD R0, R0, R3; Generate cipherReturnLD R5, SaveR5; Restore R5LD R2, SaveR2; Restore R2RET; Decryption Subroutine (reverse the ciphertext to cleartext)DecryptST R5, SaveR5; Save R5ST R2, SaveR2; Save R2; Perform inverse operations of EncryptAND R0, R0, R6; Get 1st ASCIINOT R2, R0; Invert for toggling LSBAND R2, R2, #1; Toggle the LSBAND R0, R0, R5; Set LSB to 0ADD R0, R2, R0; Add toggled LSB backSUB R0, R0, R3; Subtract key to reverse cipherLD R5, SaveR5; Restore R5LD R2, SaveR2; Restore R2
RET; Data SectionCleartext .FILL x3110Key.FILL x3100Mask.FILL x00FFSaveR5.FILL #0; Space for saving R5SaveR2.FILL #0; Space for saving R2.END