简介
对字符串进行复制、查找、替换、大小写转换、输出等编辑功能。
程序运行结果
image.pngimage.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_FIND_EXPLAIN DB "please input string you want to find in original string",13,10,0
STR_FIND_RESULT_EXPLAIN DB "position of string you want to find:",0
STR_NOT_FIND_RESULT_EXPLAIN DB "the string you want to find not in original string",13,10,0
STR_FUNCTION_SELECTION_EXPLAIN DB "please select function:1.copy 2.find 3.replace 4.upper 5.lower 6.output other.quit: ",0
STR_ENTER DB 13,10,0
STR_IN DB 100H DUP(0) ;"HELLO_WORLD" ,0
STR_OUT DB 100H DUP(0) ;
STR_SRC DB 100H DUP(0) ;"L",0
STR_DST DB 100H DUP(0) ;"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
MAIN_LOOP:
MOV AX,OFFSET STR_ENTER
PUSH AX
CALL OUTPUT_STR
MOV AX,OFFSET STR_FUNCTION_SELECTION_EXPLAIN
PUSH AX
CALL OUTPUT_STR
MOV AH,01
INT 21H
MOV DL,AL
CMP DL,'1'
JNZ MAIN_2
CALL MAIN_STRCPY
JMP MAIN_LOOP
MAIN_2:
CMP DL,'2'
JNZ MAIN_3
CALL MAIN_STRSTR
JMP MAIN_LOOP
MAIN_3:
CMP DL,'3'
JNZ MAIN_4
CALL MAIN_STRRPL
JMP MAIN_LOOP
MAIN_4:
CMP DL,'4'
JNZ MAIN_5
CALL MAIN_UPPER
JMP MAIN_LOOP
MAIN_5:
CMP DL,'5'
JNZ MAIN_6
CALL MAIN_LOWER
JMP MAIN_LOOP
MAIN_6:
CMP DL,'6'
JNZ MAIN_OTHER
CALL MAIN_OUTPUT_STR
JMP MAIN_LOOP
MAIN_OTHER:
JMP EXIT
EXIT: MOV AX,4C00H
INT 21H
MAIN ENDP
MAIN_OUTPUT_STR PROC NEAR
MOV AX,OFFSET STR_IN
PUSH AX
CALL OUTPUT_STR
RET
MAIN_OUTPUT_STR ENDP
MAIN_STRRPL PROC NEAR
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_IN
PUSH AX
MOV AX,OFFSET STR_OUT
PUSH AX
CALL SAFE_STRCPY
RET
MAIN_STRRPL ENDP
MAIN_STRCPY PROC NEAR
MOV AX,OFFSET STR_OUT
PUSH AX
MOV AX,OFFSET STR_IN
PUSH AX
CALL SAFE_STRCPY
RET
MAIN_STRCPY ENDP
MAIN_STRSTR PROC NEAR
MOV AX,OFFSET STR_FIND_EXPLAIN
PUSH AX
CALL OUTPUT_STR
MOV AX,OFFSET STR_SRC ;read dst str
PUSH AX
CALL INPUT_STR
MOV AX,OFFSET STR_IN
PUSH AX
MOV AX,OFFSET STR_SRC
PUSH AX
CALL STRSTR
CMP AX,-1
JZ MAIN_STRSTR_NOT_FIND
MOV BX,AX
MOV AX,OFFSET STR_FIND_RESULT_EXPLAIN
PUSH AX
CALL OUTPUT_STR
PUSH BX
CALL OUTPUT16
RET
MAIN_STRSTR_NOT_FIND:
MOV AX,OFFSET STR_NOT_FIND_RESULT_EXPLAIN
PUSH AX
CALL OUTPUT_STR
RET
MAIN_STRSTR ENDP
MAIN_LOWER PROC NEAR
MOV AX,OFFSET STR_IN
PUSH AX
CALL LOWER
RET
MAIN_LOWER ENDP
MAIN_UPPER PROC NEAR
MOV AX,OFFSET STR_IN
PUSH AX
CALL UPPER
RET
MAIN_UPPER ENDP
LOWER PROC NEAR ; [SP+2]ADD of STR \0 end
PUSH SI
PUSH DX
MOV SI,SP
MOV SI,SS:[SI+6]
LOWER_LOOP:
MOV DL,[SI]
CMP DL,0
JZ END_LOWER_LOOP
CMP DL,'A'
JB SKIP_LOWER
CMP DL,'Z'
JA SKIP_LOWER
OR DL,32
MOV [SI],DL
SKIP_LOWER:
INC SI
JMP LOWER_LOOP
END_LOWER_LOOP:
POP DX
POP SI
RET 2
LOWER ENDP
UPPER PROC NEAR ; [SP+2]ADD of STR \0 end
PUSH SI
PUSH DX
MOV SI,SP
MOV SI,SS:[SI+6]
UPPER_LOOP:
MOV DL,[SI]
CMP DL,0
JZ END_UPPER_LOOP
CMP DL,'a'
JB SKIP_UPPER
CMP DL,'z'
JA SKIP_UPPER
AND DL,223
MOV [SI],DL
SKIP_UPPER:
INC SI
JMP UPPER_LOOP
END_UPPER_LOOP:
POP DX
POP SI
RET 2
UPPER 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
STRSTR PROC NEAR ;find pos of str2 in str1; [SP+2]STR2 [SP+4] STR1; return in AX
PUSH SI
PUSH DI
PUSH BX
PUSH CX
PUSH DX
MOV SI,SP
MOV DI,SS:[SI+12];str2
MOV SI,SS:[SI+14];str1
PUSH SI
LOOP_STRSTR:
MOV CX,0
MOV BX,0
LOOP_STRSTR_CMP:
MOV AH,[DI+BX]
MOV AL,[SI+BX]
CMP AH,0
JZ END_LOOP_STRSTR_CMP
XOR AL,AH
OR CL,AL
INC BX
JMP LOOP_STRSTR_CMP
END_LOOP_STRSTR_CMP:
CMP CX,0
JZ END_LOOP_STRSTR
CMP BYTE PTR [SI],0
JZ END_LOOP_STRSTR_NOT_FIND
INC SI
JMP LOOP_STRSTR
END_LOOP_STRSTR_NOT_FIND:
POP BX
MOV AX,-1
JMP END_LOOP_STRSTR_EXIT
END_LOOP_STRSTR:
POP BX
MOV AX,SI
SUB AX,BX
END_LOOP_STRSTR_EXIT:
POP DX
POP CX
POP BX
POP DI
POP SI
RET 4
STRSTR ENDP
OUTPUT16 PROC NEAR ;OUTPUT ONE NUMBER(16) [SP+2] num
PUSH AX
PUSH BX
PUSH CX
PUSH DX
PUSH SI
MOV BX,SP
MOV BX,SS:[BX+12]
MOV CX,4
OUTPUT_LOOP:
MOV DL,BH
SHR DL,1
SHR DL,1
SHR DL,1
SHR DL,1
CMP DL,10
JB OUTPUT_NUM
ADD DL,41H
SUB DL,10
JMP OUTPUT_FIN
OUTPUT_NUM:
ADD DL,30H
OUTPUT_FIN:
MOV AH,02H
INT 21H
SHL BX,1
SHL BX,1
SHL BX,1
SHL BX,1
LOOP OUTPUT_LOOP
POP SI
POP DX
POP CX
POP BX
POP AX
RET 2
OUTPUT16 ENDP
CODE1 ENDS
END MAIN
网友评论