1,105,386 Community Members

Reversing string Problem

Member Avatar
football.12345
Newbie Poster
1 post since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
TITLE Copying a String Backwards                 (CopyStr.asm)

; This program copies a string backwards.

INCLUDE Irvine32.inc

.data
source  BYTE  "This is the source string",0
target  BYTE  SIZEOF source DUP('#')

.code
main PROC

	mov  esi,0	; index register
	mov  ecx,SIZEOF source	; loop counter

L1:
	mov  al,source[ecx-1]		; get a character from source
	mov  target[esi],al		; store it in the target
	inc esi			; move to next character
	loop L1				; repeat for entire string

	mov esi,OFFSET target
	mov ebx,1
	mov ecx,SIZEOF target
	call DumpMem

	exit
main ENDP
END main

I know my code reveres the string correctly because I look at the memory in debug mode, but when I DumpMem I get an answer that is off by one spot. I get the following:

00 67 6E 69 72 74 73 20 65 63 72 75 6F 73 20 65 68 74 20 73 69 20 73 69 68 54

but it should be:

67 6E 69 72 74 73 20 65 63 72 75 6F 73 20 65 68 74 20 73 69 20 73 69 68 54

I cant figure out why I have the extra 00 can someone please help?

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 80 [?]
Team Colleague
 
0
 

The extra 00 is the 0 you tacked onto the end of source . You should initialize ecx to SIZEOF source - 1, since that 0 is a sentinel rather than a value character.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: