Character count in a string
The program counts the occurrences of dots, commas, spaces and capital letters in a given string.
Program source code
title .model small .stack 100h data segment public string db "Hi. My name is Ann, and I am 37.\0" strlen equ ($-string) - 2 ; Compute the array length output db "Number of dots: %.4d\n" db "Number of commas: %.2d\n" db "Number of spaces: %.1d\n" db "Number of capital letters: %d\0" dots dw 0 commas dw 0 spaces dw 0 uppers dw 0 data ends code segment public ; Start of the code segment assume ds:data, cs:code include Handyfar.inc ; Include the file with functions main proc ; Start of the main procedure call SetDataSegment ; Call the SetDataSegment procedure lea si, ds:[string] ; Load the address of the string variable into the si register mov cx, strlen ; Set the counter equal to the string length CountSymbolsLoop: lodsb ; Load a byte from the si source register into the al register cmp al, 2Eh ; Compare the value in the al register with the character . jz IncrementDots ; If ZF=1, then jump to the IncrementDots label cmp al, 2Ch ; Compare the value in the al register with the character , jz IncrementCommas ; If ZF=1, then jump to the IncrementCommas label cmp al, 20h ; Compare the value in the al register with the character ' ' jz IncrementSpaces ; If ZF=1, then jump to the IncrementSpaces label jmp CheckUpperCase ; Jump unconditionally to the CheckUpperCase label IncrementDots: inc dots ; Increment the dots variable value jmp Next ; Jump unconditionally to the Next label IncrementCommas: inc commas ; Increment the commas variable value jmp Next ; Jump unconditionally to the Next label IncrementSpaces: inc spaces ; Increment the spaces variable value jmp Next ; Jump unconditionally to the Next label CheckUpperCase: cmp al, 0C0h ; Compare the value in the al register with the character A jae CheckHigher ; If the character is not below A, then jump to the CheckHigher label jmp Next ; Jump unconditionally to the Next label CheckHigher: cmp al, 0DFh ; Compare the value in the al register with the character Z jbe IncrementUppers ; If the character is not above Z, then jump to the IncrementUppers label jmp Next ; Jump unconditionally to the Next label IncrementUppers: inc uppers ; Increment the uppers variable value Next: loop CountSymbolsLoop ; Loop while CX≠0 push uppers ; 5th: Save the value of the uppers variable on the stack push spaces ; 4th: Save the value of the spaces variable on the stack push commas ; 3rd: Save the value of the commas variable on the stack push dots ; 2nd: Save the value of the dots variable on the stack lea ax, ds:[output] ; 1st: Load the address of the output format string into the ax register call PrintFormatString ; Call the PrintFormatString procedure add sp, 8 ; Align the stack xor al, al ; Clear the al register call ExitProcess ; Call the ExitProcess procedure main endp ; End of the main procedure code ends ; End of the code segment end main ; End of file. The main procedure is the entry point
Number of dots: 2
Number of commas: 1
Number of spaces: 8
Number of capital letters: 4
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".