Comparing two strings
A program that compares two strings.
Program source code
title .model small .stack 100h data segment public aString db "Hello students!$" aStrLen equ ($-aString) bString db "Hello children!$" bStrLen equ ($-bString) equalMsg db "Strings are equal!$" notEqualMsg db "Strings are not equal!$" lenNotEqual db "The length of the strings is not equal!$" 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 dx, bStrLen ; Copy the value of the bStrLen variable into the dx register mov cx, aStrLen ; Copy the value of the aStrLen variable into the cx register lea di, es:[bString] ; Load the address of the bString variable into the destination register es:di lea si, ds:[aString] ; Load the address of the aString variable into the source register ds:si cmp dx, cx ; Compare the lengths of both strings jne LengthNotEqual ; If ZF=0, jump to the LengthNotEqual label repz cmpsb ; Compare the byte in the si register with the byte in the di register jne NotEqual ; If ZF=0, jump to the NotEqual label lea dx, ds:[equalMsg] ; Load the address of the equalMsg variable into the ds:dx register jmp Done ; Jump unconditionally to the Done label NotEqual: lea dx, ds:[notEqualMsg] ; Load the address of the notEqualMsg variable into the ds:dx register jmp Done ; Jump unconditionally to the Done label LengthNotEqual: lea dx, ds:[lenNotEqual] ; Load the address of the lenNotEqual variable into the ds:dx register Done: mov ah, 09h ; Copy the string output function number into ah int 21h ; Call the interrupt 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
Strings are not equal!
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".