Question

I write a program to reverse a string it given, but it seems that the result didn't match.

I use foward and backward to indicate the index to exchange and finally add the null character in the tail.

The result 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 00

but My result is

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 00

it seem something wrong, bur I cannot find out where is the bug.

Could you help me to find where might be wrong?

Thx in advance.

INCLUDE Irvine32.inc

.data
source BYTE "This is the source string",0;26
target BYTE SIZEOF source DUP('#')
strl BYTE 0 
foward DWORD 0
backward DWORD 0

.code
main PROC
    mov strl, LENGTHOF source;26
    dec strl; last char is 25. 26 is null
    movzx ecx, strl;count = 25 
    mov foward, 0
    movzx eax, strl
    mov backward, eax

    L1:
        mov esi, backward
        mov al,source[esi]
        dec backward

        mov esi, foward
        mov target[esi], al
        inc foward

        loop L1

    movzx esi, strl

    mov al,source[esi]
    mov target[esi], al



    mov esi,OFFSET target ; offset of variable
    mov ebx,1 ; byte format
    mov ecx,SIZEOF target ; counter
    call DumpMem

main ENDP

END main
Was it helpful?

Solution

Change this:

    mov esi, backward
    mov al,source[esi]
    dec backward

To:

    dec backward
    mov esi, backward
    mov al,source[esi]

because:

    movzx eax, strl
    mov backward, eax

initially backward points not to last element of string, but one after last (0 byte), so you first read it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top