String length calculation in 8086 ALP

Program Description

Finding the length of a given string in 8068 Assembly Language.
The program prompts the user for an input string and displays its length.
The string array has the following structure in 8086:

0th byte->size
1st byte->length of string
from 2nd byte->actual string

We therefore use the 1st byte of the array to find the length of the string and display it,as shown in the code below.

; String length calculation in 8086 ALP

;macro for printing a string
print macro m
mov ah,09h
mov dx,offset m
int 21h

.model small

;******  Data Segment ******

msg db 10,13, "Exiting the program $"

empty db 10,13, "   $"
str1 db 25,?,25 dup('$')

len db ?
mstring db 10,13, "Enter the string: $"
mlength db 10,13, "Length is: $"

;********** Code Segment ************


        mov ax,@data
        mov ds,ax

        print mstring    
		call accept_string   ;function call to accept a string

        mov cl,str1+1        ;storing length in cl from first byte of the array
        mov bl,cl            ;copying in bl for displaying 
		print mlength
        call display1        ;printing the length

mov ah,4ch       ;exit the program
int 21h

;accept procedure

accept proc near

mov ah,01
int 21h
accept endp

display1 proc near

   mov al,bl
   mov bl,al
   and al,0f0h
   mov cl,04
   rol al,cl

   cmp al,09
   jbe number
   add al,07
number:  add al,30h
         mov dl,al
         mov ah,02
         int 21h

         mov al,bl
         and al,00fh
         cmp al,09
         jbe number2
         add al,07
number2:  add al,30h
          mov dl,al
          mov ah,02
          int 21h
display1 endp

accept_string proc near

mov ah,0ah          ;accept string from user function
mov dx,offset str1  ; store the string in memory pointed by "DX"
int 21h
accept_string endp

end start


comments powered by Disqus