0% found this document useful (0 votes)
2 views2 pages

Exp 4 Reverse String

This assembly language program reverses a string using a stack. It pushes each character of the string onto the stack and then pops them back to overwrite the original string in reverse order. Finally, it displays the reversed string using a DOS function before exiting the program.

Uploaded by

ubaidsayyad36
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)
2 views2 pages

Exp 4 Reverse String

This assembly language program reverses a string using a stack. It pushes each character of the string onto the stack and then pops them back to overwrite the original string in reverse order. Finally, it displays the reversed string using a DOS function before exiting the program.

Uploaded by

ubaidsayyad36
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

.

MODEL SMALL
.STACK 100H

.DATA
STR1 DB 'Computer', '$' ; Original string (ends with $)
LEN DW 6 ; Length of the string

.CODE
MAIN PROC
MOV AX, @DATA ; Initialize Data Segment
MOV DS, AX

MOV SI, OFFSET STR1 ; SI points to the start of the string


MOV CX, LEN ; Load loop counter with string length

; --- Step 1: Push characters onto the Stack ---


PUSH_LOOP:
MOV AL, [SI] ; Get character
PUSH AX ; Push onto stack (Note: PUSH works on 16-bit)
INC SI ; Move to next character
LOOP PUSH_LOOP

; --- Step 2: Pop characters back to String ---


MOV SI, OFFSET STR1 ; Reset SI to start of string
MOV CX, LEN ; Reset loop counter

POP_LOOP:
POP AX ; Pop top of stack (last character pushed)
MOV [SI], AL ; Overwrite string memory with the character
INC SI ; Move to next position
LOOP POP_LOOP

; --- Step 3: Display the reversed string ---


MOV DX, OFFSET STR1 ; Load address for DOS display function
MOV AH, 09H ; DOS string output function
INT 21H

; --- Exit Program ---


MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN

You might also like