Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

3.Write the programs for string manipulations using string instruction of 8086.

a) Aim: Write a program to read a character from keyboard

data segment
Char db 1 dup(0)

data ends
code segment
assume cs:code ,ds:data

start: mov ax, data ;initialization of data segment


mov ds,ax
mov ah,01 ;‘01’ function value is for reading a
;character from keyboard
int 21h ;DOS interrupt
mov char, al ;store the ASCII value of the character read
from the keyboard to memory
mov ah,4ch
int 21h
code ends
end start

RESULT:
b). Aim: Write a program to display a character on dos prompt

data segment

char db ‘s’
data ends
code segment
assume cs:code, ds:data
start:
mov ax, data ;initialization of data segment
mov ds,ax
mov ah, 02 ;’02’ function value is for character display
mov dl, char ;character to be displayed is entered in ‘dl’
int 21h ;DOS interrupt
mov ah,4ch
int 21h ;exit to DOS mode
end start

RESULT:
c) Aim: Write a program to display a string

data segment
msg db ‘Microprocessors lab’, 0dh,0ah,24h

data ends

code segment
assume cs:code, ds:data
start: mov ax, @data ;initialize the data segment
mov ds, ax
mov ah, 09 ;’09’ function value is for displaying the
;string on the screen
lea dx, msg ;store the starting address of the string in
;dx’
int 21h ;DOS interrupt
mov ah, 4ch
int 21h ;exit to DOS mode

code ends
end start

RESULT:
d). Aim: Write a program to move a given string

data segment
src db 'Micro Processors$'
data ends
extra segment
dst db 17 DUP(0)
extra ends
code segment
assume cs:code, ds:data, es:extra
start: mov ax, data ;initialize data segment
mov ds,ax
mov ax, extra ;initialize extra segment
mov es, ax
mov si,offset src ;initialize source and destination string
;pointers
mov di, offset dst
cld ;clear direction flag to increment pointers
mov cx,17 ;number of ASCII characters
rep movsb ;repeat the process to move all the ASCII
;characters from source location to
;destination location until count is zero
mov ah,09 ; DOS interrupt to display the destination
;string on the screen
lea dx, dst
int 21h
mov ah,4ch ;exit to DOS mode
int 21h
code ends
end start

RESULT:
e) Aim: Write a program to reverse a given string

data segment
str1 db 'GNITS'
str2 db 5 DUP(0)
data ends
code segment
assume cs: code, ds: data
start: mov ax, data ;initialization of data segment
mov ds,ax
mov si,offset str1 ;initialize the memory pointers
mov di, offset str2
add di,4 ;last location of the dest. String is pointed
mov cx,5 ;number of ASCII characters to be moved
back: mov al,[si] ;get a character and store it in reverse order
mov [di],al
inc si ;update pointers
dec di
loop back ;repeat until all the characters are moved
mov ah,4ch ;exit to DOS mode
int 21h
code ends
end start

RESULT:
f) Aim: Write a program to insert a given string

data segment
str1 db 'MICROPRORS LAB'
data ends
extra segment
str2 db 20 dup(0)
extra ends
code segment
assume cs: code, ds: data, es: extra
start: mov ax, data ;initialize data segment
mov ds,ax
mov ax ,extra ;initialize extra segment
mov es, ax
mov si, offset str1 ;initialize memory pointers
mov di, offset str2
cld ;clear direction flags to increment pointers
mov cx,8 ;move first 8 characters in the same order
rep movsb
mov dl,5 ;read 5 characters from keyboard and insert
;them in the given string
l1: mov ah,01h ;DOS interrupt to read a character from
int 21h ;keyboard
stos str2
dec dl
jnz l1
mov cx,6 ;transfer the remaining 6 characters in the
;same order
rep movsb
mov ah,4ch ;exit to DOS mode
int 21h
code ends
end start

RESULT:
g). Aim: Write a program to delete a word from a given string

data segment
str1 db 'Hello Very Good Morning'
data ends
extra segment
str2 db 18 dup(0)
extra ends
code segment
assume cs: code, ds: data, es: extra
start: mov ax, data ;initialize the data segment
mov ds,ax
mov ax, extra ;initialize the extra segment
mov es, ax
mov si, offset str1 ;initialize the pointers
mov di, offset str2
cld ;clear direction flag to increment pointers
mov cx,6 ;transfer the first 6 characters
rep movsb
cld
add si,5 ;update pointer ‘si’ to point the location after
;the deletion of the given string
mov cx,12 ;transfer the remaining characters in the
;same order
rep movsb
mov ah,4ch ;exit to DOS mode
int 21h
code ends
end start

RESULT:
h). Aim: Write a program to find the length of a given string

data segment
str1 db 'Microprocessors Lab$'
strlen db,00
data ends

code segment
assume cs: code, ds: data
start: mov ax,data ;initialization of data segment
mov ds,ax
sub cl,cl ;clear ‘cl’ to store the length of a string
mov si,offset str1 ;initialize memory pointer
l1: lodsb ;get a character from the string and compare
;with the ‘$’character and increment the
;length
inc cl
cmp al,'$'
jnz l1 ;if not equal, repeat
sub cl,01 ;if equal, end of the string is detected
mov strlen, cl ;move the length of the string in memory
mov ah,4ch ;exit to DOS mode
int 21h
code ends
end start

RESULT:

You might also like