Tuesday 1 March 2016

Assembly Program to count no. of character in a string

;Assembly program to count no. of character in a stringorg 100h 
;code section
mov cl, 0               ;clear counter
mov bx, offset string   ;load address of string

repeat:                
mov al, [bx]            ;load first byte of string
cmp al, '$'             ;check is it end of string
je endit                ;if yes then goto endit label
inc bx                  ;else increase bx by 1
inc cl                  ;count this byte as string character
jmp repeat              ;goto repeat label to check next byte

endit:
add cl, '0'             ;convert byte to ASCII code
mov offset len, cl      ;save in memory

mov cx, 0               ;clear counter

;print output message
mov ah, 09h            
mov dx, offset msg
int 21h
mov dx, offset len
int 21h

ret

;data section
len db '0','$' 
string db "abcdefgh",'$'  
msg db "Length is : ",'$'



No comments:

Post a Comment