LC3 program to enter 2 values and show the difference

LC3 Program to Enter 2 Values and Show the Difference

 Objective
Develop an LC-3 assembly program to subtract two integers provided by the user.

Problem Statement

The objective of this programming assignment is to develop a program that asks the user for two
numbers and displays their difference on the screen. The desired output for the program is shown
below.

Load the OS
In order to be able to interact with a user and to print to the screen, we will need to load an OS to
LC-3 that makes this possible. The BB LC-3 folder has a file named lc3os.asm. Download and
assemble this file and load the .obj to your LC-3. This OS has the required trap instructions
programmed. You will load the rest of your files over this .obj. Thus, load lc3os.obj first and
then load myprogram.obj over it.

Add and print
Start the assignment by running the following example from the LC-3 Guide: “Example
2:
Debugging the program to input numbers and add them”. Create an asm file and paste the
following code:
.ORIG x3000
TRAP x23 ;the trap instruction which is also known as “IN”
ADD R1,R0,x0 ;move the first integer to register 1
TRAP x23 ;another “IN”
ADD R2,R0,R1 ;add the two integers
LEA R0,MESG ;load the address of the message string
TRAP x22 ;”PUTS” outputs a string
ADD R0,R2,x0 ;move the sum to R0, to be output
TRAP x21 ;display the sum
DONE HALT
MESG .STRINGZ “The sum of those two numbers is ”
.END
Assemble and load the program to LC-3. Notice that values stored that x3009 to x3028
correspond to the ASCII representation of the string stored in MESG.
Place a breakpoint at line x3008 and run the program. First instruction (TRAP x23) is similar to a
Scanner object in Java. It is used to collect a char input from the user.

The program asks the user for two inputs and adds these two integers and displays the sum on the
screen. Notice that we are limited to a sum of less than 9 to simplify the program.
Enter “4” or any other single digit number to the left corner of LC-3 as shown in Figure 1. Notice
that R0 and R1 are loaded with the ASCII value of your int as you enter them. For instance,
ASCII representation for “4” is 0x34. Enter “1” as your second input. “1” is 0x31 in ASCII.

Debugging
We expect the sum to be “5”. However, the program displays “The sum of those two numbers is
e”.
What is the error? How can we fix it?
Look at an ASCII table to find how is “e” represented. You will find that it is located at 0x65 or
0x34+0x31.
This means that our addition works; however, it is not in performed in our desired space of
integers. To fix it, we will use the same trick that we use for Java: we will dereference the ASCII
value of “0” (0x30) to convert the char to an int. This means that given an input of 0x34, we can
get “4” by computing 0x34 – 0x30.

First, let’s create some variables to make our lives easier:
ASCII .FILL x30 ;the mask to add to a digit to convert it to ASCII
NEGASCII .FILL xFFD0 ;the negative version of the ASCII mask (-x30)
We will then modify our program to:
– Convert the input char 1 to an int using NEGASCII
– Convert the input char 2 to an int using NEGASCII
– Convert the sum to a char using ASCII
Update your program using the code below to reflect these changes:
.ORIG x3000
LD R6, ASCII ;load the ASCII “0” offset
LD R5, NEGASCII ;load the negated ASCII “0” offset
TRAP x23 ;the trap instruction which is also known as “IN”
ADD R1,R0,x0 ;move the first integer to register 1
ADD R1, R1, R5 ;convert first ASCII number to numerical value
TRAP x23 ;another “IN”
ADD R0, R0, R5 ;convert second ASCII number to numerical value
ADD R2,R0,R1 ;add the two integers: R2 = R0 + R1
ADD R2, R2, R6 ;convert the sum to its ASCII representation
LEA R0,MESG ;load the address of the message string
TRAP x22 ;”PUTS” outputs a string
ADD R0,R2,x0 ;move the sum to R0, to be output
TRAP x21 ;display the sum
DONE HALT
MESG .STRINGZ “The sum of those two numbers is ”
ASCII .FILL x30 ;the mask to add to a digit to convert it to ASCII
NEGASCII .FILL xFFD0 ;the negative version of the ASCII mask (-x30)
.END

Subtract
You are to modify this program to subtract these two numbers and display their difference. The
desired output for the program is shown below. The desired output text is circled in the bottom
left of the figure. You can assume that both inputs will be integers 0 to 9 and that the first input is
greater than the second input. Thus, you can display only the positive results, such as 7 – 4 = 3.

The flowchart can be hand-drawn or scanned. If you want to use a phone to take a pic of your
flow chart, do so at your own risk. If the picture is not professional looking, you will lose points.
You can use an app such as “Office Lens” to “scan” your hand-drawn image with your phone.

Solution

.ORIG x3000

LD R5, NEGASCII0 ;load the negated ASCII “0” offset

TRAP x23 ;the trap instruction which is also known as “IN”

ADD R1,R0,R5 ;move the first integer to register 1

TRAP x23 ;another “IN”

ADD R2,R0,R5 ;move the second integer to register 2

NOT R2,R2

ADD R2,R2,x1 ;negate R2 (-A == (NOT A) + 1)

LEA R0,MESG ;load the address of the message string

TRAP x22 ;”PUTS” outputs a string

ADD R2,R2,R1 ;add the two integers

BRZP NOTNEG ;total is >= 0 so output

LD R0,MINUS

TRAP x21 ;output – character

NOT R2,R2

ADD R2,R2,x1 ;negative R2 (since it was -#)

NOTNEG

LD R5,ASCII0 ;load the ASCII “0” value

ADD R0,R2,R5 ;move the difference to R0, to be output

TRAP x21 ;display the difference

DONE HALT

MESG  .STRINGZ “The difference of those two numbers is ”

MINUS .FILL 0x002D ;- CHARACTER

ASCII0      .FILL 0x0030 ;theascii value of 0

NEGASCII0 .FILL xFFD0 ;the negative version of the ASCII mask (-x30)

.END