Write an LC-3 assembly language program that counts the occurrences of a charact
ID: 3668295 • Letter: W
Question
Write an LC-3 assembly language program that counts the occurrences of a character in string stored in a word in memory. The address of the word is passed in register R1 and the total count is returned in the register R0 and displayed on the screen. You must document the protocols for passing parameters and returning the result to/from the subroutine and also follow the convention of saving and restoring registers in your subroutine. Your program should be well documented and in particular, begin with a series of comments specifying: your name, course #, section #, semester and other relevant information, and a brief description of what the program does. Print out a copy of the program along with the output. Submit this printout for grading.Explanation / Answer
; 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 characters
GETC ; R0 gets character input
LDR R1, R3, #0 ; R1 gets first character
;
; 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, #1 ; R1 = -R1
ADD R1, R1, R0 ; R1 == R0?
BRnp GETCHA ; If no match, do not increment
ADD R2, R2, #1
;
; Get next character from file.
;
GETCHA: ADD R3, R3, #1 ; Point to next character.
LDR R1, R3, #0 ; R1 gets next char to test
BRnzp TEST
;
; 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
PTR: .FILL x4000 .END
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.