Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I'm writing a program to reverse a string, and I can't seem to find the error in my program. It doesn't seem to want to output the reverse string. Here's what I've got:
.model small
.stack 100h
.data
prompt db "Please enter a message: ",0
OutputID db "The reverse of that is: ",0
string db 0
strlen equ 32
string2 db strlen dup(0).code
extrn Writechar:proc,Writestring:proc,Readstring:proc, Clrscr:proc,Crlf:proc
main proc
mov ax,@data
mov ds,ax
call Clrscrmov dx,offset prompt
call Writestringmov dx,offset string
call Readstringmov dx,offset OutputID
call Writestringlea si,string+strlen-1
lea di,string2mov cx,ax
L1:
mov ah,[si]
mov [di],ah
dec si
inc di
loop L1mov dx,offset string2
call Writestringmov ax,4c00h
int 21h
main endp
end main
Thanks for any help you can provide!

Hi,
Well, my asm is rusty. Your code seemed to be in perfect logic. Have you test it under a debugger? The first thing come to mind is the code :
mov ah, [si]
mov [di], ahIf I'm not mistaken (well, if it is, sorry) (E)SI regs defaulted to use DS reg as segment reg, while (E)DI defaulted to ES. At the start of your code, you only set DS reg. ES reg could point anywhere (or is it point to the PSP... hmm I wonder...) not necessarily to your data seg. If my suspicion is right, you might be writing the reversed string somewhere, and the writestring routine (I assume) will assume that you want to print the string pointed by DS:DX, which may or maynot contain the reversed string.
If this is the problem, the answer is either
a. init ES with the data segment at the start of your code, i.e.
mov ax, @data
mov ds, ax
mov es, axb. use les instead of lea for string2 before L1 loop, i.e.
lea si, string+strlen-1
les di, string2c. use segment prefix when moving the data, i.e.
mov ah, [si]
mov [ds:di], ahYou only need to take only one of the above options.
An improvement is to use string instructions inside the loop, in this case, stosb. A slightly modified code :
lea si, string2
les di, string+strlen-1L1:
mov ah, [di]
stosb
dec di
loop L1The loop is one instruction less, which can be significant for very large strings.
HTH

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |