【51汇编示例】 51汇编实现冒泡法;
第一步准备阶段: 准备十个数字
M EQU 10 TAB EQU 30H MOV 30H,#04H MOV 31H,#03H MOV 32H,#05H MOV 33H,#06H MOV 34H,#09H MOV 35H,#08H MOV 36H,#15H MOV 37H,#10H MOV 38H,#16H MOV 39H,#20H
执行完后Memory:
可以看到以30H为头的十个数据成功写入。
之后具体冒泡实现:
MAOPAO:
MOV R0,#09H
LOOP1:
MOV R1,#30H
MOV A,R0
MOV R2,A
LOOP2:
MOV A,@ R1
MOV R3,A
INC R1;
SUBB A,@R1 ; 前减后 看CY位判断大小
JC LOOP3 ; 前减后 看CY位判断大小
MOV A,R3
XCH A,@R1
DEC R1
MOV @ R1,A
INC R1
LOOP3:
DJNZ R2,LOOP2
DJNZ R0,LOOP1
SJMP $
END
执行后效果:
看到已经完成!。
完整程序:
;======================================= ;实验12: 冒泡排序 ;要求: 将依次存储在30H的十个数升序排列 ;======================================= ORG 0000H LJMP MAIN ORG 0040H MAIN: M EQU 10 TAB EQU 30H ;Êý¾Ý×¼±¸ MOV 30H,#04H MOV 31H,#03H MOV 32H,#05H MOV 33H,#06H MOV 34H,#09H MOV 35H,#08H MOV 36H,#15H MOV 37H,#10H MOV 38H,#16H MOV 39H,#20H ;¿ªÊ¼Ã°ÅÝ MAOPAO: MOV R0,#09H LOOP1: MOV R1,#30H MOV A,R0 MOV R2,A LOOP2: MOV A,@ R1 MOV R3,A INC R1; SUBB A,@R1 JC LOOP3 MOV A,R3 XCH A,@R1 DEC R1 MOV @ R1,A INC R1 LOOP3: DJNZ R2,LOOP2 DJNZ R0,LOOP1 SJMP $ END