X86汇编应用(2)

1.STRCPY的汇编实现

DATA	SEGMENT
		STRING1		DB	'I AM A STRING!',0ah,0dh,'$'	;其中0ah,0dh表示回车换行
		STRING2		DB	50	DUP(?)
		MESSAGE1	DB	0ah,0dh,'SOURCE STRING:','$'
		MESSAGE2	DB	0ah,0dh,'DESTINATION STRING:','$'
DATA	ENDS

CODE	SEGMENT
		ASSUME	DS:DATA,CS:CODE
START:	MOV	AX,DATA
		MOV DS,AX
		
		LEA	SI,STRING1		;得到STRING1的段内相对偏移地址
		
		MOV	AX,SEG STRING2
		MOV ES,AX			;将STRING2的段地址给ES
		LEA	DI,STRING2		;得到STRING2的段内相对偏移地址
		
		MOV	CX,17
LOOP1:	MOVSB				;将DS:SI一个字节的数据搬运到ES:DI中
		LOOP	LOOP1
		
		LEA	DX,MESSAGE1
		MOV	AH,09H
		INT 21H
		LEA	DX,STRING1
		MOV	AH,09H
		INT 21H				;打印STRING1的信息
		
		LEA	DX,MESSAGE2
		MOV	AH,09H
		INT 21H
		LEA	DX,STRING2
		MOV	AH,09H
		INT 21H				;打印STRING2的信息
		
		MOV	AH,4CH
		INT 21H
		
CODE	ENDS
		END START
	

程序运行结果:



2.在一段字符串中匹配子串,并替换为另一个子串(两子串长度相同)


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

DATA	SEGMENT
		STRING1		DB	'ZHANGCHONGZHI'				;原有的字符串
		STRING2		DB	'CHONGZHIZHANG'				;替换后的字符串
		STR_LEN		DW	STRING2 - STRING1			;字符串的长度
		RAM_AREA	DB	'LINE 1: ZHANGCHONGZHI IS A STUDENT OF SCSE,BUAA.',0DH,0AH
					DB	'LINE 2: ZHANGCHONGZHI IS A BOY.',0DH,0AH
					DB  'LINE 3: ZHANGCHONGZHI IS INTERESTED IN DEEP LEARNING.',0DH,0AH
					DB	'$'							;查找的内存区域
		AREA_LEN    EQU	$ - RAM_AREA
		AREA_END	DW	$
DATA	ENDS

CODE	SEGMENT
		ASSUME	CS:CODE,DS:DATA,SS:STACK

MAIN    PROC	FAR
		MOV		AX,DATA
		MOV		DS,AX
		MOV		ES,AX
		MOV		AX,STACK
		MOV		SS,AX
		MOV		AX,STACK_BTM
		MOV		SP,AX								;初始化段寄存器
		
		LEA  	DX,RAM_AREA
		MOV		AH,09H
		INT 	21H									;打印替换之前的内存里的内容
		
		CALL	SEARCH_REPLACE						
		
		LEA  	DX,RAM_AREA							;打印替换之后的内存里的内容
		MOV		AH,09H
		INT 	21H
		
		MOV		AH,4CH								
		INT		21H
MAIN	ENDP

SEARCH_REPLACE	PROC
		PUSH	DI
		PUSH	AX
		PUSH	BX
		PUSH	SI									;调用分程序的时候,先将本程序会改变的寄存器原有数据保存到栈里
		
		LEA		DI,RAM_AREA
		LEA		BX,AREA_END
		
LOOP1:	CMP		DI,BX
		JA		PROC_END							;如果将内存区域都扫描过了,就返回主程序
		LEA		SI,STRING1
		MOV		CX,STR_LEN							
		PUSH	DI

LOOP2:	MOV		AL,BYTE PTR [SI]
		CMP		BYTE PTR [DI],AL					;开始逐字节的比较是否匹配原有的字符串
		JNZ		CONTINUE1 							;匹配失败,返回之前的DI的下一个字节重新寻找
		INC		SI
		INC		DI
		LOOP	LOOP2
		LEA		SI,STRING2							
		POP		DI
		CALL	REPLACE
		JMP		CONTINUE2							;匹配成功,替换之后,跳过替换部分,继续开始寻找

CONTINUE1:
		POP		DI
		INC		DI
		JMP		LOOP1
		
CONTINUE2:
		ADD		DI,STR_LEN
		JMP		LOOP1
		
PROC_END:
		POP		SI									
		POP		BX
		POP		AX
		POP		DI									;返回上级程序的时候,将保存的寄存器原有数据从栈里调回寄存器
		RET
		
SEARCH_REPLACE	ENDP

REPLACE	PROC
		PUSH	CX
		CLD
		MOV		CX,STR_LEN
		REP 	MOVSB								;逐字节的替换过程
		POP		CX									
		RET
REPLACE	ENDP

CODE	ENDS
		END	MAIN

程序运行结果:



3.在一段字符串中实现大小写转换


DATA	SEGMENT
		STRING	DB	'THIS is a String.',0DH,0AH,'$'
		LEN		EQU	25
		UPSTRING	DB	50 DUP(?)
		DOWNSTRING	DB	50 DUP(?)
		MSG1	DB	'TO LOWER: $'
		MSG2	DB	'TO UPPER: $'
		MSG3	DB	'ORIGINAL: $'
DATA	ENDS

CODE	SEGMENT
		ASSUME	CS:CODE,DS:DATA
MAIN	PROC	FAR
		MOV		AX,DATA				;初始化
		MOV		DS,AX
		
		LEA		DX,MSG3				;打印源字符串
		MOV		AH,09H
		INT		21H
		LEA		DX,STRING
		MOV		AH,09H
		INT		21H
		
		LEA		DX,MSG2
		MOV		AH,09H
		INT		21H
		
		LEA		SI,STRING
		LEA		DI,UPSTRING
		
		MOV		CX,LEN
LOOP1:	CMP		BYTE PTR DS:[SI],61H	;将源字符串转化成全大写
		JB		NEXT
		CMP		BYTE PTR DS:[SI],7AH
		JA		NEXT
		MOV		AL,BYTE PTR DS:[SI]
		SUB		AL,20H
		MOV		BYTE PTR DS:[DI],AL	
		JMP		NEXT2
NEXT:	MOV		AL,BYTE PTR DS:[SI]
		MOV		BYTE PTR DS:[DI],AL
NEXT2:	INC		SI
		INC		DI
		LOOP	LOOP1
		
		LEA		DX,UPSTRING				;打印全大写的字符串
		MOV		AH,09H
		INT		21H

		LEA		DX,MSG1
		MOV		AH,09H
		INT		21H
		
		LEA		SI,STRING
		LEA		DI,DOWNSTRING
		
		MOV		CX,LEN
LOOP2:	CMP		BYTE PTR DS:[SI],41H	;将源字符串转化成全小写
		JB		NEXT3
		CMP		BYTE PTR DS:[SI],5AH
		JA		NEXT3
		MOV		AL,BYTE PTR DS:[SI]
		ADD		AL,20H
		MOV		BYTE PTR DS:[DI],AL	
		JMP		NEXT4
NEXT3:	MOV		AL,BYTE PTR DS:[SI]
		MOV		BYTE PTR DS:[DI],AL
NEXT4:	INC		SI
		INC		DI
		LOOP	LOOP2
		
		LEA		DX,DOWNSTRING			;打印全小写的字符串
		MOV		AH,09H
		INT		21H
		
		
EXIT:	MOV		AX,4C00H
		INT		21H
MAIN	ENDP
CODE	ENDS
		END 	MAIN

程序运行结果:



4.三个长度相同的字符串根据字典序排序


DATA	SEGMENT
		STRING1	DB	'ABOVE$'
		STRING2	DB	'ABOUT$'
		STRING3	DB	'AGAIN$'
		TEMP_STRING	DB	10	DUP(?)
		MY_ENTER	DB	0DH,0AH,'$'
		STRING_LEN	EQU	6
DATA	ENDS 

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

CODE	SEGMENT
		ASSUME	CS:CODE,DS:DATA,SS:STACK

MAIN	PROC	FAR
		MOV		AX,DATA
		MOV		DS,AX
		MOV		AX,STACK
		MOV		SS,AX
		MOV		AX,STACK_BTM
		MOV		SP,AX								;初始化段寄存器
		
		LEA		SI,STRING1
		LEA		DI,STRING2
		MOV		AX,SEG	STRING1
		MOV		DS,AX
		MOV		AX,SEG	STRING2
		MOV		ES,AX
		MOV		CX,5
		
LOOP1:
		MOV		AL,BYTE PTR DS:[SI]
		CMP		AL,BYTE PTR ES:[DI]
		JA		NEED_SWAP1
		JB		NO_NEED_SWAP1
		INC		SI
		INC		DI
		LOOP	LOOP1	
		
NEED_SWAP1:	
		LEA		SI,STRING1
		LEA		DI,STRING2
		LEA		DX,TEMP_STRING
		MOV		BX,SEG	TEMP_STRING		
		CALL	SWAP
		
		
NO_NEED_SWAP1:		
		LEA		SI,STRING1
		LEA		DI,STRING3
		MOV		AX,SEG	STRING1
		MOV		DS,AX
		MOV		AX,SEG	STRING3
		MOV		ES,AX
		MOV		CX,5

		
LOOP2:		
		MOV		AL,BYTE PTR DS:[SI]
		CMP		AL,BYTE PTR ES:[DI]
		JA		NEED_SWAP2
		JB		NO_NEED_SWAP2
		INC		SI
		INC		DI
		LOOP	LOOP2

NEED_SWAP2:
		LEA		SI,STRING1
		LEA		DI,STRING3
		LEA		DX,TEMP_STRING
		CALL	SWAP
		
NO_NEED_SWAP2:
		LEA		SI,STRING2
		LEA		DI,STRING3
		MOV		AX,SEG	STRING2
		MOV		DS,AX
		MOV		AX,SEG	STRING3
		MOV		ES,AX
		MOV		CX,5		

LOOP3:		
		MOV		AL,BYTE PTR DS:[SI]
		CMP		AL,BYTE PTR ES:[DI]
		JA		NEED_SWAP3
		JB		NO_NEED_SWAP3
		INC		SI
		INC		DI
		LOOP	LOOP3

NEED_SWAP3:
		LEA		SI,STRING2
		LEA		DI,STRING3
		LEA		DX,TEMP_STRING
		CALL	SWAP

NO_NEED_SWAP3:
		MOV		AX,SEG	STRING1
		MOV		DS,AX
		LEA		DX,STRING1
		MOV		AH,09H
		INT		21H
		
		MOV		AX,SEG	MY_ENTER
		MOV		DS,AX
		LEA		DX,MY_ENTER
		MOV		AH,09H
		INT		21H
		
		MOV		AX,SEG	STRING2
		MOV		DS,AX
		LEA		DX,STRING2
		MOV		AH,09H
		INT		21H

		MOV		AX,SEG	MY_ENTER
		MOV		DS,AX
		LEA		DX,MY_ENTER
		MOV		AH,09H
		INT		21H
		
		MOV		AX,SEG	STRING3
		MOV		DS,AX
		LEA		DX,STRING3
		MOV		AH,09H
		INT		21H

EXIT:	MOV		AX,4C00H
		INT		21H
		
MAIN	ENDP

SWAP	PROC		

		
		PUSH	AX			;默认第一个STRING在DS:SI,第二个STRING在ES:DI,TEMP在DS:DX中
		PUSH	CX
		MOV		CX,STRING_LEN
SWAP1:
		MOV		AL,BYTE PTR DS:[SI]		;第一个串复制到temp中
		XCHG	DX,SI
		MOV		BYTE PTR DS:[SI],AL
		XCHG	DX,SI
		INC		SI
		INC		DX
		LOOP	SWAP1
		SUB		SI,STRING_LEN
		SUB		DX,STRING_LEN

		MOV		CX,STRING_LEN
SWAP2:
		MOV		AH,BYTE PTR ES:[DI]		;第二个串复制到第一个中
		MOV		BYTE PTR DS:[SI],AH
		INC		DI
		INC		SI
		LOOP	SWAP2
		SUB		DI,STRING_LEN
		SUB		SI,STRING_LEN
	
		MOV		CX,STRING_LEN
SWAP3:
		XCHG	SI,DX
		MOV		AL,BYTE PTR DS:[SI]		;temp串复制到第二个中
		MOV		BYTE PTR ES:[DI],AL
		XCHG	SI,DX
		INC		DI
		INC		DX
		LOOP	SWAP3
		SUB		DI,STRING_LEN
		SUB		DX,STRING_LEN

		
		POP		CX
		POP		AX	
		
		RET
SWAP	ENDP

CODE	ENDS
		END		MAIN

程序运行结果:


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_41400276/article/details/80322097
个人分类: X86汇编
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭