0% found this document useful (0 votes)
5 views11 pages

Exp 3

The document contains assembly code for string manipulation operations using the 8086 microprocessor. It includes programs to move a string, reverse a string entered from the keyboard, and check if a string is a palindrome. Each program is accompanied by a detailed explanation of its functionality and the relevant code segments.

Uploaded by

allwebsites861
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views11 pages

Exp 3

The document contains assembly code for string manipulation operations using the 8086 microprocessor. It includes programs to move a string, reverse a string entered from the keyboard, and check if a string is a palindrome. Each program is accompanied by a detailed explanation of its functionality and the relevant code segments.

Uploaded by

allwebsites861
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Exp. No.

3
Aim : Programs for String manipulation operations (Using 8086).
A. MOVE THE STRING
Code:
assume cs:code, ds:data

data segment
msg db " hello everyone", "$" ; Define a message " hello everyone"
terminated with a '$' (used for printing).
nline db 10, 13, '$' ; Define newline characters (Line Feed and Carriage
Return) followed by a '$'.
msg1 db 40 dup('$') ; Define an empty buffer of 40 characters
initialized with '$'.
data ends

code segment
start:
mov ax, data ; Load the address of the data segment into AX.
mov ds, ax ; Move the content of AX (data segment address)
into DS to point to the data segment.

mov ah, 09h ; DOS interrupt function to display a string (function


09h).
lea dx, msg ; Load the effective address of the message string
into DX.
int 21h ; Call DOS interrupt 21h to print the string " hello
everyone".

lea si, msg ; Load the address of the original message into SI.
lea di, msg1 ; Load the address of the destination buffer (msg1)
into DI.

l1:
mov al, [si] ; Load the byte at the address pointed to by SI into
AL.
cmp al, '$' ; Compare the byte in AL with '$' (end of string
marker).
je l2 ; If AL is '$', jump to label l2 (end of copy loop).
mov [di], al ; Copy the byte from AL to the location pointed to by
DI.
inc si ; Increment SI to point to the next character in the
source string.
inc di ; Increment DI to point to the next character in the
destination buffer.
jmp l1 ; Repeat the loop to copy the next character.

l2:
mov ah, 09h ; DOS interrupt function to display a string (function
09h).
lea dx, nline ; Load the effective address of the newline characters
into DX.
int 21h ; Call DOS interrupt 21h to print the newline (10, 13).

mov ah, 09h ; DOS interrupt function to display a string (function


09h).
lea dx, msg1 ; Load the effective address of the copied message
(msg1) into DX.
int 21h ; Call DOS interrupt 21h to print the copied message.

mov ah, 4Ch ; DOS interrupt function to terminate the program


(function 4Ch).
int 21h ; Terminate the program.

code ends
end start

Explanation:
Line-by-Line Explanation:
1. assume cs:code, ds:data
This tells the assembler to assume that the code segment is addressed by
CS and the data segment by DS.
2. Data Segment:
o msg db " hello everyone", "$"
Defines a string " hello everyone" terminated with a $ (DOS uses
this as a string terminator for printing).
o nline db 10, 13, '$'
Defines a newline sequence (LF: 10, CR: 13) followed by a $, which
will be used to print a newline.
o msg1 db 40 dup('$')
Reserves a 40-character buffer filled with $ to copy the original
message into.
3. Code Segment:
o mov ax, data
Load the segment address of the data segment into the AX register.
o mov ds, ax
Set the DS register to the data segment address, so the data
segment is correctly referenced.
4. Displaying the Original Message:
o mov ah, 09h
Set AH to 09h, the DOS function number for displaying a string.
o lea dx, msg
Load the address of the string msg into the DX register.
o int 21h
Call DOS interrupt 21h to print the string in msg.
5. Copying the Message to msg1:
o lea si, msg
Load the address of the original message (msg) into SI.
o lea di, msg1
Load the address of the destination buffer (msg1) into DI.
6. Copy Loop (l1):
o mov al, [si]
Move the character at the memory location pointed to by SI into AL.
o cmp al, '$'
Compare AL with $, which is the end-of-string marker.
o je l2
If AL is $, jump to label l2 to end the loop.
o mov [di], al
Copy the value in AL to the memory location pointed to by DI.
o inc si
Increment SI to point to the next character in the original message.
o inc di
Increment DI to point to the next position in the destination buffer
(msg1).
o jmp l1
Jump back to the start of the loop (l1) to repeat the copying
process.
7. End of Copying and Displaying a Newline:
o mov ah, 09h
Set AH to 09h to prepare for displaying another string.
o lea dx, nline
Load the address of the newline sequence into DX.
o int 21h
Call DOS interrupt 21h to print the newline (LF and CR).
8. Displaying the Copied Message:
o mov ah, 09h
Set AH to 09h again for the string display function.
o lea dx, msg1
Load the address of the copied message (msg1) into DX.
o int 21h
Call DOS interrupt 21h to print the contents of msg1.
9. Program Termination:
o mov ah, 4Ch
Set AH to 4Ch, which is the DOS function to terminate the program.
o int 21h
Call DOS interrupt 21h to exit the program and return control to the
operating system.
Summary:
 This program copies the string " hello everyone" from msg to msg1 and
then displays both the original string and the copied string with a newline
in between.
 The $ is used to mark the end of strings for the DOS interrupt 21h service
that handles string output.
B.REVERSE THE STRING READ FROM THE KEYBOARD AND DISPLAY

Code:
assume cs:code, ds:data

data segment
msg db " enter the string : ", "$" ; Prompt message to enter a string.
str1 label byte
strmax db 20 ; Maximum number of characters that can be
entered.
stract db ? ; Actual number of characters entered by the
user.
strfld db 10 dup('$') ; Buffer for storing the input string (10
characters).

nline db 10, 13, '$' ; Newline sequence (LF + CR) for formatting
output.

rev db 10 dup('$') ; Buffer to store the reversed string (10


characters).

msg1 db " the reversed string is : ", "$" ; Message to display the reversed
string.
data ends

code segment
start:
mov ax, data ; Load the data segment address into AX.
mov ds, ax ; Move the data segment address into DS.

mov ah, 09h ; DOS interrupt to display a string (function


09h).
lea dx, msg ; Load the address of the prompt message into
DX.
int 21h ; Call DOS interrupt 21h to display the message.

mov ah, 0Ah ; DOS interrupt to take input (function 0Ah).


lea dx, str1 ; Load the address of the input buffer into DX.
int 21h ; Call DOS interrupt 21h to accept user input.
mov ah, 09h ; DOS interrupt to display a string (function
09h).
lea dx, nline ; Load the address of the newline into DX.
int 21h ; Print a newline for formatting.

lea si, strfld ; Load the address of the input string into SI.
lea di, rev ; Load the address of the reverse string buffer
into DI.

xor cx, cx ; Clear CX (used as the counter register).


mov cl, stract ; Load the actual length of the input string into
CL (from stract).

add si, cx ; Move SI to the end of the input string.


dec si ; Adjust SI to point to the last character of the
string.

top:
mov al, [si] ; Move the character from [SI] to AL.
mov [di], al ; Store the character in the reverse buffer.
inc di ; Increment DI to point to the next position in the
reverse buffer.
dec si ; Decrement SI to move to the previous character
in the original string.
loop top ; Repeat the loop until all characters are
processed.

mov ah, 09h ; DOS interrupt to display a string (function


09h).
lea dx, nline ; Load the address of the newline sequence into
DX.
int 21h ; Print the newline.

mov ah, 09h ; Prepare to display the message about the


reversed string.
lea dx, msg1 ; Load the address of the message ("the
reversed string is: ") into DX.
int 21h ; Call DOS interrupt to display the message.

mov ah, 09h ; Prepare to display the reversed string.


lea dx, rev ; Load the address of the reversed string buffer
into DX.
int 21h ; Call DOS interrupt to display the reversed
string.

exit:
mov ah, 4Ch ; DOS interrupt to terminate the program
(function 4Ch).
int 21h ; Exit the program.
code ends

end start
3. CHECK WHETHER THE GIVEN STRING IS PALINDROME
Code:
assume cs:code, ds:data

data segment
msg db "enter the string", "$" ; Message to prompt user input

str1 label byte


strmax db 20 ; Maximum length of input string
stract db ? ; Actual length of input string
strfld db 10 dup('$') ; Buffer for the input string

nline db 10, 13, '$' ; Newline (LF + CR) for formatting

rev db 10 dup('$') ; Buffer to store the reversed string


msg1 db "the entered string is palindrome", "$" ; Message if the string is
a palindrome
msg2 db "the entered string is not palindrome", "$" ; Message if the string
is not a palindrome
data ends

code segment
start:
mov ax, data ; Load data segment address into AX
mov ds, ax ; Move the data segment address into DS

; Display message to enter the string


mov ah, 09h ; DOS interrupt to display a string
lea dx, msg ; Load address of the prompt message
int 21h ; Display message

; Newline for formatting


mov ah, 09h ; DOS interrupt to display a string
lea dx, nline ; Load address of newline
int 21h ; Display newline

; Take input from user


mov ah, 0Ah ; DOS interrupt to take input
lea dx, str1 ; Load address of input buffer
int 21h ; Take string input

; Display newline
mov ah, 09h ; DOS interrupt to display a string
lea dx, nline ; Load address of newline
int 21h ; Display newline

; Display the entered string


mov ah, 09h ; DOS interrupt to display a string
lea dx, strfld ; Load address of the input string
int 21h ; Display the entered string

; Reverse the input string


lea si, strfld ; Load input string address into SI
lea di, rev ; Load reverse string buffer address into DI
xor cx, cx ; Clear CX
mov cl, stract ; Load the actual length of the input string

add si, cx ; Move SI to the end of the input string


dec si ; Adjust SI to point to the last character

reverse_loop:
mov al, [si] ; Move character from SI to AL
mov [di], al ; Store character in reverse buffer
inc di ; Increment DI to the next position
dec si ; Decrement SI to the previous position
loop reverse_loop ; Loop until all characters are reversed

; Display the reversed string


mov ah, 09h ; DOS interrupt to display a string
lea dx, nline ; Load address of newline
int 21h ; Display newline

mov ah, 09h ; DOS interrupt to display a string


lea dx, rev ; Load reversed string address
int 21h ; Display reversed string

; Compare original and reversed string to check for palindrome


lea si, strfld ; Load input string address into SI
lea di, rev ; Load reverse string address into DI
xor cx, cx ; Clear CX
mov cl, stract ; Load the actual length of the input string

compare_loop:
mov al, [si] ; Load character from original string
cmp al, [di] ; Compare it with the corresponding character
from reversed string
jne not_palindrome ; If not equal, jump to not_palindrome
inc si ; Move to next character in original string
inc di ; Move to next character in reversed string
loop compare_loop ; Repeat for all characters

; If palindrome, display palindrome message


mov ah, 09h ; DOS interrupt to display a string
lea dx, nline ; Load address of newline
int 21h ; Display newline

mov ah, 09h ; DOS interrupt to display a string


lea dx, msg1 ; Load palindrome message address
int 21h ; Display palindrome message
jmp exit ; Jump to exit

not_palindrome:
; If not palindrome, display not palindrome message
mov ah, 09h ; DOS interrupt to display a string
lea dx, nline ; Load address of newline
int 21h ; Display newline

mov ah, 09h ; DOS interrupt to display a string


lea dx, msg2 ; Load not palindrome message address
int 21h ; Display not palindrome message

exit:
mov ah, 4Ch ; DOS interrupt to terminate the program
int 21h ; Exit program
code ends

end start

Explanation:
1. Data Segment:
o msg: Holds the prompt message for entering the string.

o strfld: Stores the string entered by the user.

o rev: Stores the reversed version of the input string.

o msg1: Message displayed when the string is a palindrome.

o msg2: Message displayed when the string is not a palindrome.

2. Code Segment:
o The program starts by prompting the user to enter a string, then
displays the string.
o It reverses the string and compares the original with the reversed
version to determine if it's a palindrome.
o Depending on the result, it displays whether the string is a
palindrome or not, then exits.
This program checks if a user-input string is a palindrome by reversing it and
comparing the reversed string with the original.

You might also like