x86基础实验-字符串替换

简介

读取一个字符串,将指定子串替换为另一个字符串并输出

程序运行结果
image.png
代码
ASSUME  CS:CODE1,DS:DATA1,SS:STACK1

STACK1  SEGMENT STACK
STACK_AREA  DW  100H DUP(?)
STACK_BTM   EQU $-STACK_AREA
STACK1      ENDS

DATA1   SEGMENT
STR_IN_EXPLAIN  DB  "please input original string",13,10,0
STR_SRC_EXPLAIN DB  "please input string you want to replace for",13,10,0
STR_DST_EXPLAIN DB  "please input string you want to replace to",13,10,0

STR_IN  DB  100H dup(?) ;"HELLO_WORLD" ,0
STR_OUT DB  100H DUP(?) ;
STR_SRC DB  100H DUP(?) ;"L",0
STR_DST DB  100H DUP(?) ;"LL" ,0

DATA1   ENDS

CODE1   SEGMENT

MAIN    PROC    FAR

        MOV     AX,STACK1
        MOV     SS,AX
        MOV     SP,STACK_BTM
        MOV     AX,DATA1
        MOV     DS,AX


        MOV     AX,OFFSET STR_IN_EXPLAIN ;read str in
        PUSH    AX
        CALL    OUTPUT_STR

        MOV     AX,OFFSET STR_IN   
        PUSH    AX
        CALL    INPUT_STR


        MOV     AX,OFFSET STR_SRC_EXPLAIN   ;read src str
        PUSH    AX
        CALL    OUTPUT_STR

        MOV     AX,OFFSET STR_SRC
        PUSH    AX
        CALL    INPUT_STR



        MOV     AX,OFFSET STR_DST_EXPLAIN
        PUSH    AX
        CALL    OUTPUT_STR

        MOV     AX,OFFSET STR_DST   ;read dst str
        PUSH    AX
        CALL    INPUT_STR

        MOV     AX,OFFSET STR_IN
        PUSH    AX
        MOV     AX,OFFSET STR_OUT
        PUSH    AX
        MOV     AX,OFFSET STR_SRC
        PUSH    AX
        MOV     AX,OFFSET STR_DST
        PUSH    AX
        CALL    STRRPL

        MOV     AX,OFFSET STR_OUT
        PUSH    AX
        CALL    OUTPUT_STR

EXIT:   MOV     AX,4C00H
        INT     21H

MAIN    ENDP

INPUT_STR   PROC    NEAR    ; [SP+2]ADD of DST_STR \n end
    
    PUSH SI
    PUSH AX
    PUSH BX
    PUSH CX
    PUSH DX

    MOV     SI,SP
    MOV     SI,SS:[SI+12]
    MOV     BX,0    

INPUT_STR_LOOP:
    MOV     AH,01H
    INT     21H
    CMP     AL,13   ;\n
    JZ      END_INPUT_STR_LOOP    
    MOV     [SI+BX],AL 
    INC     BX
    JMP     INPUT_STR_LOOP

END_INPUT_STR_LOOP:
    MOV     BYTE PTR [SI+BX],0


    POP DX
    POP CX
    POP BX
    POP AX
    POP SI

    RET 2

    
INPUT_STR   ENDP

OUTPUT_STR  PROC    NEAR ;OUTPUT a string end with '\0' [SP+2] str
    PUSH    SI
    PUSH    AX
    PUSH    DX

    MOV     SI,SP
    MOV     SI,SS:[SI+8]

    MOV     AH,2H
OUTPUT_STR_LOOP:
    MOV     DL,[SI]
    CMP     DL,0
    JZ      END_OUTPUT_STR_LOOP
    INT     21H
    INC     SI
    JMP     OUTPUT_STR_LOOP

END_OUTPUT_STR_LOOP:    

    POP     DX
    POP     AX
    POP     SI
    RET     2
OUTPUT_STR  ENDP

STRRPL  PROC    NEAR    ;[SP+2]STR_DST [SP+4]STR_SRC [SP+6]STR_OUT [SP+8]STR_IN 

    
    PUSH    BP
    PUSH    SI 
    PUSH    DI
    PUSH    AX
    PUSH    BX
    PUSH    CX
    PUSH    DX
    
        
        MOV     BP,SP
        ADD     BP,14


        MOV     SI,    [BP+4] ;STR_SRC
        MOV     BX,-1
LOOP_GET_STR_SRC_LEN:   ;BP-2
        INC     BX
        MOV     AL,[BX+SI]
        CMP     AL,0
        JNZ      LOOP_GET_STR_SRC_LEN
        PUSH    BX 


        MOV     DI,     [BP+2];STR_DST
        MOV     BX,-1
LOOP_GET_STR_DST_LEN:   ;BP-4
        INC     BX
        MOV     AL,[BX+DI]
        CMP     AL,0
        JNZ      LOOP_GET_STR_DST_LEN
        PUSH    BX 

        
        MOV     SI, [BP+8]  ;STR_IN
        MOV     DI, [BP+6]  ;STR_OUT

LOOP_CREATE_NEW_STR:


        MOV     BX,0        
        MOV     CX,0
        PUSH    DI
        MOV     DI,[BP+4]   ;STR_SRC

LOOP_CMP_STR:   ;   CX:0 is same
        MOV     AL,[SI+BX]  
        MOV     AH,[DI+BX]

        CMP     AH,0 
        JZ      LOOP_CMP_STR_END

        XOR     AL,AH
        OR      CL,AL
        

        INC     BX
        JMP     LOOP_CMP_STR 

LOOP_CMP_STR_END:

        POP     DI      ;STR_OUT

        CMP     CX,0
        JNZ     SKIP_RPL

        PUSH    DI
        MOV     AX,[BP+2];STR_DST
        PUSH    AX

        CALL    SAFE_STRCPY


        MOV     AX,[BP-16]
        ADD     SI,AX
        MOV     AX,[BP-18]
        ADD     DI,AX

        JMP LOOP_CREATE_NEW_STR

SKIP_RPL:
        MOV AL,[SI]
        MOV [DI],AL
        INC SI
        INC DI

        CMP AL,0
        JNZ LOOP_CREATE_NEW_STR

LOOP_CREATE_NEW_STR_END:
    
    POP     BX
    POP     BX

    POP     DX 
    POP     CX 
    POP     BX 
    POP     AX 
    POP     DI
    POP     SI
    POP     BP 

        RET 8


STRRPL  ENDP


SAFE_STRCPY PROC    NEAR    ; [SP+2]SRC_STR [SP+4]DST_STR

    PUSH    ES
    PUSH    SI 
    PUSH    DI
    PUSH    AX
    PUSH    BX
    PUSH    CX
    PUSH    DX

    MOV     BX,SP
    MOV     SI,SS:[BX+16]
    MOV     DI,SS:[BX+18]


    MOV     BX,-1
LOOP_GET_LEN:
    INC     BX
    MOV     AX,[SI+BX]
    CMP     AX,0
    JNE     LOOP_GET_LEN

    MOV     CX,BX
    INC     CX
    
    MOV     AX,DS
    MOV     ES,AX

    CMP     SI,DI
    JA      LOOP_CPY 

    STD
    ADD     SI,BX
    ADD     DI,BX

LOOP_CPY:
    MOVSB     
    LOOP LOOP_CPY
    


    POP     DX 
    POP     CX 
    POP     BX 
    POP     AX 
    POP     DI
    POP     SI
    POP     ES

    RET 4

SAFE_STRCPY ENDP

CODE1   ENDS
END     MAIN

你可能感兴趣的:(x86基础实验-字符串替换)