length of string in alp MASM code example

Example: alp to find length of a string

.model small                                    ;defines the memory model to be used for the ALP
.data                                           ;data segment begins here
        msg1 db 10d,13d,"Enter a string: $"     ;String gets stored in msg1
        msg2 db 10d,13d,"Entered String: $"     ;String gets stored in msg2
        arr db 0Bh dup(0)                       ;array declaration & initialising with 0
        len db 00h                              ;variable initialisation with 0
        msg3 db 10d,13d,"Length: $"             ;String gets stored in msg3

.code                                           ;code segment begins here
        mov ax,@data                            ;ax now has address of data segment
        mov ds,ax                               ;ds now stores the contents of ax

        lea dx,msg1                             ;load the offset address of msg1
        mov ah,09h                              ;display the contents of dx
        int 21h                                 ;Call the Kernel
        lea si,arr                              ;load the offset address of arr
l1:     mov ah,01h                              ;read into ah
        int 21h                                 ;Call the Kernel
        cmp al,0Dh                              ;Comparing the al contents with 0DH(Enter Key)
        jz l2                                   ;jump to l2 label 
        mov [si],al                             ;copy contents of al into si
        inc si                                  ;increment si to point to next address
        inc len                                 ;increment len variable
        jmp l1

l2:     mov al,24h                              ;
        mov [si],al                             ;si now has contents of al

        lea dx,msg2                             ;load the offset address of msg2
        mov ah,09h                              ;display the contents of dx
        int 21h                                 ;Call the Kernel
        lea dx,arr                              ;load the offset address of arr
        mov ah,09h                              ;display the contents of dx
        int 21h                                 ;Call the Kernel
        lea dx,msg3                             ;load the offset address of msg3
        mov ah,09h                              ;display the contents of dx
        int 21h                                 ;Call the Kernel

        cmp len,09h                             ;compare the len with 09H
        jbe l3                                  ;jump to label l3
        add len,07h                             ;add 07H to len

l3:     add len,30h                             ;add 30H to len
        mov dl,len                              ;copy the contents of len into dl
        mov ah,02h                              ;display contents of dx
        int 21h                                 ;Call the Kernel

        mov ah,4Ch                              ;to terminate the program
        int 21h                                 ;Call the Kernel
end                                             ;end label

Tags:

Misc Example