Determining the string length
A program that calculates the string length. We organize a loop in which every character is checked against the string terminator character 24h ($).
Program source code
title .model small .stack 100h data segment public string db "When I was a little boy, I had a dream where I was looking for my " db "father among huge pink flowers.$" output db "Length of the string: $" 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 mov cx, 0FFFFh ; Copy the number -1 into the cx register mov al, 24h ; Copy the value to search for ('$') into the al register lea di, es:[string] ; Load the address of the string variable into the es:di register repnz scasb ; Search for the '$' character in es:di while the ZF flag is not 1 not cx ; Invert all bits in the cx register dec cx ; Decrement the value by 1 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, cx ; Copy the cx register value into the dx register call PrintNumber ; Call the PrintNumber procedure (described in Example 2: Counting all vowels in a string) mov ax, 4C00h ; Copy the termination function number into ah and the value into al int 21h ; Call the interrupt main endp code ends end main
Length of the string: 97
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".