0% found this document useful (0 votes)
17 views4 pages

Lab A1

This document contains an assembly language program that reverses a user-input string. It prompts the user for input, reads the string, and then displays the reversed string on the screen. The program concludes by waiting for a key press before exiting.

Uploaded by

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

Lab A1

This document contains an assembly language program that reverses a user-input string. It prompts the user for input, reads the string, and then displays the reversed string on the screen. The program concludes by waiting for a key press before exiting.

Uploaded by

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

COAL (LAB) JUNE 12, 2025

ASSEMBLY LANGUAGE PROGRAM


REVERSE A STRING
Code:
.model small
.stack 100h
.data
msg db 'Enter a string: $'
reverseMsg db 13, 10, 'Reversed string: $' ; Message with new line
exitMsg db 13, 10, 'Press any key to exit...$'
input db 50 ; Max buffer size (50 characters)
db ? ; Actual characters entered
db 50 dup (?) ; Input data buffer

.code
main:
mov ax, @data
mov ds, ax

; Display input prompt


lea dx, msg
mov ah, 09h
int 21h

; Read string input from user


lea dx, input
mov ah, 0Ah ; DOS buffered input
int 21h

; Load length of input


mov cl, [input + 1] ; Number of characters entered
mov ch, 0 ; Clear upper byte of CX

; Display reverse message (will stay on the same line)


lea dx, reverseMsg
mov ah, 09h
int 21h

; Prepare to display in reverse


lea si, input + 2 ; SI points to start of input string
add si, cx ; Move SI to last character
dec si ; Adjust to last valid character

reverse_display:
mov dl, [si] ; Load character
mov ah, 02h ; DOS display character function
int 21h
dec si ; Move to previous character

Page | 1
COAL (LAB) JUNE 12, 2025

loop reverse_display ; Continue until all characters displayed

; Display exit message


lea dx, exitMsg
mov ah, 09h
int 21h

; Wait for any key press


mov ah, 01h ; DOS input character function (waits for key)
int 21h

; Exit program
mov ah, 4Ch
int 21h

end main

Output:

Step-by-Step Explanation:
.model small
.stack 100h

 .model small: Defines a small memory model (separate code and data segments, each less
than 64KB).
 .stack 100h: Reserves a stack of size 256 bytes (100h = 256 in hexadecimal).

.data
msg db 'Enter a string: $'
reverseMsg db 13, 10, 'Reversed string: $'
exitMsg db 13, 10, 'Press any key to exit...$'
input db 50
db ?
db 50 dup (?)

 msg: Prompt message displayed before user input. Ends with $ for DOS interrupt 21h
(function 09h).
 reverseMsg: Displays "Reversed string:" with a new line (13, 10 = carriage return + line
feed).

Page | 2
COAL (LAB) JUNE 12, 2025

 exitMsg: Displays exit instruction, also starting on a new line.


 input db 50: DOS buffered input. First byte = maximum input length.
 db ?: Placeholder. DOS stores the number of characters actually typed here.
 db 50 dup (?): Reserves 50 bytes for the actual string data entered by the user.

.code
main:
mov ax, @data
mov ds, ax

 .code: Begins the code segment.


 main:: Program entry point.
 mov ax, @data: Loads the address of the data segment into AX.
 mov ds, ax: Initializes the DS register to point to the data segment.

; Display input prompt


lea dx, msg
mov ah, 09h
int 21h

 lea dx, msg: Load the effective address of msg into DX.
 mov ah, 09h: DOS function to display a string.
 int 21h: Interrupt to call DOS function (display string until $ is found).

; Read string input from user


lea dx, input
mov ah, 0Ah
int 21h

 lea dx, input: Load the input buffer address into DX.
 mov ah, 0Ah: DOS buffered input function. Reads string into the buffer.
 int 21h: Calls the DOS input function.

; Load length of input


mov cl, [input + 1]
mov ch, 0

 mov cl, [input + 1]: Gets the number of characters entered from the second byte of the
buffer.
 mov ch, 0: Clears upper byte of CX. Now CX holds the correct length.

; Display reverse message


lea dx, reverseMsg
mov ah, 09h
int 21h

 lea dx, reverseMsg: Load address of the reverse message.


 mov ah, 09h: DOS function to display a string.
 int 21h: Displays "Reversed string: " (with a new line).

Page | 3
COAL (LAB) JUNE 12, 2025

; Prepare to display in reverse


lea si, input + 2
add si, cx
dec si

 lea si, input + 2: Points SI to the first character entered (buffer offset + 2).
 add si, cx: Moves SI forward by the length of the string → now SI points just after the last
character.
 dec si: Moves SI back by one byte → now SI points exactly to the last character.

reverse_display:
mov dl, [si]
mov ah, 02h
int 21h
dec si
loop reverse_display

 mov dl, [si]: Loads the character at the current position into DL.
 mov ah, 02h: DOS function to display a single character (character in DL).
 int 21h: Display the character.
 dec si: Move SI backward → next character to display in reverse.
 loop reverse_display: Decrement CX → if CX ≠ 0, repeat the loop.

; Display exit message


lea dx, exitMsg
mov ah, 09h
int 21h

 lea dx, exitMsg: Load exit message address.


 mov ah, 09h: DOS function to display a string.
 int 21h: Show "Press any key to exit..."

; Wait for any key press


mov ah, 01h
int 21h

 mov ah, 01h: DOS function to wait for a key press (character will be read but not used).
 int 21h: Pauses program until the user presses any key.

; Exit program
mov ah, 4Ch
int 21h

 mov ah, 4Ch: DOS function to terminate the program.


 int 21h: Ends the program and returns control to the OS.

end main

 end main: Marks the end of the program and sets the entry point to main.

Page | 4

You might also like