Computing.Net > Forums > Programming > assembly - string manipulation

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

assembly - string manipulation

Reply to Message Icon

Name: Rob
Date: November 20, 2001 at 11:19:34 Pacific
Comment:

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 Clrscr

mov dx,offset prompt
call Writestring

mov dx,offset string
call Readstring

mov dx,offset OutputID
call Writestring

lea si,string+strlen-1
lea di,string2

mov cx,ax
L1:
mov ah,[si]
mov [di],ah
dec si
inc di
loop L1

mov dx,offset string2
call Writestring

mov ax,4c00h
int 21h
main endp
end main


Thanks for any help you can provide!



Sponsored Link
Ads by Google

Response Number 1
Name: Apple
Date: November 20, 2001 at 14:05:19 Pacific
Reply:

output? What's your return value look like?

How do you figure strlen?


0

Response Number 2
Name: Xeon
Date: November 21, 2001 at 13:12:56 Pacific
Reply:

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], ah

If 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, ax

b. use les instead of lea for string2 before L1 loop, i.e.
lea si, string+strlen-1
les di, string2

c. use segment prefix when moving the data, i.e.
mov ah, [si]
mov [ds:di], ah

You 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-1

L1:
mov ah, [di]
stosb
dec di
loop L1

The loop is one instruction less, which can be significant for very large strings.

HTH


0

Response Number 3
Name: sam
Date: February 14, 2002 at 18:28:02 Pacific
Reply:

why do u have to use
mov ax,@data
mov ds,ax
why we cannot do
mov ds,@data


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: assembly - string manipulation

VB String Manipulation www.computing.net/answers/programming/vb-string-manipulation/3883.html

Functions & String Manipulation www.computing.net/answers/programming/functions-string-manipulation/17352.html

MASM611 in DOS, reverse chars www.computing.net/answers/programming/masm611-in-dos-reverse-chars/246.html