LC-3 Assembly Language Programming Examples - PowerPoint PPT Presentation

About This Presentation
Title:

LC-3 Assembly Language Programming Examples

Description:

TEST ADD R4, R1, #-4 ; Test for EOT (ASCII x04) BRz OUTPUT ; If done, prepare the output ; ... LD R6, P30 ;convert to ascii & print. ADD R0, R0, R6. OUT ... – PowerPoint PPT presentation

Number of Views:3116
Avg rating:3.0/5.0
Slides: 14
Provided by: larry109
Category:

less

Transcript and Presenter's Notes

Title: LC-3 Assembly Language Programming Examples


1
LC-3Assembly LanguageProgramming Examples
2
Sample Program
  • Count the occurrences of a character in a file.

3
Count the occurrences of a character in a file (1
0f 2).
  • Program to count occurrences of a character in
    a file.
  • Character to be input from the keyboard.
  • Result to be displayed on the monitor.
  • Program only works if no more than 9
    occurrences are found.
  • Initialization
  • .ORIG x3000
  • AND R2, R2, 0 R2 is counter, initially 0
  • LD R3, PTR R3 is pointer to character file
  • GETC R0 gets input character
  • LDR R1, R3, 0 R1 gets first character from
    file
  • Test character for end of file
  • TEST ADD R4, R1, -4 Test for EOT (ASCII x04)
  • BRz OUTPUT If done, prepare the output

4
Count the occurrences of a character in a file (2
of 2).
  • Output the count.
  • OUTPUT LD R0, ASCII Load the ASCII template
  • ADD R0, R0, R2 Covert binary count to ASCII
  • OUT ASCII code in R0 is displayed.
  • HALT Halt machine
  • Storage for pointer and ASCII template
  • ASCII .FILL x0030 ASCII offset
  • PTR .FILL x4000 PTR to character file
  • .END

5
  • Programming Exercise 1
  • Write a program to add the contents of R0 and R1,
    and indicate in R2 if there
  • was an overflow
  • Flow Diagram
  • Assembly Code

6
Programming Exercise 1
  • Add R3R0R1, R20 indicates no overflow
  • .ORIG x3000
  • AND R2, R2, 0 Initially R20 (no Overflow
    assumed)
  • ADD R3, R0, R1 R3R0R1
  • test for overflow
  • ADD R0, R0, 0 test R0
  • BRN NEG Branch if RO negative
  • ADD R1, R1, 0 test R1
  • BRN DONE No overflow if operand signs
    differ (R1 NEG)
  • ADD R3, R3, 0 maybe, test R3
  • BRZP DONE No overflow if result sign
    matches (All POS)
  • ADD R2, R2, 1 R21 indicating overflow
  • NEG ADD R1, R1, 0 test R1
  • BRZP DONE No overflow if operand signs
    differ (R1 POS)

7
  • Programming Exercise 2
  • Write a program to count the 1s in register R0
  • Flow Diagram
  • Assembly code

8
Programming Exercise 2
  • Program to count 1's in Register R0
  • R3 is a working copy of R0
  • R1 contains the count
  • R2 is a loop counter
  • .orig x3100
  • ADD R3, R0, 0 copy R0 into R3
  • AND R1, R1, 0 clear count
  • ADD R3, R3, 0 test for Neg
  • BRZP NEXT count if Neg
  • ADD R1, R1, 1
  • NEXT AND R2, R2, 0 check remaining 15 bits
  • ADD R2, R2, -15
  • LOOP ADD R3, R3, R3 shift R3 left
  • BRZP AGAIN count if Neg
  • ADD R1, R1, 1
  • AGAIN ADD R2, R2, 1 loop until done
  • BRN LOOP

9
  • Programming Exercise 3
  • Write a program to add two, two digit numbers
    read from the console
  • Flow Diagram
  • Assembly code

10
  • Programming Exercise 3

Program to add two 2 digit decimal numbers read
from the console R1 R2 are working
registers to load 2 digit number R3 is first
number R4 is second number R5 is the
sum R6 is conversion offset .orig
x3600 LEA R0, MESSAGE print message
PUTS Get first number LD R0, NEWLINE
print PROMPT1 OUT OUT LEA
R0, PROMPT1 PUTS GETC get
first character OUT LD R6, M30
convert char to hex ADD R0, R0, R6
ADD R1, R0, R0 R1 2xR0 ADD R2, R1,
0 copy R1 into R2 ADD R2, R2, R2 R2
4xR0 ADD R2, R2, R2 R2 8xR0
ADD R2, R2, R1 R2 10xR0 GETC
get second character OUT ADD R0,
R0, R6 convert to hex ADD R3, R2, R0
R3 first decimal number Get second number
LEA R0, PROMPT2 get first character
PUTS GETC OUT ADD R0, R0, R6
convert char to hex ADD R1, R0, R0
R1 2xR0 ADD R2, R1, 0 copy R1 into
R2 ADD R2, R2, R2 R2 4xR0 ADD
R2, R2, R2 R2 8xR0 ADD R2, R2, R1
R2 10xR0 GETC get second
character OUT ADD R0, R0, R6
convert to hex ADD R4, R2, R0 R4
first decimal number
11
  • Programming Exercise 3 (2)

Add the numbers and print results ADD
R5, R4, R3 R5 R3 R4 LEA R0, SUM
prepare to print results PUTS
LD R4, P100 find 1st digit LD R3,
M100 AND R0, R0, 0 LOOP1 ADD R0, R0, 1
ADD R5, R5, R3 subtract 100 until
negative BRZP LOOP1 ADD R5, R5, R4
ADD R0, R0, -1 LD R6, P30
convert to ascii print ADD R0, R0, R6
OUT AND R0, R0, 0 find 2nd
digit LOOP2 ADD R0, R0, 1 ADD R5, R5,
-10 subtract 10 until negative BRZP
LOOP2 ADD R5, R5, 10 ADD R0, R0,
-1 LD R6, P30 convert to ascii
print ADD R0, R0, R6 OUT
ADD R0, R5, R6 convert and print 3rd digit
OUT LD R0, NEWLINE OUT
HALT MESSAGE .STRINGZ "Enter two 2-digit decimal
numbers" NEWLINE .FILL x000A PROMPT1 .STRINGZ "
The sum of " PROMPT2 .STRINGZ " and " SUM
.STRINGZ " is " M30 .FILL xFFD0 -x30 P30
.FILL X0030 x30 M100 .FILL xFF9C
-100 P100 .FILL x0064 100 .END
12
  • Programming Exercise 4
  • Write a program to read characters from the
    keyboard, echo them on the
  • console, and pack them into a file (2 characters
    per word)
  • Flow Diagram
  • Assembly code

13
HW, due 11/14/07
  • Write and test an LC-3 assembly language program
    to calculate and print all the numbers in the
    Fibonacci series that can be stored in words of
    the LC-3. The program should stop when it
    determines it has found the largest number.
  • Show
  • A snapshot of your well commented Assembly
    program in the LC-3 Editor Window with the
    Assembly response.
  • A snapshot of the Simulator Console Display
    Window with the Fibonacci numbers displayed
Write a Comment
User Comments (0)
About PowerShow.com