Presentation is loading. Please wait.

Presentation is loading. Please wait.

LC-3 Assembly Language Programming Examples

Similar presentations


Presentation on theme: "LC-3 Assembly Language Programming Examples"— Presentation transcript:

1 LC-3 Assembly Language Programming 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 ; Test character for match. If a match, increment count. NOT R1, R1 ADD R1, R1, R0 ; If match, R1 = xFFFF NOT R1, R1 ; If match, R1 = x0000 BRnp GETCHAR ; If no match, do not increment ADD R2, R2, #1 ; Get next character from file. GETCHAR ADD R3, R3, #1 ; Point to next character. LDR R1, R3, #0 ; R1 gets next char to test BRnzp TEST

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 R3=R0+R1, R2=0 indicates no overflow ; .ORIG x3000 AND R2, R2, #0 ;Initially R2=0 (no Overflow assumed) ADD R3, R0, R1 ;R3=R0+R1 ; 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 ;R2=1 indicating overflow NEG ADD R1, R1, #0 ;test R1 BRZP DONE ;No overflow if operand signs differ (R1 POS) ADD R3, R3, #0 ;maybe, test R3 BRN DONE ;No overflow if result sign matches (All NEG) ADD R2, R2, #1 ;R2=1 indicating overflow DONE HALT .END

7 Programming Exercise #2
Write a program to count the 1’s 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 AGAIN ADD R2, R2, #1 ;loop until done BRN LOOP HALT .END

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 LEA R0, PROMPT1 GETC ;get first character LD R6, M ;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 ADD R0, R0, R6 ;convert to hex ADD R3, R2, R0 ;R3 = first decimal number ; Get second number LEA R0, PROMPT2 ;get first character GETC ADD R0, R0, R6 ;convert char to hex GETC ;get second character 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, P ;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, P ;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, R5, R6 ;convert and print 3rd digit LD R0, NEWLINE HALT MESSAGE .STRINGZ "Enter two 2-digit decimal numbers:" NEWLINE .FILL x000A PROMPT1 .STRINGZ " The sum of " PROMPT2 .STRINGZ " and " SUM .STRINGZ " is " M FILL xFFD0 ;-x30 P FILL X ; x30 M FILL xFF9C ;-100 P FILL x ; 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


Download ppt "LC-3 Assembly Language Programming Examples"

Similar presentations


Ads by Google