LC3 Assembly Language Programming Examples Sample Program Count
LC-3 Assembly Language Programming Examples
Sample Program • Count the occurrences of a character in a file.
Count the occurrences of a character in a file (1 0 f 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 AND LD GETC LDR x 3000 R 2, #0 ; R 2 is counter, initially 0 R 3, PTR ; R 3 is pointer to character file ; R 0 gets input character R 1, R 3, #0 ; R 1 gets first character from file ; ; Test character for end of file ; TEST ADD R 4, R 1, #-4 ; Test for EOT (ASCII x 04) BRz OUTPUT ; If done, prepare the output ; ; Test character for match. If a match, increment count. ; NOT R 1, R 1 ADD R 1, R 0 ; If match, R 1 = x. FFFF NOT R 1, R 1 ; If match, R 1 = x 0000 BRnp GETCHAR ; If no match, do not increment ADD R 2, #1 ; ; Get next character from file. ; GETCHAR ADD R 3, #1 ; Point to next character. LDR R 1, R 3, #0 ; R 1 gets next char to test BRnzp TEST
Count the occurrences of a character in a file (2 of 2). ; ; Output the count. ; OUTPUT LD R 0, ASCII ; Load the ASCII template ADD R 0, R 2 ; Covert binary count to ASCII OUT ; ASCII code in R 0 is displayed. HALT ; Halt machine ; ; Storage for pointer and ASCII template ; ASCII. FILL x 0030 ; ASCII offset PTR. FILL x 4000 ; PTR to character file. END
Programming Exercise #1 Write a program to add the contents of R 0 and R 1, and indicate in R 2 if there was an overflow • • Flow Diagram Assembly Code
Programming Exercise #1 ; ; Add R 3=R 0+R 1, R 2=0 indicates no overflow. ORIG x 3000 AND R 2, #0 ADD R 3, R 0, R 1 ; Initially R 2=0 (no Overflow assumed) ; R 3=R 0+R 1 ; test for overflow ADD R 0, #0 BRN NEG ADD R 1, #0 BRN DONE ; test R 0 ; Branch if RO negative ; test R 1 ; No overflow if operand signs differ (R 1 NEG) ADD R 3, #0 BRZP DONE ADD R 2, #1 ; maybe, test R 3 ; No overflow if result sign matches (All POS) ; R 2=1 indicating overflow ADD R 1, #0 BRZP DONE ; test R 1 ; No overflow if operand signs differ (R 1 POS) ADD R 3, #0 BRN DONE ADD R 2, #1 ; maybe, test R 3 ; No overflow if result sign matches (All NEG) ; R 2=1 indicating overflow NEG DONE HALT. END
Programming Exercise #2 Write a program to count the 1’s in register R 0 • • Flow Diagram Assembly code
Programming Exercise #2 ; Program to count 1's in Register R 0 ; R 3 is a working copy of R 0 ; R 1 contains the count ; R 2 is a loop counter. orig x 3100 ADD R 3, R 0, #0 ; copy R 0 into R 3 AND R 1, #0 ; clear count NEXT LOOP AGAIN ADD R 3, #0 BRZP NEXT ADD R 1, #1 ; test for Neg ; count if Neg AND R 2, ADD R 3, BRZP AGAIN ADD R 1, ADD R 2, BRN LOOP ; check remaining 15 bits HALT. END #0 #-15 R 3 #1 #1 ; shift R 3 left ; count if Neg ; loop until done
Programming Exercise #3 Write a program to add two, two digit numbers read from the console • • Flow Diagram Assembly code
Programming Exercise #3 ; Program to add two 2 digit decimal numbers read from the console ; R 1 & R 2 are working registers to load 2 digit number ; R 3 is first number ; R 4 is second number ; R 5 is the sum ; R 6 is conversion offset. orig x 3600 LEA R 0, MESSAGE ; print message PUTS ; Get first number LD R 0, NEWLINE OUT LEA R 0, PROMPT 1 PUTS GETC OUT LD R 6, ADD R 0, ADD R 1, ADD R 2, ; print PROMPT 1 ; get first character M 30 R 0, R 1, R 2, ; convert char to hex R 6 R 0 #0 R 2 R 1 ; R 1 = ; copy ; R 2 = 2 x. R 0 R 1 into R 2 4 x. R 0 8 x. R 0 10 x. R 0 GETC ; get second character OUT ADD R 0, R 6 ; convert to hex ADD R 3, R 2, R 0 ; R 3 = first decimal number ; Get second number LEA R 0, PROMPT 2 PUTS GETC OUT ADD R 0, R 6 ADD R 1, R 0 ADD R 2, R 1, #0 ADD R 2, R 2 ADD R 2, R 1 GETC OUT ADD R 0, R 6 ADD R 4, R 2, R 0 ; get first character ; convert char to hex ; R 1 = 2 x. R 0 ; copy R 1 into R 2 ; R 2 = 4 x. R 0 ; R 2 = 8 x. R 0 ; R 2 = 10 x. R 0 ; get second character ; convert to hex ; R 4 = first decimal number
Programming Exercise #3 (2) ; Add the numbers and print results ADD R 5, R 4, R 3 ; R 5 = R 3 + R 4 LEA R 0, SUM ; prepare to print results PUTS LOOP 1 LOOP 2 LD R 4, P 100 LD R 3, M 100 AND R 0, #0 ADD R 0, #1 ADD R 5, R 3 BRZP LOOP 1 ADD R 5, R 4 ADD R 0, #-1 LD R 6, P 30 ADD R 0, R 6 OUT AND R 0, ADD R 5, BRZP LOOP 2 ADD R 5, ADD R 0, LD R 6, P 30 ADD R 0, OUT ; find 1 st digit ; subtract 100 until negative ; convert to ascii & print #0 ; find 2 nd digit #1 #-10 ; subtract 10 until negative #10 #-1 ; convert to ascii & print R 6 ADD R 0, R 5, R 6 OUT LD R 0, NEWLINE OUT ; convert and print 3 rd digit HALT MESSAGE NEWLINE PROMPT 1 PROMPT 2 SUM M 30 P 30 M 100 P 100 . STRINGZ "Enter two 2 -digit decimal numbers: ". FILL x 000 A. STRINGZ " The sum of ". STRINGZ " and ". STRINGZ " is ". FILL x. FFD 0 ; -x 30. FILL X 0030 ; x 30. FILL x. FF 9 C ; -100. FILL x 0064 ; 100. END
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
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
- Slides: 13