Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.