8086 assembly language programs for string operations

8086 assembly language programs for string operations

1. Length of the string
Aim:
To write an assembly language program to find the length of the given string.
Tools:
PC installed with TASM.
Program:
ASSUME CS : CODE, DS : DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AL, ’$’
MOV CX, 00H
MOV SI, OFFSET STR1
BACK : CMP AL, [SI]
JE GO
INC CL
INC SI
JMP BACK
GO : MOV LENGTH, CL
HLT
CODE ENDS
DATA SEGMENT
STR1 DB ‘STUDENT BOX OFFICE$’
LENGTH DB ?
DATA ENDS
END
Result:
Input:
STR (DS : 0000H) = STUDENT BOX OFFICE
Output:
LENGTH = 18

2. Display the string
Aim:

To write an assembly language program to display the given string.(DOS PROGRAMMING)

Software required:

TASM TURBO ASSEMBLER

Program:

ASSUME CS : CODE, DS : DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AH, 09H
MOV DX,OFFSET MSG
INT 21H
MOV AH, 4CH
INT 21H
CODE ENDS
DATA SEGMENT
MSG DB 0DH, 0AH, "WELCOME TO MICROPROCESSORS LAB", 0DH, 0AH, "$"
DATA ENDS
END
Result:

WELCOME TO MICROPROCESSORS LAB

3. Reverse the string
Aim:

To write an assembly language program to reverse the given string.

Sofware required:

TASM TURBO ASSEMBLER

Program:

ASSUME CS : CODE, DS : DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV CL, COUNT
MOV SI, OFFSET STR1
MOV DI, COUNT - 1
BACK:MOV AL, [SI]
XCHG [DI], AL
MOV [SI], AL
INC SI
DEC DI
DEC CL
JNZ BACK
HLT
CODE ENDS
DATA SEGMENT
STR1 DB ‘MPMC$’
COUNT EQU 04H
STR2 DB DUP (0)
DATA ENDS
END
Result:
Input:
STR1 (DS:0000H)  =  
Output:
STR1 (DS:0004H) =  


(a) Concatenation of two strings (b) Number of occurrences of a sub-string in the given string.

Use PUBLIC and EXTERN directive. Create .OBJ files of both the modules and link them to create an .EXE file.

  • 16 Bit TASM Code
program_1.asm
.model small
.data
        menu db 10d,13d,"        MENU"
             db 10d,"1. Concatenation"
             db 10d,"2. Substring"
             db 10d,"3. Exit"
             db 10d,"Enter your choice: $"
            
.code
;concat and substring procedures will be defined in external files
extrn concat:far               
extrn substring:far

        mov ax,@data
        mov ds,ax

main:
        lea dx,menu
        mov ah,09h      ;display menu string
        int 21h

        mov ah,01h      ;single char input
        int 21h

        cmp al,'1'
        je case1
        cmp al,'2'
        je case2
        jmp exit

case1:  call concat     ;call procedure
        jmp main
case2:  call substring  ;call procedure
        jmp main
exit:
        mov ah,4Ch
        int 21h
end                     ;end of program


program_2.asm
.model small
.data
        m1 db 10d,13d,"Enter first string:  $"
        m2 db 10d,13d,"Enter second string: $"
        m3 db 10d,13d,"Concatenated string: $"
        s1 db 20 dup('$')
        s2 db 20 dup('$')
        s3 db 40 dup('$')
        nwline db 10d,"$"
        m4 db 10d,13d,"Second String is Substring.$"
        m5 db 10d,13d,"Second String is not a Substring.$"
        m6 db 10d,13d,"No. Of Occurrence: $"
        count db 1 dup(0)

.code
        public concat,substring
;procedures scope has been made public
;accessible to other segments also

;*********Generalised 2 parameters 16 bit macro************
scall macro xx,yy
        lea dx,xx
        mov ah,yy
        int 21h
endm                    ;end of macro

;*******************CONCATENATION PROCEDURE*****************
concat proc
        mov ax,@data
        mov ds,ax
       
        scall m1,09h    ;display m1 string
        scall s1,0Ah    ;buffered s1 string input

        scall m2,09h    ;display m2 string
        scall s2,0Ah    ;buffered s2 string input

        lea si,s1
        lea di,s3
        inc si
        mov cl,[si]     ;length of s1 string

;copying entire s1 string to s3
loop1:  inc si
        mov al,[si]
        mov [di],al
        inc di
        dec cl
        jnz loop1

        lea si,s2
        inc si
        mov cl,[si]     ;length of s2 string

;copying entire s2 string to s3
loop2:  inc si
        mov al,[si]
        mov [di],al
        inc di
        dec cl
        jnz loop2

        mov al,24h      ;'$'=24h, putting $ at end of string s3
        mov [di],al

        scall nwline,09h
        scall m3,09h    ;display m3 string
        scall s3,09h    ;display concatenated string

        ret             ;return from prodecure
endp                    ;end of prodecure

;****************SUBSTRING PROCEDURE**********************
substring proc
        mov ax,@data
        mov ds,ax

        scall m1,09h    ;display m1 string
        scall s1,0Ah    ;accept s1 string
        scall m2,09h    ;display m2 string
        scall s2,0Ah    ;accept s2 string

        lea si,s1
        inc si
        mov cl,[si]     ;take length of s1 string in cl
        inc si
       
        lea di,s2
        inc di
        mov ch,[di]     ;take length of s2 string in cl
        inc di
        mov dh,ch       ;backup of ch register
       
        mov [count],0   ;initialise count with zero
       
loop3:   
        mov al,[si]
        mov bp,si       ;backup of si pointer
        cmp al,[di]
        je loop4
        inc si
       
        dec cl          ;counter for main string
        jnz loop3

        mov dl,[count]
        cmp dl,0        ;dl=0 implies no string found
        je fail
        jmp result

loop4:  
        dec ch          ;counter for sub-string
        cmp ch,0
        je success
        inc si
        inc di
        mov al,[si]
        cmp al,[di]
        je loop4        ;continue this loop till string are same
       
        jmp loop3       ;inc case of mismatch, start again

success:
        add [count],01
        lea di,s2
        add di,2        ;move di to string place
        inc bp
        mov si,bp       ;restore si from bp
        dec cl
        mov ch,dh       ;restore ch from dh
        
        jmp loop3       ;start again till main string ends
       
fail:
        scall m5,09h    ;display m5
        ret             ;return from procedure

result:
        scall m4,09h    ;display m4 string
        scall m6,09h    ;display m6 string
      
        mov dl,[count] 
        add dl,30h             
        mov ah,02h      ;display dl contents
        int 21h        
        ret             ;return from procedure
endp                    ;end of procedure
end                     ;end of Program

No comments: