替换程序集中的字符串

时间:2015-12-18 06:59:52

标签: string assembly

我想获得一个字符串,在其中找到并用替换字符串替换该键,以便我复制 源代码和结果中的替换字符串。
当源字符串中的密钥不存在时,它会输出正确的提示:"密钥不会出现在字符串中。"
但是当来源包含时,它会停滞不前并且不会继续运行 (它看起来在找到的标签部分已被遗漏并且有溢出)
任何人都可以帮助纠正找到部分?

任何帮助将不胜感激:)

; program to search for one string embedded in another
; author: R. Detmer revised: 10/97

.386
.MODEL FLAT

ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
INCLUDE io.h

cr          EQU    0dh   ; carriage return character
Lf          EQU    0ah   ; linefeed character

.STACK 4096              ; reserve 4096-byte stack

.DATA
prompt1      BYTE   "String to search? ", 0
prompt2      BYTE   cr, Lf, "Key to search for? ", 0
prompt3      BYTE   cr, Lf, "Word to replace? ", 0
source       BYTE   100 DUP (?)
key          BYTE   20 DUP (?)
replace      BYTE   20 DUP (?)
srcLength    DWORD  ?
keyLength    DWORD  ?
repLength    DWORD  ?
BeginLength  DWORD  ?
restLength   DWORD  ?
cpyLength    DWORD  ?
lastPosn     DWORD  ?
restPosition DWORD  ?
firstParam   DWORD  ?
secondParam  DWORD  ?
keyPosition  DWORD  ?
failure      BYTE   cr,Lf,Lf,"The key does not appear in the string.",cr,Lf,0
success      BYTE   cr,Lf,Lf, " The result string is : " ,cr,Lf,Lf
result       BYTE   200 DUP (?)

PUBLIC _start                      ; make entry point public
.CODE

_start:     output prompt1         ; ask for
            input  source,100       ; and input source string
            lea    eax, source     ; find length of string
            push   eax             ; length parameter
            call   strlen
            mov    srcLength,eax  ; save length of source

            output prompt2         ; ask for
            input  key,20          ; and input key string
            lea    eax, key        ; find length of string
            push   eax             ; length parameter
            call   strlen
            mov    keyLength,eax   ; save length of key

            output prompt3         ; ask for
            input  replace,20          ; and input replace string
            lea    eax, replace        ; find length of string
            push   eax             ; length parameter
            call   strlen
            dec    eax
            mov    repLength,eax   ; save length of replace

; calculate last position of source to check
            mov    eax,srcLength
            sub    eax,keyLength
            inc    eax             ; srcLength − keyLength + 1
            mov    lastPosn, eax
            cld                    ; left to right comparison

            mov    eax,1           ; starting position
whilePosn:  cmp    eax,lastPosn    ; position <= last_posn?
            jnle   endWhilePosn    ; exit if past last position
            lea    esi,source      ; address of source string
            add    esi,eax         ; add position
            dec    esi             ; address of position to check is incremented automatically
            lea    edi,key         ; address of key
            mov    ecx,keyLength   ; number of positions to check
            repe   cmpsb           ; check
            jz     found           ; exit on success
            inc    eax             ; increment position
            jmp    whilePosn       ; repeat
endWhilePosn:
            output failure         ; the search failed
            jmp    quit             ; exit
            ;-------------------------------------------------------------

found:      
            mov    keyPosition, eax  ; position of key
            mov    ebx, eax         ;copy start position of key
            lea    eax, source
            sub    ebx, eax          ;position - source address 
            mov    BeginLength, ebx  ;begin Source length (before key)
            add    ebx, keyLength
            mov    eax, srcLength   
            sub    eax, ebx
            mov    restLength, eax ;rest of Source length (after key)
            mov    eax, keyPosition
            add    eax, keyLength  ; position + key 
            mov    restPosition, eax

           ;source begin to result
            lea    eax, result 
            mov    firstParam, eax   ; destination address                  
            lea    eax, source
            mov    secondParam, eax
            mov    eax, BeginLength  ; copy length
            mov    cpyLength, eax                   

            mov    esi,firstParam     ;initial source address
            mov    edi,secondParam     ;destination
            mov    ecx ,cpyLength
            rep  movsb                     ;copy bytes



            ;replace to result
            mov    eax, firstParam
            add    eax , BeginLength   
            mov    firstParam, eax   ; address of  rest of result
            lea    eax, replace
            mov    secondParam, eax   ; string to replace
            mov    eax, repLength     ; copy length
            mov    cpyLength, eax 

            mov    esi,firstParam     ;initial source address
            mov    edi,secondParam     ;destination
            mov    ecx ,cpyLength
            rep  movsb                     ;copy bytes

            ;Rest to result
            mov    eax, firstParam
            add    eax , repLength
            mov    firstParam, eax  ; address of  rest of result
            mov    eax, restPosition
            mov    secondParam, eax
            mov    eax, restLength
            mov    cpyLength, eax

            mov    esi,firstParam     ;initial source address
            mov    edi,secondParam     ;destination
            mov    ecx ,cpyLength
            rep  movsb                     ;copy bytes

            mov    BYTE PTR [edi],0  ;terminate destination string
            output success

quit:
            INVOKE ExitProcess, 0  ; exit with return code 0
            ;----------------------------------------------------------

strlen      PROC   NEAR32
; find length of string whose address is passed on stack
; length returned in EAX
            push   ebp             ; establish stack frame
            mov    ebp, esp
            pushf                  ; save flags
            push   ebx             ; and EBX
            sub    eax, eax        ; length := 0
            mov    ebx, [ebp+8]    ; address of string
whileChar:  cmp    BYTE PTR [ebx], 0 ; null byte?
            je     endWhileChar    ; exit if so
            inc    eax             ; increment length
            inc    ebx             ; point at next character
            jmp    whileChar       ; repeat
endWhileChar:
            pop    ebx             ; restore registers and flags
            popf
            pop    ebp
            ret    4               ; return, discarding parameter
strlen      ENDP

            END

1 个答案:

答案 0 :(得分:1)

success     BYTE   cr,Lf,Lf, " The result string is : " ,cr,Lf,Lf

您忘记了终止成功消息 它会破坏你的最终宏调用output success,因此似乎程序没有正确替换字符串。