汇编语言 - 如何使用STOSB和LODSB来反转字符串?

时间:2014-05-13 07:04:14

标签: string assembly x86 reversing

StrReverse proc 

                   uses ecx eax edi esi,
                   StrAdd1:dword,   ;string 1 address
                   StrAdd2:dword    ;string 2 address



    std                     ;backward direction - set direction flag
    push StrAdd2            ;address of str2 arg to StrlenAsm
    call StrLenAsm          ;get length of str2
                            ;called function responsible for stack cleanup
    mov ecx,eax             ;length of string in ecx for rep
    mov edi,StrAdd1         ;edi gets destination address for copy
    mov esi,StrAdd2         ;esi gets source address for copy

loopTop:

    lodsb                   ;
    stosb                   ;
    loop loopTop

    mov byte ptr[edi],0     ;null terminate copied string
    ret                     ;return control to caller

StrReverse endp

我知道STD造成了麻烦,但我想如果我想要倒转字符串,我应该使用std ...任何人都可以向我解释为什么这是错误的并给出提示如何解决它?

感谢您的进一步帮助!

修改: 那样这样吗?

StrReverse proc 
               uses ecx eax edi esi, ;
               StrAdd1:dword,   ;string 1 address
               StrAdd2:dword    ;string 2 address


push StrAdd1            ;address of str2 arg to StrlenAsm
call StrLenAsm          ;get length of str2
                        ;called function responsible for stack cleanup
mov ecx,eax             ;length of string in ecx for rep
mov edi,StrAdd1         ;edi gets destination address for copy
mov esi,StrAdd2         ;esi gets source address for copy
add edi, ecx

loopTop:

cld                     ;forword direction - clear direction flag
lodsb                   ;read from source string
std                     ;backward direction - set direction flag
stosb                   ;write into distination string
loop loopTop

mov byte ptr[edi],0     ;null terminate copied string
ret                     ;return control to caller

StrReverse endp

它仍在崩溃= [

1 个答案:

答案 0 :(得分:0)

我建议改变方向,因为:

  1. 当循环结束时 - 但最迟在程序结束时 - 方向标志必须设置为“前进”,

  2. mov byte ptr[edi],0EDI必须指向最后 字符。

  3. 字符串的长度是loop的正确数字(在ECX == 0时断开),但不是ESI(从偏移0开始)。在Google上搜索“关闭一个错误”。

    这个有效:

    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