冒泡排序
DATAS SEGMENT
TIELE DB "-----BUBBLE SORT-----",10,13,"-2016110131S-",10,13,"ARRAY[6]={1,7,9,2,6,5}",10,13,"After:","$"
BLANK DB " $"
ARR DW 1,7,9,2,6,5
CNT EQU $-ARR
DATAS ENDS
STACKS SEGMENT
DB 256 DUP()
STACKS ENDS
CODES SEGMENT
ASSUME CS:CODES,DS:DATAS,SS:STACKS
START:
MOV AX,DATAS
MOV DS,AX
MOV AH,09
MOV DX,OFFSET TIELE ;打印标题
INT 21H
MOV CX,CNT ;将循环计数器CX设为数组大小
DEC CX ;CX--
OUTER: MOV DI,CX ;保存CX
MOV BX,0 ;int BX=0
INNER: MOV AX,ARR[BX]
CMP AX,ARR[BX+2] ;if(ARR[BX]<ARR[BX+2])
JGE COUNT ;>=
XCHG AX,ARR[BX+2]
MOV ARR[BX],AX ;SWAP(ARR[BX],ARR[BX+2])
COUNT: ADD BX,2 ;BX+2
LOOP INNER ;内层循环
MOV CX,DI ;内层循环结束 回复CX
LOOP OUTER ;外层熏循环
CALL DISPA
MOV AH,4CH
INT 21H
;输出数组子程序
;子程序名:DISPA
;变量参数: CNT(记录数组长度)
; ARR[](数组)
DISPA PROC
SAVE: PUSH CX ;保存寄存器
PUSH DX
MOV CX,CNT-5
MOV DI,-2
DISPL: ADD DI,2 ;DI++
MOV DX,ARR[DI] ;输出ARR[DI]
ADD DL,30H
MOV AH,02H
INT 21H
MOV AH,09
MOV DX,OFFSET BLANK ;打印空格
INT 21H
LOOP DISPL
RECOV: POP DX
POP CX
EXIT: MOV AH,4CH ;恢复寄存器
INT 21H
DISPA ENDP
CODES ENDS
END START
双精度加法
data segment
;定义数据
X Db 0001H,0001H
Y Db 0003H,0001H
Z Db 0000H,0000H
;定义字符串
string DB 'Calculate Result =',13,10,'$'
data ends
code segment
start:
mov ax,data
mov ds,ax
;X Y Z加法
MOV Al,X
ADD Al,Y
MOV Z,Al
;计算结果文字
LEA DX,STRING
MOV AH,9
INT 21H
;高位加法
MOV Ah,X+1
ADC Ah,Y+1
MOV Z+1,Ah
;输出高位
mov dl,z+1
add dl,30h
mov ah,02
int 21h
;输出小数点
mov dl,'.'
mov ah,02
int 21h
;输出低位
mov dl,z
add dl,30h
mov ah,02
int 21h
;等待输入
mov ah,1
int 21h
mov ax,4ch
int 21h
code ends
end start
串拷贝
;串拷贝 源串位于数据段
DATAS1 SEGMENT
str1 DB 'HELLO WORLD!$'
DATAS1 ENDS
DATAS2 SEGMENT
str2 DB 20 dup(?)
DATAS2 ENDS
CODES SEGMENT
ASSUME CS:CODES,DS:DATAS1,ES:DATAS2
START:
MOV AX,DATAS1
MOV DS,AX
MOV AX,DATAS2
MOV ES,AX
lea si,str1 ;DS->si
lea di,str2 ;ES->di
mov cx,20
cld
rep movsb
lea dx,str2
mov ax,es
mov ds,ax ;es->dx?
MOV AH,9
INT 21H
MOV AH,4CH
INT 21H
CODES ENDS
END START
;串拷贝 源串位于附加段
DATAS2 SEGMENT
str1 DB 'Hello world$'
str2 DB 20 dup(?)
DATAS2 ENDS
CODES SEGMENT
ASSUME CS:CODES,ES:DATAS2
START:
MOV AX,DATAS2
MOV ES,AX
lea si,str1 ;source -> si
lea di,str2 ;destination -> di
mov cx,20
cld
rep movs es:byte ptr[di],es:[si]
lea dx,str2
;DONT KNOW WHAT
;消除乱码?
mov ax,es
mov ds,ax
;OUTPUT
MOV AH,9
INT 21H
MOV AH,4CH
INT 21H
CODES ENDS
END START