What is wrong with my code? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

What is wrong with my code?

LEMLEM Posts: 15Member
Hi everyone!!! I have a question how do you copy a string backwards?
I'm using the following code:

INCLUDE Irvine32.inc
.data
source BYTE "This is the source string",0
target Byte SIZEOF source DUP(0)
.code
main PROC
mov esi,0 ; index register
mov ecx,SIZEOF source ; loop counter


L1:

mov al, source[esi] ; get a character from source
mov target[esi],al ; store it in the target
inc esi ; move to next character
loop L1


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

exit
main ENDP

END main


So then my output is:


Dump of offset 0040401A
-------------------------------
54 68 69 73 20 69 73 20 74 68 65 20 73 6F 75 72
63 65 20 73 74 72 69 6E 67
Press any key to continue

Which is wrong it should be backwards starts at 67 and ends at 54

Thanks in advance, bye.....

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : Hi everyone!!! I have a question how do you copy a string backwards?
    : I'm using the following code:
    :
    : INCLUDE Irvine32.inc
    : .data
    : source BYTE "This is the source string",0
    : target Byte SIZEOF source DUP(0)
    : .code
    : main PROC
    : mov esi,0 ; index register
    : mov ecx,SIZEOF source ; loop counter
    :
    :
    : L1:
    :
    : mov al, source[esi] ; get a character from source
    : mov target[esi],al ; store it in the target
    : inc esi ; move to next character
    : loop L1
    :
    :
    : mov esi,OFFSET target
    : mov ebx,1
    : mov ecx,SIZEOF target-1
    : call DumpMem
    :
    : exit
    : main ENDP
    :
    : END main
    :
    :
    : So then my output is:
    :
    :
    : Dump of offset 0040401A
    : -------------------------------
    : 54 68 69 73 20 69 73 20 74 68 65 20 73 6F 75 72
    : 63 65 20 73 74 72 69 6E 67
    : Press any key to continue
    :
    : Which is wrong it should be backwards starts at 67 and ends at 54
    :
    : Thanks in advance, bye.....
    :
    :
    [code]
    L1:
    mov al, source[esi] ; get a character from source
    mov target[esi],al ; store it in the target
    inc esi ; move to next character
    loop L1
    [/code][blue]You are simply copying the symbols from one buffer to the other using the [red]same[/red] ESI. You should set up TWO index regs to work with. One set at the source 1-st byte, another at the target LAST byte. Then after move you increment source, BUT decrement target.[/blue]
Sign In or Register to comment.