Reverse a string in ASM

Below code is the program to reverse a string in assembly language.

;REVERSE STRING
NAME "REVERSE"
ORG 100H
JMP START
STRING1 DB 'EGAUGNAL YLBMESSA'
START:  LEA BX,STRING1
MOV SI,BX
NEXT_BYTE: CMP [SI],'$'
JE FOUND_THE_END
INC SI
JMP NEXT_BYTE
FOUND_THE_END: DEC SI
DO_REVERSE: CMP BX,SI
JAE DONE
MOV AL,[BX]
MOV AH,[SI]
MOV [SI],AL
MOV [BX],AH
INC BX
DEC SI
JMP DO_REVERSE
DONE:   LEA DX,STRING1
MOV AH, 09H
INT 21H
MOV AH,0
INT 16H
RET

In the above program, the string ‘EGAUGNAL YLBMESSA’ is declared in the register. Each character is reversed in the string and displayed the output as ‘ASSEMBLY LANGUAGE’.

Leave a Reply