I suggest to turn the directions because:
When the loop ends - but at the latest at the end of the procedure - the direction flag must be set to "forward",
mov byte ptr[edi],0
:EDI
must point behind the last character.
The length of the string is the right number for loop
(which breaks at ECX==0) but not for ESI
(which begins at offset 0). Search on Google for "off by one error".
This one works:
include \masm32\include\masm32rt.inc ; MASM32 headers for 'printf'
.data
str2 db "Hallo World!", 0
str1 db SIZEOF str2 dup ('-')
.code
StrReverse proc uses ecx eax edi esi,
StrAdd1:dword, ;destination string address
StrAdd2:dword ;source string address
mov ecx, SIZEOF str2 - 1 ; length of string (w/o last null) in ecx for rep
mov edi,StrAdd1 ; edi gets destination address for copy
mov esi,StrAdd2 ; esi gets source address for copy
add esi, ecx ; source pointer to the end
sub esi, 1 ; adjust esi to base 0 (1 char = +0 bytes)
loopTop:
std ; backward
lodsb ; read from source string
cld ; forward
stosb ; write into distination string
loop loopTop
mov byte ptr[edi],0 ; null terminate copied string
ret ; return control to caller
StrReverse endp
main PROC
printf ("%s\n", OFFSET str2)
push OFFSET str2
push OFFSET str1
call StrReverse
printf ("%s\n", OFFSET str1)
invoke ExitProcess, 0
main ENDP
END main