简介
将一个内存区域的字符串复制到另一内存区域(允许两区域存在任意方式重合部分)
程序运行结果
- src在前,dst在后且存在重合
运行前内存情况(红色位置是水印)
image.png
运行后内存情况
image.png
-
dst在前,src在后且存在重合
image.png
运行前内存情况
image.png
运行后内存情况
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
SYMBOL DB 15H,23H,10H,44H
BLANK1 DB 10H DUP(0)
SRC_STR DB "HELLO_WORLD",0
DST_STR EQU SRC_STR-2
BLANK2 DB 10H DUP(0)
;SRC_STR DB 50H DUP(?)
;DST_STR DB 50H DUP(?)
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 SRC_STR
; PUSH AX
; CALL INPUT_STR
MOV AX,OFFSET DST_STR
PUSH AX
MOV AX,OFFSET SRC_STR
PUSH AX
CALL SAFE_STRCPY
EXIT: MOV AX,4C00H
INT 21H
MAIN ENDP
INPUT_STR PROC NEAR ; [SP+2]ADDRESS of DST_STR
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
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
;判断目的地址和源地址的关系,(dst>src ==>从高字节到低字节拷贝)|(dst<=src ==>从低字节到到高字节拷贝)
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
网友评论