1. Write an ALP to search a character in a string.
2.Write an ALP to given string is palindrome or not.
1.Write an ALP to search a character in a string.
CODE:
data SEGMENT
MSG1 DB 10,13,'ENTER THE STRING:$' ; Message to prompt for string input
MSG2 DB 10,13,'Exists$' ; Message to indicate the character exists
MSG3 DB 10,13,'Does not exist$' ; Message to indicate the character does not exist
MSG4 DB 10,13,'ENTER THE Character:$' ; Message to prompt for character input
STR1 DB 50 DUP(0) ; String input buffer, 50 bytes initialized to 0
data ENDS
code SEGMENT
ASSUME CS:code, DS:data
START:
MOV AX, data
MOV DS, AX ; Initialize data segment
LEA DX, MSG1
MOV AH, 09H
INT 21H ; Display "ENTER THE STRING:"
LEA DI, STR1 ; Point DI to the string buffer
MOV AH, 01H ; Prepare to read a character
NEXT_CHAR:
INT 21H ; Read a character
CMP AL, 0DH ; Check if the character is Carriage Return (Enter key)
JE TERMINATE ; If Enter, terminate input
MOV [DI], AL ; Store the character in the buffer
INC DI ; Increment DI to next position
JMP NEXT_CHAR ; Continue reading characters
TERMINATE:
MOV AL, '$' ; Terminate the string with a '$'
MOV [DI], AL
LEA DX, MSG4
MOV AH, 09H
INT 21H ; Display "ENTER THE Character:"
MOV AH, 01H
INT 21H ; Read the character to search for
MOV BL, AL ; Store the character in BL
LEA SI, STR1 ; Point SI to the start of the string
MOV CX, 50 ; Set CX to the maximum string length (50)
CHECK_CHAR:
LODSB ; Load byte from string into AL and increment SI
CMP AL, '$' ; Check if end of string
JE NOTEXISTS ; If end of string, character not found
CMP AL, BL ; Compare string character with the search character
JE EXISTS ; If found, jump to EXISTS
LOOP CHECK_CHAR ; Repeat for the length of the string
NOTEXISTS:
LEA DX, MSG3
MOV AH, 09H
INT 21H ; Display "Does not exist"
JMP END_PROGRAM
EXISTS:
LEA DX, MSG2
MOV AH, 09H
INT 21H ; Display "Exists"
END_PROGRAM:
MOV AH, 4CH
INT 21H ; Terminate program
code ENDS
END START
OUTPUT:
EXPLAINATION:
I'd be glad to explain the assembly code you provided:
Data Segment:
data SEGMENT: This line defines a segment in memory named "data". Segments are used to organize different parts of a program.- Message Variables (
MSG1,MSG2,MSG3,MSG4):DB(Define Byte): These lines define byte arrays to store string messages displayed to the user.10, 13: These are the ASCII codes for newline (\n) and carriage return (\r), respectively, used for formatting the output.- The messages prompt the user for input and indicate the search results.
STR1 DB 50 DUP(0): This line defines a byte array namedSTR1with 50 bytes (enough to hold up to 49 characters plus the null terminator).DUP(0)initializes all bytes to 0, creating an empty string buffer.
Code Segment:
code SEGMENT: This line defines the code segment where the program instructions reside.ASSUME CS:code, DS:data: These lines inform the assembler about the segment registers used for code (CS) and data (DS).START:: This label marks the beginning of the program's execution.MOV AX, data: This instruction loads the address of the "data" segment into theAXregister.MOV DS, AX: This instruction sets theDSregister (data segment) to the value inAX, initializing the data segment for accessing variables.- Displaying "ENTER THE STRING:" (Lines starting with
LEA DXand ending withINT 21H):LEA DX, MSG1**: This instruction loads the address of theMSG1message into theDXregister for displaying.MOV AH, 09H**: This sets theAHregister (interrupt service routine function number) to 9, which is the DOS function for displaying a string.INT 21H**: This instruction triggers an interrupt (a signal to the operating system) with number 21H, invoking the DOS function to display the string.
Reading Input String:
LEA DI, STR1: This instruction loads the address ofSTR1into theDIregister, pointing it to the start of the string buffer.MOV AH, 01H: This setsAHto 1, which is the DOS function number for reading a character from the keyboard.NEXT_CHAR: This label marks the loop for reading characters.INT 21H: This interrupts the CPU to read a character.CMP AL, 0DH: This instruction compares the character inAL(register holding the read character) with the ASCII code for\r(carriage return).JE TERMINATE: IfALis equal to 0DH (Enter key pressed), jump to theTERMINATElabel to end input.MOV [DI], AL: This instruction stores the read character into the current position of the string buffer pointed to byDI.INC DI: This incrementsDIto point to the next byte in the buffer for the next character.JMP NEXT_CHAR: This instruction jumps back to theNEXT_CHARlabel, continuing the character reading loop.
Terminating String Input:
TERMINATE:: This label marks the termination point for string input.MOV AL, '$': This loads the dollar sign ($) character intoAL, which acts as the string terminator.MOV [DI], AL: This stores the$character at the end of the string buffer.
Searching for a Character:
- Displaying "ENTER THE Character:" (Repeated Lines): This part is identical to the previous message display functionality.
MOV AH, 01H: This setsAHfor reading a character to search for.INT 21H: This reads the search character from the keyboard.MOV BL, AL: This stores the read character in theBLregister for comparison.LEA SI, STR1: This loads the address ofSTR1intoSI, pointing it to the beginning of the string.MOV CX, 50: This setsCX
Searching for a Character
CHECK_CHAR:: This label marks the loop for checking characters in the string.LODSB: This instruction loads a byte from the memory location pointed to bySIintoALand incrementsSIautomatically. This effectively movesSIthrough the string while loading each character intoALfor comparison.CMP AL, '$': This compares the character inALwith the dollar sign ($), the string terminator.JE NOTEXISTS: IfALis equal to$(end of string reached), jump to theNOTEXISTSlabel, indicating the character wasn't found.CMP AL, BL: This compares the character inALwith the search character inBL.JE EXISTS: IfALis equal toBL(character found), jump to theEXISTSlabel.LOOP CHECK_CHAR: This instruction decrementsCXby 1. IfCXis not zero, it jumps back to theCHECK_CHARlabel, continuing the loop for the remaining characters in the string.
Displaying Search Results:
NOTEXISTS:: This label marks the point where the character wasn't found.LEA DX, MSG3: This loads the address of the message "Does not exist" intoDXfor display.MOV AH, 09H: This setsAHfor displaying a string.INT 21H: This calls the DOS function to display "Does not exist".JMP END_PROGRAM: This unconditional jump transfers control to the program termination part.EXISTS:: This label marks the point where the character was found.LEA DX, MSG2: This loads the address of the message "Exists" intoDXfor display.MOV AH, 09H: This setsAHfor displaying a string.INT 21H: This calls the DOS function to display "Exists".
Program Termination:
END_PROGRAM:: This label marks the program's end.MOV AH, 4CH: This setsAHto 4CH, which is the DOS function number for program termination.INT 21H: This triggers the interrupt with 21H, calling the DOS function to terminate the program.code ENDS: This marks the end of the code segment.END START: This signifies the overall program end, withSTARTbeing the starting point.
In summary, this assembly code prompts the user to enter a string and a character, then searches for the character within the string. It displays messages indicating whether the character was found or not.
2.Write an ALP to given string is palindrome or not.
code:
.model small
.stack 100h
data SEGMENT
MSG1 DB 10,13,'ENTER THE STRING: $'
MSG2 DB 10,13,'STRING IS PALINDROME$'
MSG3 DB 10,13,'STRING IS NOT PALINDROME$'
STR1 DB 50 DUP(0)
data ENDS
code SEGMENT
ASSUME CS:code, DS:data
START:
; Initialize Data Segment
MOV AX, @data
MOV DS, AX
; Display message to enter the string
LEA DX, MSG1
MOV AH, 09H
INT 21H
LEA SI,STR1
LEA DI,STR1
MOV AH,01H
NEXT:
INT 21H
CMP AL,0DH
JE TERMINATE
MOV [DI],AL
INC DI
JMP NEXT
TERMINATE:
MOV AL,'$'
MOV [DI],AL
DOTHIS:
; Compare characters from both ends
DEC DI
MOV AL, [SI]
CMP [DI],AL
JNE NOTPALINDROME
; Move pointers
INC SI
CMP SI,DI
JL DOTHIS
; If palindrome, display message
PALINDROME:
MOV AH, 09H
LEA DX, MSG2
INT 21H
JMP XX
NOTPALINDROME:
; If not palindrome, display message
MOV AH, 09H
LEA DX, MSG3
INT 21H
XX:
; Exit program
MOV AH, 4CH
INT 21H
code ENDS
END START
EXPLAINATION:
Explanation of the Assembly Code for Palindrome Checking
This assembly code checks if a user-entered string is a palindrome. Here's a breakdown of each section:
Model and Stack:
.model small: This line defines the memory model for the program.smallindicates a 16-bit memory model with limited addressing capabilities..stack 100h: This allocates 100h (256 bytes) of memory for the program's stack, used for storing temporary data during function calls.
Data Segment:
data SEGMENT: This marks the beginning of the data segment. It holds static data like messages and the string buffer.- Message variables (
MSG1,MSG2,MSG3):DB(Define Byte): These lines define byte arrays for string messages displayed to the user.10, 13: These represent the ASCII codes for newline (\n) and carriage return (\r), respectively, used for formatting the output.- The messages prompt the user for input and indicate the palindrome result.
STR1 DB 50 DUP(0): This defines a byte array namedSTR1with 50 bytes (enough to hold up to 49 characters plus the null terminator).DUP(0)initializes all bytes to 0, creating an empty string buffer.
- Message variables (
data ENDS: This marks the end of the data segment.
Code Segment:
code SEGMENT: This marks the beginning of the code segment. It holds the program instructions.ASSUME CS:code, DS:data: These lines inform the assembler about the segment registers used for code (CS) and data (DS).START:: This label marks the program's entry point.
Initialization:
MOV AX, @data: This loads the address of the "data" segment into theAXregister. The@symbol is used to get the segment address.MOV DS, AX: This sets theDSregister (data segment) to the value inAX, initializing the data segment for accessing variables.
Prompting and Reading Input:
Displaying "ENTER THE STRING:" (Lines starting with
LEA DXand ending withINT 21H):LEA DX, MSG1**: This loads the address of theMSG1message into theDXregister for displaying.MOV AH, 09H**: This setsAH(interrupt service routine function number) to 9, which is the DOS function for displaying a string.INT 21H**: This triggers an interrupt (a signal to the operating system) with number 21H, invoking the DOS function to display the string.
LEA SI, STR1: This instruction loads the address ofSTR1into theSIregister, pointing it to the start of the string buffer.LEA DI, STR1: This loads the address ofSTR1into theDIregister, pointing it to the beginning of the buffer as well.MOV AH, 01H: This setsAHto 1, which is the DOS function number for reading a character from the keyboard.NEXT: This label marks the loop for reading characters.INT 21H: This instructs the operating system to read a character.CMP AL, 0DH: This compares the character inAL(register holding the read character) with the ASCII code for\r(carriage return).JE TERMINATE: IfALis equal to 0DH (Enter key pressed), jump to theTERMINATElabel to end input.MOV [DI], AL: This stores the read character into the current position of the string buffer pointed to byDI.INC DI: This incrementsDIto point to the next byte in the buffer for the next character.JMP NEXT: This jumps back to theNEXTlabel, continuing the character reading loop.
Terminating String Input and Adding Null Terminator:
TERMINATE:: This label marks the termination point for string input.MOV AL, '$': This loads the dollar sign ($) character intoAL, which acts as the string terminator.MOV [DI], AL: This stores the$character at the end of the string buffer.
Palindrome Check:
DOTHIS:: This label marks the loop for checking if the string is a palindrome.DEC DI: This decreasesDIto point to the character just before the$(excluding the terminator).MOV AL, [SI]: This loads the character at the current position pointed to bySIinto theALregister.
CMP [DI], AL: This compares the character inALwith the character pointed to byDI.JNE NOTPALINDROME: If the characters are not equal, jump to theNOTPALINDROMElabel, indicating the string is not a palindrome.
- Moving Pointers and Checking for Midpoint:
INC SI: This incrementsSIto point to the next character towards the center of the string.CMP SI, DI: This comparesSIandDI.JL DOTHIS: IfSIis less thanDI(pointers haven't met in the middle yet), jump back toDOTHISto continue checking characters.
Displaying Results:
PALINDROME:: This label marks the point where the loop finishes without encountering mismatching characters (palindrome).MOV AH, 09H: This setsAHfor displaying a string.LEA DX, MSG2: This loads the address of the "STRING IS PALINDROME" message intoDX.INT 21H: This calls the DOS function to display the message.
NOTPALINDROME:: This label marks the point where a mismatch was found (not a palindrome).MOV AH, 09H: This setsAHfor displaying a string.LEA DX, MSG3: This loads the address of the "STRING IS NOT PALINDROME" message intoDX.INT 21H: This calls the DOS function to display the message.
Program Termination:
XX:: This label marks the program's end point.MOV AH, 4CH: This setsAHto 4CH, which is the DOS function number for program termination.INT 21H: This terminates the program by calling the DOS function.
code ENDS: This marks the end of the code segment.END START: This signifies the overall program end, withSTARTbeing the starting point.
This code effectively reads a string from the user, checks if it's a palindrome, and displays the appropriate message.




0 Comments