X86汇编应用(1)

1.冒泡排序实现降序排序,并实现十进制和十六进制的输出

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

MY_DATA		SEGMENT
TABLE_LEN	DW		16
TABLE   	DW		200,300,400,10,20,0,1,8
			DW		41H,40,42H,50,60,0FFFFH,2,3
MY_DATA		ENDS

MY_CODE		SEGMENT
			ASSUME	CS:MY_CODE,DS:MY_DATA,SS:MY_STACK
MAIN		PROC	FAR			;sort(task1)
			MOV		AX,MY_STACK
			MOV		SS,AX
			MOV		AX,MY_DATA
			MOV  	DS,AX
			MOV		SP,STACK_BTM
			
LOOP1:		MOV		BX,1
			MOV		CX,TABLE_LEN
			DEC		CX
			MOV		SI,OFFSET TABLE

LOOP2:		MOV		AX,[SI]
			CMP		AX,[SI+2]
			JA		CONTINUE
			XCHG	AX,[SI+2]
			MOV		[SI],AX		;change two numbers in memory
			MOV		BX,0

CONTINUE:	ADD		SI,2
			LOOP 	LOOP2
			CMP		BX,1
			JZ		TURN_TO_HEX
			JMP		SHORT LOOP1
			
TURN_TO_HEX:					;print the array in hex(task2)
			MOV		CX,TABLE_LEN
			MOV		BX,0

LOOP3:		MOV		SI,OFFSET TABLE
			ADD		SI,BX
			MOV		AX,[SI]
			MOV		SI,0
			MOV		DI,0010H
	
LOOP4:		MOV		DX,0
			DIV		DI
			CMP		DX,10
			JAE		BRANCH1
			ADD		DX,30H
			JMP		BRANCH2

BRANCH1:	ADD		DX,57H

BRANCH2:	PUSH	DX
			INC		SI		
			CMP		AX,0
			JNZ		LOOP4

LOOP5:		POP		DX
			MOV		AH,2
			INT		21H
			DEC		SI
			CMP		SI,0
			JNZ		LOOP5
			MOV		DL,32
			MOV		AH,2
			INT		21H
			ADD		BX,2
			LOOP	LOOP3
			JMP		EXIT			

TURN_TO_DEC:					;print the array in dec(task3)
			MOV		CX,TABLE_LEN
			MOV		BX,0

LOOP6:		MOV		SI,OFFSET TABLE
			ADD		SI,BX
			MOV		AX,[SI]
			MOV		SI,0
			MOV		DI,000AH
			
LOOP7:		MOV		DX,0
			DIV		DI
			ADD		DX,30H
			PUSH	DX
			INC		SI
			CMP		AX,0
			JNZ		LOOP7
			
LOOP8:		POP		DX
			MOV		AH,2
			INT		21H
			DEC		SI
			CMP		SI,0
			JNZ		LOOP8
			MOV		DL,32
			MOV		AH,2
			INT		21H
			ADD		BX,2
			LOOP	LOOP6
			JMP		EXIT
			
EXIT:		MOV		AX,4C00H
			INT		21H
MAIN		ENDP
MY_CODE		ENDS
			END    	MAIN

程序实现的效果如下图(在DOSBOX中编译并运行):


                       十六进制输出结果


                            十进制输出结果

2.两个三十二位的数字的乘法实现


DATA	SEGMENT
		X		DD	0FFFFFFFFH
		Y		DD	0FFFFFFFFH;申请两个双字,作为乘法所需的两个乘数
		RESULT	DW	0,0,0,0   ;申请四个字的空间,存放结果
DATA	ENDS

CODE	SEGMENT
		ASSUME	CS:CODE,DS:DATA
MAIN	PROC	FAR
		MOV		AX,DATA
		MOV		DS,AX
		
		MOV		BX,WORD PTR Y
		MOV		AX,WORD	PTR	X;先将两个乘数的低位进行乘法
		MUL		BX
		MOV		RESULT,AX
		MOV		RESULT+2,DX
		
		MOV		BX,WORD	PTR	Y
		MOV		AX,WORD PTR X+2;之后两步,是分别用一个数的高位乘另一个的低位
		MUL		BX
		ADD		RESULT+2,AX
		ADC		RESULT+4,DX
		ADC		RESULT+6,0
		
		MOV		BX,WORD	PTR	Y+2
		MOV		AX,WORD PTR X
		MUL		BX
		ADD		RESULT+2,AX
		ADC		RESULT+4,DX
		ADC		RESULT+6,0
		
		MOV		BX,WORD	PTR	Y+2
		MOV		AX,WORD PTR X+2;最后将高位相乘
		MUL		BX
		ADD		RESULT+4,AX
		ADC		RESULT+6,DX
		
		MOV 	AX,4C00H;退出
		INT		21H
		
MAIN	ENDP
CODE	ENDS
		END 	MAIN

程序实现的效果如下图(在DOSBOX中编译并运行):


你可能感兴趣的:(X86汇编)