Thursday 23 April 2009

Reversing a String

;8086 program to reverse the entered string

data segment
msg1 db "Enter the string: $"
msg2 db 0ah,0dh,"Reversed string is: $"
ent db 0dh,0ah,"$"
str1 db 100 dup(?)
rev db 100 dup(?)
data ends

print macro msg
lea dx,msg
mov ah,09h
int 21h
endm

read macro
mov ah,01h
int 21h
endm

code segment
start:
mov ax,data
mov ds,ax

mov bx,0000h
lea si,str1
print msg1

L1:
read
mov [si],al
inc bx
inc si
cmp al,0dh
jnz L1


dec si
mov cx,bx
lea di,rev

L2:
mov al,[si]
mov [di],al
inc di

dec si
dec cx
jnz L2

print msg2
add si,bx

mov cx,bx
print ent

L3:
mov dl,[si]
mov ah,02h
int 21h
dec si
dec cx
jnz L3


en:

mov ah,4ch
int 21h
code ends
end start

;Program ends Here

querrymail@gmail.com

0 comments: