Example: Write a subroutine that appends the string 2 after the string 1. The folded string ends with 0
Dseg SEGMENT BUFFER1 db ' Hello World ', 0 db DUP (0) BUFFER2 DB ' Welcome!!! ', 0 dseg ENDS cseg SEGMENT as
SUME ds:dseg,cs:cseg Start:mov ax,dseg MOV ds,ax;
MOV si,offset BUFFER1 call dmess call NEWLINE; display buff1 information;
MOV si,offset BUFFER2 call dmess call NEWLINE; display BUFF2 information; mov si,offset BUFFER1 mov di,offset BUFFER2 call STRCAT; Add string 2 to string 1 mov si,offset BUFFER1 call DM ESS; Display the string after add call NEWLINE MOV ax,4c00h INT 21H; subroutine name: dmess; function: Displays a string ending with 0; entry parameter: SI = display The first address of the string; Export parameters: No dmess PROC Dmess1:mov Dl,[si] INC SI or DL,DL; or operation enables CF=0,ZF to reflect results JZ DMESS2 MOV ah,2 INT 21
H JMP DMESS1 dmess2:ret dmess ENDP NEWLINE PROC push AX push DX mov dl,0dh mov ah,2 INT 21H mov dl,0ah MOV ah,2 INT 21H pop DX pop AX RET NEWLINE ENDP; subroutine name: STRCAT; function: At the end of the string 1, add the string 2; entry parameter: Ds:si = string 1 The segment value of the actually address: Offset Ds:di = The segment value of the starting address of string 2: offset; exit parameters: none; say Clear: Do not consider whether to leave enough space after the string 1 STRCAT PROC push ES push AX push CX push SI push DI CLD; direction flag bit push for clear DF DS POP es; make es equal to DS PUSH di; save di MOV di,si; convert SI XOR al,al; empty Al, Make Al = 0 MOV cx,0ffffh repnz S
CASB; Scans the string that Si points at, and points DI to the end of the string, LEA si,[di-1]; points si to the last character in the string, not 0 POP DI; MOV cx,0ffffh repnz SCASB not CX; Confirm the length of the string in SUB di,cx; place DI, re-point to the beginning of the string XCHG Si,di; swap SI with DI T EST si,1; Determines whether the address of the string that SI points to is an odd address JZ STRCAT1 MOVSB; if it is an odd address, first save a byte DEC CX strcat1:shr cx,1 REPZ
MOVSW JNC STRCAT2 movsb strcat2:pop DI pop SI pop CX pop AX pop ES RET STRCAT ENDP cseg ENDS END START