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?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.