Exp 3
Exp 3
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.
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).
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.
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.
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.
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.
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
code segment
start:
mov ax, data ; Load data segment address into AX
mov ds, ax ; Move the data segment address into DS
; Display newline
mov ah, 09h ; DOS interrupt to display a string
lea dx, nline ; Load address of newline
int 21h ; Display newline
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
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
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
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.
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.