Friday, 20 December 2013

Now create an array of 10 values, starting at location x4000 so that it is “out of reach” of the instructions in your code (which should, as usual, load at address x3000). Using what you learnt of console i/o last week, populate that array with characters input from the keyboard.

    .ORIG x3000
    LD R3,FT        ; load x4000 in R3 as array base address
    LD R2,Count        ; load #10 in R2 as counter from 10 to 1
Loop
    TRAP x23        ; take input from user
    STR R0,R3,#0        ; store inputted chatacter in mem addr stored in R3
    ADD R3,R3,#1        ; increment R3 (mem location)
    ADD R2,R2,#-1        ; decrement R2 (counter)
    BRp Loop
    HALT
FT    .FILL x4000
Count    .FILL #10
    .END

Set up an array of 10 values following your code, and programmatically populate it with the first ten powers of 2, starting with 21 = 2, then read back the last value into register 2.

    .ORIG x3000       
    LD R1,Max       
    LD R0,Zero       
    LD R7,Zero   
Loop:
    LEA R6,Array
    LD R3,Zero
    ADD R4,R0,#0
Inner:
    ADD R3,R3,#2
    ADD R4,R4,#-1
    BRp Inner

    ADD R6,R6,R7
    STR R3,R6,#0

    ADD R2,R0,#0
    AND R0,R0,#0
MostInner:
    ADD R0,R0,#2
    ADD R2,R2,#-1
    BRp MostInner

    ADD R7,R7,#1
    ADD R1,R1,#-1
    BRp Loop


    ; Loading Last value in register R2
    LDR R2,R6,#0

    HALT
Array    .BLKW #10
Max    .FILL #10
Zero    .FILL #0
    .END

a. Using the .FILL & .BLKW pseudo-ops, store the value x4000 in one location, and reserve the following location for storing a result. Label ONLY the .FILL location. b. Use the direct mode to load the stored value into R2. c. Double the number in R2, and use the relative mode to store it to the reserved location (i.e. the address following the .FILL). d. Now use the indirect mode to store the same number to the address x4000.

.ORIG    x3000                ; Starting program from address x3000;
LD R2,ONLY                ; Using Direct mode to store the value of ONLY into R2.

ADD R3,R2,R2                ; Adding R2 and R2 then Storing result in register R3.

LEA R4,ONLY                ; Loading the address stored in lable ONLY into register R4.
STR R3,R4,1                ; Using Relative mode store the value of R3 into
                    ; relative memory address (address stored in register R4+1) which was reserved.

STI R3,ONLY                ; Use Indirect mode to store the value of R3 into memory address stored in Label ONLY (i.e.-x4000). 
HALT
ONLY .FILL x4000            ; Fill the value x4000 into Label ONLY.
.BLKW 1                    ; Reserving 1 block for storing the result;
.END                    ; End of the Program.

Modify your code from exercise 4 so that each output character is separated by a space, or output to a new line.

    .ORIG x3000

; Take Input from user
    LEA R3,Label        ; load array base addr in R3
    LD R2,Ten        ; load 10 in R2 (counter)
Input
    TRAP x23        ; input character
    STR R0,R3,#0        ; store inputted char in memory
    ADD R3,R3,#1        ; increment array base addr
    ADD R2,R2,#-1        ; decrement counter register R2
    BRp Input        ; branch to input if R2 is positive

; Add 1 to each inputted character
    LEA R3,Label        ; load array base addr in R3
    LD R2,Ten        ; load 10 in R2 (counter)
Add1
    LDR R0,R3,#0        ; load value from mem in R0
    ADD R0,R0,#1        ; increment R0 so it'll be plus 1 character
    STR R0,R3,#0        ; store inputted char in memory
    ADD R3,R3,#1        ; increment array base addr
    ADD R2,R2,#-1        ; decrement counter register R2
    BRp Add1        ; branch to Add1 if R2 is positive

; Output resultant Array character each in New Line
    LEA R3,Label        ; load array base addr in R3
    LD R2,Ten        ; load 10 in R2 (counter)
OutputByNewLine
    LDR R0,R3,#0        ; load value from mem in R0
    TRAP x21        ; display character stored in R0 on console
    AND R0,R0,#0
    LD R0,NewLine        ; load ascii value of NewLine in R0
    TRAP x21
    ADD R3,R3,#1        ; increment array base addr
    ADD R2,R2,#-1        ; decrement counter register R2
    BRp OutputByNewLine    ; branch to Output if R2 is positive

; Output resultant Array character each separated by Space
    LEA R3,Label        ; load array base addr in R3
    LD R2,Ten        ; load 10 in R2 (counter)
OutputBySpace
    LDR R0,R3,#0        ; load value from mem in R0
    TRAP x21        ; display character stored in R0 on console
    AND R0,R0,#0
    LD R0,Space        ; load ascii value of Space in R0
    TRAP x21
    ADD R3,R3,#1        ; increment array base addr
    ADD R2,R2,#-1        ; decrement counter register R2
    BRp OutputBySpace    ; branch to Output if R2 is positive

    HALT
Label    .BLKW #10
Ten    .FILL #10
NewLine    .FILL x000A
Space    .FILL x0020
    .END

We can very easily turn the array created in exercise 3 into a c-string: just make it an 11 element array, with the last element a null (remember that in the LC-3, each 8-bit character occupies the lower byte of a 16-bit word, so the last word in the array will be x0000). Adjust your code from exercise 3 to create a 10 character c-string (i.e. 10 input characters terminated with the null character). Load the starting address of the string into R0, then use TRAP x22 to output it to the monitor, just as you did with the c-string constructed by the .STRINGZ pseudo-op.

    .ORIG x3000

; take 10 character input from user
    LEA R3,Label    ; load array base addr in R3
    LD R2,Ten    ; load 10 in R2 (counter)
Input
    TRAP x23    ; input character
    STR R0,R3,#0    ; store inputted char in memory
    ADD R3,R3,#1    ; increment array base addr
    ADD R2,R2,#-1    ; decrement counter register R2
    BRp Input    ; branch to input if R2 is positive
    LD R0,Null    ; load Null in R0
    STR R0,R3,#0    ; store Null at end of array
       
    LEA R3,Label    ; load array base addr in R3
    LD R2,Ten    ; load 10 in R2 (counter)

; add 1 to each character
Add1
    LDR R0,R3,#0    ; load value from mem in R0
    ADD R0,R0,#1    ; increment R0 so it'll be plus 1 character
    STR R0,R3,#0    ; store inputted char in memory
    ADD R3,R3,#1    ; increment array base addr
    ADD R2,R2,#-1    ; decrement counter register R2
    BRp Add1    ; branch to Add1 if R2 is positive

; output characters by using TRAP x22 from memory address stored in R0 to Null
    LEA R0,Label    ; store base array addr in R0
    TRAP x22    ; display output from memory address from R0 untill it founds Null

    HALT
Label    .BLKW #11
Null    .FILL x0000
Ten    .FILL #10
    .END

Now write a routine that takes a sequence of 10 characters from the keyboard (echoing each character as it is input) and stores them in an array. Set up a loop that traverses the array adding 1 to each value (this will have the effect of turning a into b, L into M, 8 into 9, etc.) Finally, traverse the array again and output the 10 new characters to the screen. A simple way to set up an array is to use the .BLKW pseudo-op: label .BLKW #10 This sets aside 10 locations in memory, starting at the address "label". You can then load this address into a register using the LEA instruction: LEA R4, label This decodes "label" to the memory address it stands for, and loads that address into register 4 (note that this is quite different from LD, which loads the contents of the memory location "label" into the register; LEA doesn't read anything from memory, it just decodes the address itself).Once you have the starting address of the array in a register, you can use LDR & STR instructions to access it. How will you access the successive addresses? (Note: you may hard-code the number 10 as the loop counter).

    .ORIG x3000

; take 10 character input from user
    LEA R3,Label    ; load array base addr in R3
    LD R2,Ten    ; load 10 in R2 (counter)
Input
    TRAP x23    ; input character
    STR R0,R3,#0    ; store inputted char in memory
    ADD R3,R3,#1    ; increment array base addr
    ADD R2,R2,#-1    ; decrement counter register R2
    BRp Input    ; branch to input if R2 is positive

; add 1 to each inputted character
    LEA R3,Label    ; load array base addr in R3
    LD R2,Ten    ; load 10 in R2 (counter)
Add1
    LDR R0,R3,#0    ; load value from mem in R0
    ADD R0,R0,#1    ; increment R0 so it'll be plus 1 character
    STR R0,R3,#0    ; store inputted char in memory
    ADD R3,R3,#1    ; increment array base addr
    ADD R2,R2,#-1    ; decrement counter register R2
    BRp Add1    ; branch to Add1 if R2 is positive

; output each new character
    LEA R3,Label    ; load array base addr in R3
    LD R2,Ten    ; load 10 in R2 (counter)
Output
    LDR R0,R3,#0    ; load value from mem in R0
    TRAP x21    ; display character stored in R0 on console
    ADD R3,R3,#1    ; increment array base addr
    ADD R2,R2,#-1    ; decrement counter register R2
    BRp Output    ; branch to Output if R2 is positive

    HALT
Label    .BLKW #10
Ten    .FILL #10
    .END

You should be familiar with c-strings from C or C++. They are simply a sequence of characters terminated with a null character ( x00). The LC-3 pseudo-op .STRINGZ takes a text string as a parameter and builds a c-string: label .STRINGZ "Hello world!" ;you're probably sick of "hello world" by now ... ; so make your own message! This pseudo-op stores the characters of the string, one per memory location, starting with the location "label". A null (x00) is written to the memory location following the last character. The TRAP x22 instruction will take the contents of R0 to be the starting address of a c-string, and output each character of the string until it hits the null. Write code that creates a message using .STRINGZ and outputs it to the screen using TRAP x22. Examine the memory locations starting with "label" to make sure you understand how the assembler has set up the string.

    .ORIG x3000
    LEA R0,Label    ; load array base addr in R3
    TRAP x22    ; output from address in R2 untill it gets x0000 (null)
    HALT
Label    .STRINGZ "Hello World!"    ; create string array in memory
    .END

Input any positive decimal number less than 32,000 at the keyboard and convert it to its binary value for storage in a register (you can use your code from assignment 4 for this). Now add 1 to the stored number, and output the result to the console as a decimal number (i.e. the inverse of assignment 4).

    .ORIG x3000
    AND R2 R2 #0        ; counter
    AND R5 R5 #0
Input
    TRAP x20
    ADD R1 R0 #-10
    BRz InDone

    TRAP x21
    ADD R2 R2 #1
    LD R1 FE
    ADD R0 R0 R1        ; minus 48
    ADD R3 R5 #0
    AND R5 R5 #0
Loop
    ADD R3 R3 #-1
    BRn Skip
    ADD R5 R5 #10
    BR Loop
Skip
    ADD R5 R5 R0
    BR Input
InDone
    LD R3 Max
    NOT R1 R5
    ADD R1 R1 #1
    ADD R1 R3 R1
    BRnz Invalid
   
    ADD R5 R5 #1        ; valid input


    AND R0 R0 #0
    ADD R0 R0 #10
    TRAP x21
Print
    ADD R2 R2 #-1
    BRn Done
    JSR CountPower
    JSR Divide
    BR Print
Invalid                ; invalid input
    AND R0 R0 #0
    ADD R0 R0 #10
    TRAP x21
    LEA R0 ErrMsg
    TRAP x22
Done
    HALT

CountPower
    AND R4 R4 #0        ; R4 will contain n power of 10
    AND R0 R0 #0
   
    ADD R0 R0 #10
    ADD R6 R2 #0
Again
    ADD R6 R6 #-1        ; decrement counter
    BRnz Over
    AND R1 R1 #0
    ADD R1 R1 #10        ; counter for 10 to 1
Loop2
    ADD R4 R4 R0
    ADD R1 R1 #-1
    BRp Loop2

    ADD R0 R4 #0
    AND R4 R4 #0
    BR Again
Over
    ADD R4 R0 #0
    RET

Divide
    add r6 r7 #0        ; backup r7
    ADD R2 R2 #0
    BRz R2Zero
    AND R0 R0 #0
    NOT R1 R4
    ADD R1 R1 #1        ; negate 10 to power answer
Loop3
    ADD R5 R5 R1
    BRn Skip3
    ADD R0 R0 #1
    BR Loop3
R2Zero
    ADD R0 R5 #0
Skip3
    ADD R5 R5 R4
    LD R3 FE
    NOT R3 R3
    ADD R3 R3 #1        ; convert -48 to 48
    ADD R0 R0 R3
    TRAP x21
    add r7 r6 #0        ; reload r7
    RET

Max    .FILL #32000
FE    .FILL #-48
ErrMsg    .STRINGZ "Outof Range, Input should be less than 32000"
    .END

Now modify the routine of exercise 2 to perform full input validation, as well as allowing the user to input the bit pattern with spaces separating groups of bits – e.g. the user might input the number as b0001 0010 0011 0100. This means that you must test the first digit for ‘b’, then each succeeding digit for ‘0’, ‘1’, or ‘ ‘. If it is ‘0’ or ‘1’, continue building the binary number; if it is ‘ ‘, ignore it & get the next digit; if it is anything else, output an error message & quit the program. Likewise, if the user attempts to enter a 17th digit, or a CR/LF before the 16th digit, output an error message and quit.

    .ORIG x3000
    LD R2 Counter
    LD R5 MinusTen
    LD R4 Max
    AND R3 R3 #0    ; will store ans

    LEA R0 Prompt
    TRAP x22
Input
    TRAP x20
    TRAP x21
   
    LD R7 Space
    ADD R7 R0 R7
    BRz Input

    ADD R1 R0 R5
    BRz Enter

    ADD R2 R2 #0
    BRz Invalid

    LD R7 Svntn
    ADD R7 R2 R7
    BRz Checkb

    LD R7 Zero
    ADD R7 R0 R7
    BRz ZeroLbl

    LD R7 One
    ADD R7 R0 R7
    BRz OneLbl
    BR Invalid

Enter
    ADD R2 R2 #0
    BRnp Invalid
    ADD R0 R3 #0
    TRAP x21
    BR Done
Checkb
    ADD R2 R2 #-1
    LD R7 B
    ADD R7 R0 R7
    BRnp Invalid
    BR Input
ZeroLbl
    ADD R2 R2 #-1
    JSR DivBy2
    BR Input
OneLbl
    ADD R2 R2 #-1
    ADD R3 R3 R4
    JSR DivBy2
    BR Input
Invalid
    AND R0 R0 #0
    ADD R0 R0 #10
    TRAP x21
    LEA R0 ErrMsg
    TRAP x22
Done
    HALT
DivBy2                ; subroutine that divides max by 2
    AND R0 R0 #0
    ADD R1 R4 #0
Loop
    ADD R1 R1 #-2
    BRn Skip
    ADD R0 R0 #1
    BR Loop
Skip
    ADD R4 R0 #0
    RET

Prompt    .STRINGZ "Enter Binary: "
ErrMsg    .STRINGZ "Invalid Input"
Counter    .FILL #17
Svntn    .FILL #-17
MinusTen .FILL #-10
B    .FILL #-98
Zero    .FILL #-48
One    .FILL #-49
Space    .FILL #-32
Max    .FILL #32768
    .END

The inverse of exercise 1: write a bit pattern input routine. Use the .STRINGZ pseudo-op and the trap instruction PUTS to prompt the user to input a 16-bit binary number preceded by ‘b’, and terminating with the CR/LF character (ASCII x0A, the newline character). The user inputs the characters b0001001000110100 (without spaces), which you are to store in a register (R2) as the corresponding 16-bit number. Assume the user will always input the initial ‘b’ (which you may simply discard), and exactly 16 bits. Combine this with the code of exercise 1 – i.e. replace the hard-coded setup of R2 with the user input code.

    .ORIG x3000
    LD R3 Zr
    LD R1 On
    LEA R2 Binary
    AND R6 R6 #0    ; it'll contain your answer
    LD R5 Max
Start
    ADD R2 R2 #1
    LDR R7 R2 #0
    BRz Done

    NOT R7 R7
    ADD R7 R7 #1
    ADD R7 R3 R7
    BRz Zero

    ADD R6 R6 R5
    JSR DivBy2
    BR Start
Zero
    JSR DivBy2
    BR Start
Done
    HALT
DivBy2
    AND R0 R0 #0
Again
    ADD R5 R5 #-2
    BRn Skip
    ADD R0 R0 #1
    BR Again
Skip
    ADD R5 R0 #0
    RET
Binary    .STRINGZ "b0001001000110100"
Max    .FILL #32768
Zr    .FILL x030
On    .FILL X031
    .END

Write a "bit pattern" output routine that prints to the console the value stored in a register (say R2) as a 16-bit binary number. So if R2 contains the value x1234, then the o/p would be just: b0001 0010 0011 0100 i.e. the ascii symbols for ‘b’ followed by the appropriate sequence of 1's and 0's (the space separating groups of 4 bits is optional, but the initial ‘b’ is required). For this exercise, you can have the program itself set up the register with the value to be output: LD R2, number ;set up R2 with stored value ... HALT number .FILL x1234 ;hard coded value

    .ORIG x3000
    LD R2 Number
    LD R3 Max
Start
    NOT R0 R3
    ADD R0 R0 #1
    ADD R0 R2 R0
    ADD R6 R0 #0
    BRn Zero

    ADD R3 R3 #0
    BRz Done

    LD R0 OneASCII
    TRAP x21
    ADD R2 R6 #0
    JSR DivBy2
    BR Start
Zero
    LD R0 ZeroASCII
    TRAP x21
    JSR DivBy2
    BR Start
Done
    HALT
DivBy2
    AND R0 R0 #0
    ADD R4 R3 #0
Again
    ADD R4 R4 #-2
    BRn Skip
    ADD R0 R0 #1
    BR Again
Skip
    ADD R3 R0 #0
    RET
Number    .FILL #64
Max    .FILL #32768
ZeroASCII .FILL x30
OneASCII .FILL X31
    .END

use the stack to build a single-digit RPN (Reverse Polish Notation) multiplier: this is a very useful method of calculation in which the operands are pushed onto the stack first, followed by the operation to be performed on them. So 12 + 17 would be entered as (12 17 +), and return 29 to the top of the stack. Start by asking the user to input two single-digit numbers separated by cr/lf (ASCII x0A), convert the ASCII input to binary, and push the two numbers onto the stack. Then call the subroutine MULTIPLY, which pops the two numbers off the stack, multiplies them, and pushes the result back onto the stack. Finally, after returning from the subroutine, pop the result off the stack, convert it to ASCII & output it to the monitor (remember that the result may be a 2 digit decimal number: you will need to use the conversion routine built in previous exercises & assignments).

    .ORIG x3000
    LD R2 StackSize
    LEA R3 Stack
    ADD R3 R3 #2
    LD R1 Minus48
    AND R4 R4 #0    ; will contain ans
Input
    TRAP x23
    ADD R0 R0 R1
    STR R0 R3 #0
    ADD R3 R3 #-1
    ADD R2 R2 #-1
    BRp Input

    LEA R3 Stack
    LDR R5 R3 #0    ; read operator
    LDR R2 R3 #1    ; reload n2 in R2
    LDR R3 R3 #2    ; reload n1 in R3

    LD R1 Plus
    ADD R1 R5 #5
    BRz Addition

    LD R1 Minus
    ADD R1 R5 #3
    BRz Subtraction

    LD R1 Mul
    ADD R1 R5 #6
    BRz Multiply

    LD R1 Div
    ADD R1 R5 #1   
    BRz Divide

    LEA R0 ErrMsg
    TRAP x22
    HALT

Print
    AND R3 R3 #0
PrintLoop
    ADD R4 R4 #-10
    BRn Skip
    ADD R3 R3 #1
    BR PrintLoop
Skip
    ADD R0 R3 #0
    LD R1 Plus48
    ADD R0 R0 R1
    TRAP x21
    ADD R4 R4 #10
    ADD R0 R4 #0
    LD R1 Plus48
    ADD R0 R0 R1
    TRAP x21
; printing done    
    HALT
Multiply
    JSR PrintNL
    LEA R0 MulMsg
    TRAP x22
Loop
    ADD R3 R3 #-1
    BRn Skip2
    ADD R4 R4 R2
    BR Loop
Skip2
    BR Print
Divide
    JSR PrintNL
    LEA R0 DivMsg
    TRAP x22
    NOT R2 R2 ADD R2 R2 #1
Loop2
    ADD R3 R3 R2
    BRn Skip2
    ADD R4 R4 #1
    BR Loop2
Skip3
    BR Print
Addition
    JSR PrintNL
    LEA R0 PlusMsg
    TRAP x22
    ADD R4 R3 R2
    BR Print
Subtraction
    JSR PrintNL
    LEA R0 MinusMsg
    TRAP x22
    NOT R2 R2     ADD R2 R2 #1
    ADD R4 R3 R2
    BRn Negative
    BR Print
Negative
    LD R0 Minus
    TRAP x21
    NOT R2 R2     ADD R2 R2 #1
    NOT R3 R3     ADD R3 R3 #1
    ADD R4 R2 R3
    BR Print
PrintNL
    ADD r5 r7 #0
    AND R0 R0 #0
    ADD R0 R0 #10
    add r7 r5 #0
    RET

Stack    .BLKW #3
StackSize    .FILL #3
Plus48    .FILL #48
Minus48    .FILL #-48

Minus    .FILL #45
Plus    .FILL #768
Mul    .FILL #42
Div    .FILL #47

PlusMsg    .STRINGZ "Addition: ";
MinusMsg    .STRINGZ "Subtraction: ";
MulMsg    .STRINGZ "Multiplication: ";
DivMsg    .STRINGZ "Division: ";
ErrMsg    .STRINGZ "Invalid Operator"
    .END

Implement a simple 4-word stack as a subroutine with two entry points, PUSH and POP. Each data element is a single word. Make sure you test for underflow and overflow as appropriate (you may use the text book).

    .ORIG x3000
    LEA R2 Stack        ; R2 will be Top
    LD R3 Size
    ADD R2 R2 R3 

Start
    JSR PrintNL
    JSR PrintNL
    LEA R0 Choice1
    TRAP x22
    JSR PrintNL
    LEA R0 Choice2
    TRAP x22
    JSR PrintNL
    LEA R0 Choice0
    TRAP x22
    JSR PrintNL
    LEA R0 Choice
    TRAP x22
   
    TRAP x20        ; input user choice
    TRAP x21
    LD R1 Zero    NOT R1 R1    ADD R1 R1 #1
    ADD R1 R0 R1
    BRz Done

    LD R1 One    NOT R1 R1    ADD R1 R1 #1
    ADD R1 R0 R1
    BRz ChoiceOne

    LD R1 Two    NOT R1 R1    ADD R1 R1 #1
    ADD R1 R0 R1
    BRz ChoiceTwo
   
    JSR PrintNL
    LEA R0 InvldInpt
    TRAP x22
    BR Start
Done
    JSR PrintNL
    LEA R0 Greeting
    TRAP x22
    HALT

ChoiceOne
    JSR Push
    BR Start
ChoiceTwo
    JSR Pop
    BR Start
   
Push
    add r6 r7 #0
    LEA R0 Stack
    NOT R0 R0    ADD R0 R0 #1
    ADD R0 R2 R0
    BRn Overflow
    JSR PrintNL

    LEA R0 InputStr
    TRAP x22

    TRAP x20
    TRAP x21
    STR R0 R2 #0
    JSR PrintNL
    LEA R0 PushMsg
    TRAP X22
    LDR R0 R2 #0
    TRAP x21
    ADD R2 R2 #-1    ; decrement top pointer
    add r7 r6 #0
    RET
Overflow
    add r6 r7 #0
    JSR PrintNL
    LEA R0 OverflowMsg
    TRAP x22
    add r7 r6 #0
    RET
Pop
    add r6 r7 #0
    LEA R0 Stack
    ADD R0 R0 R3
    NOT R0 R0    ADD R0 R0 #1
    ADD R0 R2 R0
    BRz Underflow
   
    JSR PrintNL
    LEA R0 OutputStr
    TRAP x22
    add R2 R2 #1
    LDR R0 R2 #0
    TRAP x21
    add r7 r6 #0
    RET

Underflow
    add r6 r7 #0
    JSR PrintNL
    LEA R0 UnderflowMsg
    TRAP x22
    add r7 r6 #0
    RET
PrintNL
    add r5 r7 #0
    AND R0 R0 #0   
    ADD R0 R0 #10
    TRAP x21
    add r7 r5 #0
    RET

Zero    .FILL x30
One    .FILL x31
Two    .FILL x32

Size    .FILL #3
Stack    .BLKW #4
Choice1    .STRINGZ "1 for Push"
Choice2 .STRINGZ "2 for Pop"
Choice0 .STRINGZ "0 for Exit"
Choice  .STRINGZ "Enter Your choice: "
Greeting .STRINGZ "Thanks for Using Stack"
InputStr .STRINGZ "Input Character to Push: "
PushMsg    .STRINGZ "Pushed Character is: "
OutputStr .STRINGZ "Popped Character is: "
OverflowMsg  .STRINGZ "Overflow Occurs"
UnderflowMsg .STRINGZ "Underflow Occurs"
InvldInpt .STRINGZ "Wrong Choice, Please try again"
    .END

CPP Code to check for a Prime Number(in Assembly like structure).

#include<iostream>
using namespace std;

int main()
{
    int n,i,flag,tempn,divisor,sum,rem;
    cout<<"Enter N : ";
    cin>>n;
    divisor=2;
    flag=0;
    i=0;
    tempn=n;

//    calculating divison by 2    i=n/2    Start;

here1:
    if(tempn<=0)
        goto there1;
    tempn=tempn-divisor;
    i=i+1;
    goto here1;
there1:
    if(tempn!=0)
        i--;
//    calculating divison by 2    i=n/2    End;

here:
    divisor=i;
    if(i-2<0)
        goto there;
    sum=0;
here2:
    if(n-sum<=0)
        goto there2;
    sum=sum+divisor;
    goto here2;
there2:   
    if(sum-n!=0)
        sum=sum-divisor;
    rem=n-sum;

   
    if(rem==0)
    {
        flag=1;
        goto there;
    }
    i=i-1;
    goto here;
there:
    if(flag==1)
        cout<<"\n"<<n<<"   NA its not prime\n\n";
    else
        cout<<"\n"<<n<<"   Yes its prime\n\n";
   
    return 0;
}

LC3 Assembly Code to check for a Prime Number.

    .ORIG x3000

    LD R2,N              ; load N in R2
    AND R4,R4,#0    ; set 0 to R4
    ADD R4,R2,#0    ; add 0 to R2 and store in R4 i.e Temp-N

    LD R5,Divisor     ; load Divisor value in R5
    LD R1,Flag         ; load Flag value in R1
    LD R3,I               ; load I in R3
   
Here1:
    ADD R4,R4,#0
    BRnz There1

    NOT R0,R5
    ADD R0,R0,#1
    ADD R4,R4,R0
    ADD R3,R3,#1
    BRnzp Here1
There1:
    BRzp Here
    ADD R3,R3,#-1
Here:
    AND R5,R5,#0
    ADD R5,R3,#0
    AND R0,R0,#0
    ADD R0,R3,#-2
    BRn There
    AND R6,R6,#0    ; use R6 as Sum
Here2:
    AND R0,R0,#0
    ADD R0,R6,#0
    NOT R0,R0
    ADD R0,R0,#1
    ADD R0,R2,R0
    BRnz There2
    ADD R6,R6,R5
    BRnzp Here2
There2:
    AND R0,R0,#0
    ADD R0,R2,#0
    NOT R0,R0
    ADD R0,R0,#1
    ADD R0,R6,R0
    BRz LABEL
    AND R0,R0,#0
    ADD R0,R5,#0
    NOT R0,R0
    ADD R0,R0,#1
    ADD R6,R6,R0
LABEL:
    AND R7,R7,#0
    NOT R0,R6
    ADD R0,R0,#1
    ADD R7,R2,R0
    BRnp LABEL2
    AND R1,R1,#0
    ADD R1,R1,#1
    BRnzp There
LABEL2
    ADD R3,R3,#-1
    BRnzp Here
There:
    ADD R1,R1,#0
    BRp One
    AND R2,R2,#0    ; R2=0 means N is Prime
    BRnzp End
One:
    AND R2,R2,#0
    ADD R2,R2,#10    ; R2=10 means N is not Prime
End:
    HALT

N             .FILL #170
I               .FILL #0
Flag         .FILL #0
Divisor    .FILL #2
    .END