Counting all vowels in a string
A program that counts the number of vowels in a given string.
Program source code
title .model small .stack 100h data segment public string db "Intel 8086. Microsoft Macro Assembler" vowels db "aeiouAEIOU" output db "Number of vowels: $" result dw 0 data ends code segment public assume ds:data, cs:code main proc mov ax, seg data ; Get the data segment address and store it in ax mov ds, ax ; Pass the data segment address from ax to ds mov es, ax ; Pass the data segment address from ax to es lea di, es:[vowels] ; Load the address of the vowels array into the es:di register lea si, ds:[string] ; Load the address of the string array into the ds:si register mov cx, 37 ; Copy the value 37 into the cx register (the length of the string) OuterLoop: push cx ; Save the cx register value on the stack mov cx, 10 ; Copy the value 10 into the cx register (the number of vowels) lodsb ; Load a byte from the si source register into the al register push di ; Save the di register value on the stack repnz scasb ; Scan the byte in the al register against the bytes at di while CX≠0 jne Skip ; If ZF=0, jump to the Skip label inc result ; Increment the result variable value Skip: pop di ; Pop the value from the stack into the di register pop cx ; Pop the value from the stack into the cx register loop OuterLoop ; Loop while CX≠0 mov ah, 09h ; Copy the string output function number into ah lea dx, ds:[output] ; Load the address of the output variable into the ds:dx register int 21h ; Call the interrupt mov dx, result ; Copy the value of the result variable into the dx register call PrintNumber ; Call the PrintNumber procedure mov ax, 4C00h ; Copy the termination function number into ah and the value into al int 21h ; Call the interrupt main endp PrintNumber proc mov ax, dx ; Copy the value of the dx register into the ax register mov bx, 10 ; Copy the number 10 into the bx register cwd ; Sign-extend the dx register div bx ; Divide the value in the ax register by bx (10) mov cl, 8 ; Copy the number 8 into the cl register shl ax, cl ; Perform a logical shift left by the count in the cl register xchg al, dl ; Exchange the contents of the al and dl registers or ax, 3030h ; Perform a logical OR of ax with 3030h xchg ax, bx ; Exchange the contents of the ax and bx registers mov ah, 02h ; Copy the console output function number into ah cmp bh, 30h ; Compare the value in the bh register with 30h je PrintOneDigit ; If ZF=1, then jump to the label PrintOneDigit mov dl, bh ; Copy the value of the bh register into the dl register int 21h ; Call the interrupt PrintOneDigit: mov dl, bl ; Copy the value of the bl register into the dl register int 21h ; Call the interrupt ret ; Return from the procedure PrintNumber endp code ends end main
Number of vowels: 10
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".