Question: . ORIG x 3 0 0 0 ; Constants for BR instruction BR _ OPCODE . FILL x 0 0 0 0 BR _ MASK

.ORIG x3000 ; Constants for BR instruction BR_OPCODE .FILL x0000 BR_MASK .FILL x0E00 ; Constants for other instructions ADD_OPCODE .FILL x0001 AND_OPCODE .FILL x0101 JMP_OPCODE .FILL x1100 JSR_OPCODE .FILL x0100 LD_OPCODE .FILL x0010 LDI_OPCODE .FILL x1010 LDR_OPCODE .FILL x0110 LEA_OPCODE .FILL x1110 NOT_OPCODE .FILL x1001 RET_OPCODE .FILL x1100 RTI_OPCODE .FILL x1000 ST_OPCODE .FILL x0011 STI_OPCODE .FILL x1011 STR_OPCODE .FILL x0111 TRAP_OPCODE .FILL x1111 ; Buffer for user input BUFFER .BLKW 20 ; States START_STATE .FILL 0 A_STATE .FILL 1 AN_STATE .FILL 2 AND_STATE .FILL 3 ; Entry point MAIN ST R7, SAVE_R7 ; Save R7 LEA R0, PROMPT ; Print prompt message TRAP x22 ; TRAP to print string LEA R0, BUFFER ; Load address of the buffer LEA R1, BUFFER ; Load address of the buffer ADD R2, R0, #1 ; Initialize length counter to 1 INPUT_LOOP GETC ; Read a character OUT ; Echo the character ADD R2, R2, #1 ; Increment length counter STR R0, R1, #0 ; Store the character in the buffer ADD R1, R1, #1 ; Move to the next position ; Check for Enter/Return key LD R3, R0, #0 ADD R3, R3, #-10 BRz PROCESS_INPUT BR INPUT_LOOP PROCESS_INPUT LEA R0, BUFFER ; Load address of the buffer TRAP x20 ; TRAP to print a new line LEA R0, BUFFER ; Load address of the buffer ADD R1, R0, #1 ; Load address of the second character LD R2, R0, #0 ; Load the first character ADD R2, R2, # -32 ; Convert to uppercase ; State machine AND R3, R2, #0xFFE0 ; Mask the first five bits BRz ERROR_STATE ; If the first character is not alphabetic, go to error state ; Transition to the appropriate state based on the first character ADD R4, R3, #-65 ; Calculate the offset to transition to the correct state ADD R4, R4, R3 BR R4 A_STATE ADD R2, R2, #32 ; Convert to lowercase ADD R5, R2, #-68 ; Calculate the offset to the next state ADD R5, R5, R2 BR R5 AN_STATE ADD R2, R2, #32 ; Convert to lowercase ADD R6, R2, #-75 ; Calculate the offset to the next state ADD R6, R6, R2 BR R6 AND_STATE ADD R7, R3, #0 ADD R7, R7, #5 LDR R7, R7, #0 ; Load the opcode based on the state ADD R0, R7, R7 LEA R0, RESULT ; Load address of the result message TRAP x22 ; TRAP to print string BR START_STATE ; Go back to the start state ERROR_STATE LEA R0, ERROR_MSG ; Load address of the error message TRAP x22 ; TRAP to print string START_STATE BR MAIN ; Go back to the main loop ; Data Section PROMPT .STRINGZ "Enter an LC-3 instruction: " SAVE_R7.BLKW 1 RESULT .STRINGZ "The opcode is: " ERROR_MSG .STRINGZ "Error: Invalid instruction! Please enter a valid LC-3 instruction." .END

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!