2

I have this code designed to take a string in "originString" and put it in reverse order in "destinationString". I can't figure out how to make destinationString fill up and not just hold one value and then also print out with writeString and not just writeChar. Here is what i have.

INCLUDE     Irvine32.inc
INCLUDELIB  Irvine32.lib

.386
.model flat, stdcall
.stack 4096

ExitProcess PROTO, dwExitCode:DWORD

.data
originString        BYTE    "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0
destinationString   BYTE    SIZEOF originString DUP(?), 0
counter             BYTE    1




.code
main PROC
    ; YOUR CODE GOES HERE...
    ; Write a procedure that copies the contents of 'originString' to
    ;  'destinationString', but in reverse order.  You should use the
    ;  provided initialization value for 'originString' to test, but your
    ;  solution must work even if the contents or 'originString' are changed.
    MOV EAX, LENGTHOF originString
    MOV ECX, EAX

countLoop_BEGIN:
                    
    MOVZX EAX, counter
    MOV EBX, LENGTHOF originString
    SUB EBX, EAX
    MOV AL, BYTE PTR [originString + (EBX-1)]
    cmp destinationString, ' '
    je   end_shifting
    inc EBX
    MOV destinationString, AL
    MOVZX EDX, destinationString
    CALL WriteString
    ;CALL Crlf
    INC counter

    LOOP countLoop_Begin    ; If ECX IS NOT EQUAL to 0, decrement ECX by 1 and JMP to 'countLoop_Begin'.
                            ; If ECX IS EQUAL to 0, do not jump and move onto the instruction following LOOP.

    MOV destinationString, AL
    MOV EDX, OFFSET destinationString
    CALL WriteString
    CALL Crlf
    end_shifting:    
        ret


    INVOKE ExitProcess, 0

main ENDP



END main

1 Answers1

0

I can't figure out how to make destinationString fill up and not just hold one value

In your loop the instruction MOV destinationString, AL always writes to the first location of the destinationString. Why don't you index the write much like the way the read from originString works? You can use counter for indexing the write too.

This is the quick fix:

countLoop_BEGIN:
    MOVZX EDX, counter                  ; 1,2,3,...
    MOV   EBX, LENGTHOF originString
    SUB   EBX, EDX
    MOV   AL, [originString + (EBX-1)]
    MOV   [destinationString + (EDX-1)], AL
    INC   counter
    DEC   ECX
    JNZ   countLoop_Begin

and then also print out with writeString and not just writeChar.

For WriteString to work, you still need to zero-terminated the destinationString.

    MOV   byte [destinationString + EDX], 0
    MOV   EDX, OFFSET destinationString
    CALL  WriteString
Sep Roland
  • 33,889
  • 7
  • 43
  • 76