Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a simple four function calculator in the LC3 assembly language that wi

ID: 3813748 • Letter: I

Question

Implement a simple four function calculator in the LC3 assembly language that will run on the LC-3 simulator. You will use these subroutines to implement the calculator. The inputs to the calculator will not exceed 4 digits. This is to avoid overflow since the range of numbers handled by the 16-bit LC-3 is [-32768: +32767]. The user interaction with the calculator is shown below: S-3 Calculator Operation (+, -, *, /)? + Enter the first operand (SRC1): 34 Enter the second operand (SRC2): 27 Sum is: 61 Continue (Y, N)? Y Operation (+, -, *, /)? Enter the first operand (SRC1): 378 Enter the second operand (SRC2): 237 Enter the Operator (+, -, *, /): - Difference is: 141 Continue (Y, N) Y Operation (+, -, *, /)? - Enter the first operand (SRC1): 34 Enter the second operand (SRC 2): 75

Explanation / Answer

.ORIG x3000 ;initializes all registers to zero AND R0, R0, #0 AND R1, R1, #0 AND R2, R2, #0 AND R3, R3, #0 AND R4, R4, #0 AND R5, R5, #0 AND R6, R6, #0 AND R7, R7, #0 ;this block will ask for user input then display it to the monitor NEXTCHARACTER GETC OUT ;reads input and determines whether or not it is a valid character ;Space? LD R1, OPPOSPACE ADD R1, R0, R1 BRz NEXTCHARACTER ;New line? LD R1, OPPOLINE ADD R1, R0, R1 BRz DONE ;Multiplication? LD R1, OPPOMULTI ADD R1, R0, R1 BRz DECODE Brn INVALID_CHARACTER ;Exponent? LD R1, OPPOEXPO ADD R1, R0, R1 BRz DECODE Brp INVALID_CHARACTER ;Is at most 9? LD R1, OPPOCOLON ADD R1, R0, R1 Brzp INVALID_CHARACTER ;Is it an apostrophe? LD R1, OPPOAPOST ADD R1, R0, R1 BRz INVALID_CHARACTER ;Is it a decimal point? LD R1, OPPODECI ADD R1, R1, R0 BRz INVALID_CHARACTER ;Operand or operator? LD R1, OPPOZERO ADD R1, R1, R0 BRzp PUSH BRn DECODE INVALID_CHARACTER .STRINGZ "Error invalid input" BRnzp DONE ERROR .STRINGZ "operation jumps are incorrect" BRnzp DONE ;stores negation of operators in stack DECODE NOT R0, R0 ADD R0, R0, #1 ;stores the opposite hex value ;figure out what operation to jump to LD R1, ADDING ADD R1, R1, R0 BRz ADDITION ;jumps to addition sequence LD R1, SUBTRACT ADD R1, R1, R0 BRz SUBTRACTION ;jumps to subtraction sequence LD R1, MULTI ADD R1, R1, R0 BRz MULTIPLICATION ;jumps to multiplication seq LD R1, DIVIDE ADD R1, R1, R0 Brz DIVISION ;jumps to division sequence LD R1, EXPONENT ADD R1, R1, R0 BRz EXPONENTIAL ;jumps to exponent sequence Brnp ERROR ;pushes and pops characters into and out of the stack PUSH ADD R6, R6, #-1 ;moves stack pointer up STR R0, R6, #0 ;stores R0 at current spot BRnzp NEXTCHARACTER POP LDR R5, R6, #0 ;loads R6 into R5 ADD R6, R6, #1 ;moves stack pointer ;here are the operations ;R2 and R3 are operands R2 holds answer ADDITION ADD R2, R5, #0 ;loads first number into R2 LDR R5, R6, #0 ;loads R6 into R5 ADD R6, R6, #1 ;moves stack pointer ADD R3, R5, #0 ;loads second number into R2 ADD R2, R2, R3 ;stores answer in R2 ADD R6, R6, #-1 ;moves stack pointer up STR R2, R6, #0 ;stores R2 in empty spot BRnzp NEXTCHARACTER ;goes back to user input to read SUBTRACTION ADD R3, R5, #0 ;loads number into R3 LDR R5, R6, #0 ADD R6, R6, #1 ;moves pointer ADD R2, R5, #0 ;loads digit to be added into R2 NOT R3, R3 ADD R3, R3, #1 ;2's complement of R3 ADD R2, R2, R3 ;subtraction of the two numbers ADD R6, R6, #-1 ;moves stack pointer up STR R2, R6, #0 ;stores R2 in empty spot BRnzp NEXTCHARACTER ;goes back to user input to read MULTIPLICATION ADD R2, R5, #0 ;loads number to be multiplied LDR R5, R6, #0 ADD R6, R6, #1 ;moves pointer ADD R3, R5, #0 ;loads number to be multiplied MULTIPLICATIONLOOP ADD R2, R3, R2 ADD R3, R3, #-1 BRp MULTIPLICATIONLOOP ADD R6, R6, #-1 ;moves stack pointer up STR R2, R6, #0 ;stores R2 in empty spot BRnzp NEXTCHARACTER ;goes back to user input to read ;R1 will be a place holder here ;R4 will hold quotient ;R5 will hold remainder ;R2 is dividend ;R3 is divisor DIVISION ADD R3, R5, #0 ;loads divisor LDR R5, R6, #0 ADD R6, R6, #1 ;moves pointer after loading R5 ADD R2, R5, #0 ;loads dividend NOT R1, R3 ADD R1, R1, #1 DIVLOOP ADD R4, R4, #1 AND R2, R2, R0 BRn NEGATIVE BRp DIVLOOP BRz ZERO NEGATIVE ADD R4, R4, #-1 ADD R5, R2, R3 ADD R6, R6, #-1 ;moves stack pointer up STR R4, R6, #0 ;stores R4 in empty spot ZERO BRnzp NEXTCHARACTER ;goes back to user input to read ;R2 is exponent ;R3 is base and answer ;R4 is base copy ;R1 is base copy EXPONENTIAL ADD R2, R5, #0 ;loads exponent into R2 LDR R5, R5, #0 ADD R6, R6, #1 ;moves pointer after loading R5 ADD R3, R5, #0 ;loads base into R3 ADD R1, R1, R3 ;loads base into R1 OUTEREXPOLOOP ADD R4, R1, #0 ;loads base into R4 for counter INNEREXPOLOOP ADD R3, R3, R3 ;starts multiplication sequence ADD R4, R4, #-1 ;decrements base counter BRp INNEREXPOLOOP ADD R2, R2, #-1 ;determines if needs to re-enter BRp OUTEREXPOLOOP ADD R6, R6, #-1 ;moves stack pointer up STR R3, R6, #0 ;stores R3 in empty spot BRnzp NEXTCHARACTER ;goes back to user input to read DONE HALT MULTI .FILL x002A EXPONENT .FILL x005E ADDING .FILL x002B SUBTRACT .FILL x002D DIVIDE .FILL x002F OPPOSPACE .FILL xFFDF OPPOLINE .FILL xFFF5 OPPOMULTI .FILL xFFD5 OPPOEXPO .FILL xFFA1 OPPOCOLON .FILL xFFC5 OPPOAPOST .FILL xFFD3 OPPODECI .FILL xFFD1 OPPOZERO .FILL xFFCF STACK_START .FILL x5000 STACK_POINTER .FILL x5000 STACK_TOP .FILL x5050 OPPOSTART .FILL xAFFF .END

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote