0

So I'm having trouble with my code I'm trying to reverse the string so the output ends in "gnirts ecruos eht si sihT". However, I can't seem to figure out the problem.

INCLUDE Irvine32.inc

;Data section declaring variables
.data
    source BYTE "This is the source string",0
    target BYTE SIZEOF source DUP('#')

   ;main code
.code
main PROC              
    mov esi,0                
   mov edi,LENGTHOF source - 2     
   mov ecx,SIZEOF source    
    L1:                   
    mov al,source[edi]                                
    mov target[edi],al  
     inc esi                        
     dec edi                  
     loop L1
    mov edx, OFFSET target     
    call WriteString          
Invoke ExitProcess,0

main ENDP

END main
Jester
  • 56,577
  • 4
  • 81
  • 125
kal
  • 1
  • 1
  • 2
    You have a typo. You used `edi` instead of `esi` for the `source`. PS: learn to use a debugger. PPS: your `target` won't be correctly zero terminated and your `ecx` seems to be one too much. – Jester Mar 02 '20 at 02:46
  • ty im trying to learn kinda new to assembly slowly learning @Jester – kal Mar 02 '20 at 02:50
  • @Jester so do you suggest something to improve the code to be more efficient or is it alright how it is? – kal Mar 02 '20 at 02:56
  • If you want *efficiency*, you can go 4 bytes at a time with `bswap`, or with SSSE3 16 bytes at a time with `pshufb`. But at this point you should mostly worry about correctness. [The `loop` instruction is slow; don't use it](https://stackoverflow.com/questions/35742570/why-is-the-loop-instruction-slow-couldnt-intel-have-implemented-it-efficiently). You could also just reverse one string in-place with pointers to the start/end; and detect them meeting or crossing in the middle. (Then your loop condition could just be `cmp esi, edi` / `ja L1` or something). That would simplify 0-terminating – Peter Cordes Mar 02 '20 at 03:05
  • @PeterCordes well I'm kinda in an intro to assembly language class in college so our professor doesn't want us to do anything complicated. I just wanted to know if my code is good enough as of now to get the desired output? – kal Mar 02 '20 at 03:08
  • You asked how to improve the code to be more efficient. That's how. But like I said, you don't *actually* want do optimize it for speed until you're more comfortable with the basics. – Peter Cordes Mar 02 '20 at 04:07

0 Answers0