; File: countChars.asm ; Written by: Deepak Kumar ; Date: March 20, 2024 ; Purpose: ; Program to count the occurrences of a character in a string. ; Program inputs the character to count from keyboard. ; The string is provided starting from memory location x4000 (EOT terminated). ; e.g. if the string is "BRYN MAWR" ; and theinput char is 'R' ; the output displayed will be 2 ; ; Assume the count will always be less than 10 (why??) ; ; Algorithm: ; count <- 0 ; c <- input character ; nextCH <- first character in string ; while nextCH != NULL do ; if nextCH = NULL then ; count <- count + 1 ; nextCH <- next character in string ; output count ; ; Registers used: ; R0: c ; R1: nextCH ; R2: count ; R3: address of string ; .ORIG x3000 START LD R3, PTR ; Address of string AND R2, R2, #0 ; R2 = 0 (count) TRAP x23 ; R0 = input char LDR R1, R3, #0 ; R1 = next char ; ; WHILE R1 is not NULL DO LOOP BRz OUTPUT ; next char is null ; ; IF char in R1 = R0 NOT R1, R1 ADD R1, R1, #1 ; R1 = -R1 ADD R1, R1, R0 ; R1 = R0 - R1 BRnp NEXTCH ; THEN ADD R2, R2, #1 ; R2 = R2 + 1 (incr count) ; NEXTCH ADD R3, R3, #1 ; incr char pointer LDR R1, R3, #0 ; R1 = next char BR LOOP ; ; Output count OUTPUT LD R0, ASCII ; Load ASCII template in R0 ADD R0, R0, R2 ; Add count to R0 (ASCII count) TRAP x21 ; Output count in ASCII END HALT ; ; STORAGE ASCII .FILL x0030 PTR .FILL x4000 .END .ORIG x4000 MESG .STRINGZ "BRYN MAWR" .END