We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,174 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Reversing string Problem

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?

2
Contributors
1
Reply
8 Hours
Discussion Span
1 Year Ago
Last Updated
2
Views
football.12345
Newbie Poster
1 post since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

Narue
Bad Cop
Team Colleague
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 54

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0595 seconds using 2.68MB