Alright i have written my code to reverse a byte string character by character in-place using a loop. We have to utilize the SIZEOF and OFFSET operands and use indirect addressing, However my issue is that I am still getting the string displayed forward and not in reverse. I think there is something wrong in the beginning of my main PROC, I feel like i am not dealing with the ecx register right or that i am not loading the esi and edi registers correctly and setting the start and end pointers right. I am new at assembly so any help would be much appreciated. The following is my code:

TITLE MASM Assignment 3 reverse a string word by word                   (assign3.asm)

; Description:
; 
; Revision date:

INCLUDE Irvine32.inc
.data
msg    BYTE   "CIS 335 is a great course at CSU",0
ecxbkp DWORD  ?     ;save ecx if necessary 


.code
main PROC

mov esi, 0                             ;index register
mov edi, 0                           
mov ecx, SIZEOF msg                    ;initialize loop counter   * I believe the issue starts here???
mov eax, OFFSET msg + SIZEOF msg       ;address of msg
mov esi, eax                           ;esi points to start       
add eax, ecx               
mov edi, eax               
dec edi                                ;edi points to end
shr ecx,1                              ;shift loop counter 

L1: 
 mov al, [esi]                         ;load characters
 mov bl, [edi]
 mov [esi], bl                         ;swap characters
 mov [edi], al
 inc esi                               ;update forward pointer by 1
 dec edi                               ;update backward pointer by 1
 dec ecx                   
                                       ;loop
 loop L1

 ; display the string

    mov  edx,OFFSET msg
    call WriteString
    call Crlf                       ; print\r\n

    exit
main ENDP

END main

Hi,

Can you replace a part of your code for this one:

mov ecx, SIZEOF msg                    ;initialize loop counter   * I believe the issue starts here???
mov eax, OFFSET msg          ;address of msg
mov esi, eax                           ;esi points to start       
add eax, ecx               
mov edi, eax               
dec edi
dec edi                                ;edi points to end
shr ecx,1                              ;shift loop counter 

L1: 
 mov al, [esi]                         ;load characters
 mov bl, [edi]
 mov [esi], bl                         ;swap characters
 mov [edi], al
 inc esi                               ;update forward pointer by 1
 dec edi                               ;update backward pointer by 1
                                       ;loop
loop L1

With some changes to your code it works.

Best wishes.

Reedited: Please, forgive me. I have answered a solved question. Well, at least, everybody can see the solution.

Edited 2 Years Ago by untio

This question has already been answered. Start a new discussion instead.