输入输出
DATAS SEGMENT
arr dw 20 dup ( ? )
num db 0
string1 db 'input the number of interger:(please < 20)' , 13 , 10 , '$'
string2 db 'input the interger:' , 13 , 10 , '$'
string3 db 13 , 10 , 'the intergers you input is that:' , 13 , 10 , '$'
DATAS ENDS
STACKS SEGMENT
STACKS ENDS
CODES SEGMENT
ASSUME CS: CODES, DS: DATAS, SS: STACKS
START:
MOV AX, DATAS
MOV DS, AX
; 输出提示语
lea dx, string1
mov ah, 9
int 21 h
; 输入要输入数字的 个数
mov ah, 1
int 21 h
sub al, '0'
mov num, al
mov ah, 1 ; 这是为了清除个数之后的回车
int 21 h
; 输出提示语
lea dx, string2
mov ah, 9
int 21 h
; 输入 cx存放要输入几个数
mov si, 0
mov ch, 0
mov cl, num
l:
call scanm; 出口参数: bx
mov arr[ si] , bx
add si, 2
loop l
; 输出提示语
lea dx, string3
mov ah, 9
int 21 h
; 输出 cx存放要输出几个数
mov ch, 0
mov cl, num
mov si, 0
l2:
mov ax, arr[ si]
call printm; 入口参数ax
; 每次输出之间有个空格
mov dl, ' '
mov ah, 2
int 21 h
;
add si, 2
loop l2
MOV AH, 4 CH
INT 21 H
; 从键盘输入数-> bx
scanm PROC
push ax
push cx
push dx
; 暂存的数放在bx中 第一步要置0
mov bx, 0
inbegin:
mov ah, 1 ; 输入一个字符到al中
int 21 h
cmp al, ' '
je inend
cmp al, 13
je inend; 如果这个数不是空格 回车 就把他加到暂存数中去
sub al, '0'
mov cl, al; 新输入的数放在cl中
; bx * 10 + cl
mov dl, 10
mov ax, bx
mul dl; ans ax
mov bx, ax
mov ch, 0
add bx, cx
jmp inbegin
inend:
pop dx
pop cx
pop ax
ret
scanm ENDP
; 从 ax中取数打印到屏幕上
printm proc
push ax
push cx
push dx
; do while 除法
; dh 10
; dl cnt
mov dh, 10
mov dl, 0
divbegin:
div dh
push ax; ah 余数
mov ah, 0
inc dl
cmp al, 0
jne divbegin
mov dh, dl; dh cnt
print_begin:
cmp dh, 0
je printm_end
pop cx
dec dh
mov dl, ch
add dl, '0'
mov ah, 2
int 21 h
jmp print_begin
printm_end:
pop dx
pop cx
pop ax
ret
printm endp
CODES ENDS
END START
输出2/16进制
DATAS SEGMENT
DATAS ENDS
STACKS SEGMENT
; 此处输入堆栈段代码
STACKS ENDS
CODES SEGMENT
ASSUME CS: CODES, DS: DATAS, SS: STACKS
START:
MOV AX, DATAS
MOV DS, AX
; ax 存要输出的数字 cl 存要输出进制 ( 2 or 16 )
mov ax, 0 aaaah
mov cl, 16
call print_x_radix
mov cl, 2
call print_x_radix
MOV AH, 4 CH
INT 21 H
; num-> ax x_radix-> cl
print_x_radix proc
push ax
push bx
push cx
push dx
; bx 存的是要与的数
; 这里假设只输出2 进制或者16 进制
cmp cl, 2
jne L0
mov cl, 1
mov bx, 0001 h
jmp L01
L0:
mov cl, 4
mov bx, 000f h
L01:
; 先算一下输出几次
push ax
mov ax, 16
div cl
mov ch, al ; ch 要输出几次
pop ax
; 移位
L1: rol ax, cl
push ax
and ax, bx
; 输出ax
; 分情况 如果<= 9 就直接输出 否则输出字母
cmp al, 9
ja L2
mov dl, al
add dl, '0'
mov ah, 2
int 21 h
jmp L21
L2:
mov dl, al
add dl, 55
mov ah, 2
int 21 h
L21:
pop ax
dec ch
cmp ch, 0
ja L1
cmp cl, 1
jne L3
mov dl, 'B'
jmp L31
L3:
mov dl, 'H'
L31:
mov ah, 2
int 21 h
mov dl, 10
mov ah, 2
int 21 h
pop dx
pop cx
pop bx
pop ax
ret
print_x_radix endp
CODES ENDS
END START
冒泡排序
DATAS SEGMENT
arr dw 20 dup ( ? )
string1 db 'input 10 nums' , 13 , 10 , '$'
string2 db 'before sort' , 13 , 10 , '$'
string3 db 13 , 10 , 'after sort:' , 13 , 10 , '$'
DATAS ENDS
STACKS SEGMENT
; 此处输入堆栈段代码
STACKS ENDS
CODES SEGMENT
ASSUME CS: CODES, DS: DATAS, SS: STACKS
START:
MOV AX, DATAS
MOV DS, AX
; 输出提示语1
lea dx, string1
mov ah, 9
int 21 h
; 循环输入
; cx 次数 10 个数
mov si, 0
mov cx, 10
l:
call scanm
mov arr[ si] , bx
add si, 2
loop l
; 输出提示语2
lea dx, string2
mov ah, 9
int 21 h
; 输出没排序的
mov cx, 10
mov si, 0
l3:
mov ax, arr[ si]
call printm
mov dl, ' '
mov ah, 2
int 21 h
add si, 2
loop l3
; 排序
; bx 数组的首地址-> bx 元素的个数-> cl
lea bx, arr
mov cl, 10
call bubble_sort
; 输出提示语3
lea dx, string3
mov ah, 9
int 21 h
; 输出结果
mov cx, 10
mov si, 0
l2:
mov ax, arr[ si]
call printm
mov dl, ' '
mov ah, 2
int 21 h
add si, 2
loop l2
MOV AH, 4 CH
INT 21 H
; for ( int i= 0 ; i< n- 1 ; ++ i) 循环n- 1 次
; for ( int j= 0 ; j< n- i- 1 ; ++ j)
; if ( a[ j] > a[ j+ 1 ] )
; swap ( a[ j] , a[ j+ 1 ] ;
; 首地址-> bx 数据: word 个数-> cl 按照升序
bubble_sort proc
push ax
push bx
push cx
push dx
push si
push di
; dl i dh j cl n
mov dl, 0
; cl -> n- 1
; ch -> n- 1 - i
sub cl, 1
i_begin:
cmp dl, cl; n- 2
je i_over
mov dh, 0
j_begin:
; n- 2 - dl
mov ch, cl
sub ch, dl
cmp dh, ch
je j_over
push dx
mov dl, dh; j-> dl
mov dh, 0 ; j-> dx
mov si, dx
add si, si; si= 2 * j
add si, bx; si= 2 * j+ bx
mov di, si; di= si
add di, 2 ; di= si+ 2 di
pop dx
; cmp [ si] [ di]
mov ax, [ si]
cmp ax, [ di]
jna swap_over
xchg ax, [ di]
mov [ si] , ax
swap_over:
inc dh
jmp j_begin
j_over:
inc dl
jmp i_begin
i_over:
pop di
pop si
pop dx
pop cx
pop bx
pop ax
ret
bubble_sort endp
; 接受输入到 bx中
scanm PROC
; 大bug
mov bx, 0
; push
push ax
push cx
push dx
inbegin:
mov ah, 1
int 21 h
cmp al, ' '
je inend
cmp al, 13
je inend
; cmp al, '9'
; ja inend
sub al, '0'
mov cl, al; cl < - al
; bx * 10 + cl
mov dl, 10
mov ax, bx
mul dl; ans ax
mov bx, ax
mov ch, 0
add bx, cx
jmp inbegin
inend:
pop dx
pop cx
pop ax
ret
scanm ENDP
printm proc
push ax
push cx
push dx
; in: ax
; do while 除法
; dh 10
; dl 计数器 cnt
mov dh, 10
mov dl, 0
divbegin:
div dh
push ax; ah 余数
mov ah, 0
inc dl
cmp al, 0
jne divbegin
mov dh, dl; dh cnt
print_begin:
cmp dh, 0
je printm_end
pop cx
dec dh
mov dl, ch
add dl, '0'
mov ah, 2
int 21 h
jmp print_begin
printm_end:
pop dx
pop cx
pop ax
ret
printm endp
CODES ENDS
END START
判断正数负数零
DATAS SEGMENT
; 注意溢出
table db - 134 , 98 , - 98 , 158 , 10 , 133 , - 45 , 66 , - 134 , - 122 , 0 , - 33 , 0 , - 99 , 0
string1 db 'positive:' , 13 , 10 , '$'
string2 db 13 , 10 , 'negetive:' , 13 , 10 , '$'
string3 db 13 , 10 , 'zero:' , 13 , 10 , '$'
DATAS ENDS
STACKS SEGMENT
; 此处输入堆栈段代码
STACKS ENDS
CODES SEGMENT
ASSUME CS: CODES, DS: DATAS, SS: STACKS
START:
MOV AX, DATAS
MOV DS, AX
mov cx, 15
mov si, 0
mov al, 0 ; 正数
mov ah, 0 ; 负数
mov bl, 0 ; 0
l:
mov bh, table[ si]
add bh, 0
js nega_p
jz zero_p
; 否则是正数
inc al
jmp myend
nega_p:
inc ah
jmp myend
zero_p:
inc bl
jmp myend
myend:
inc si
loop l
lea dx, string1
push ax
mov ah, 9
int 21 H
pop ax
push ax
mov ah, 0
; 输出al
call printm
lea dx, string2
mov ah, 9
int 21 H
pop ax
mov al, ah
mov ah, 0
; 输出ah
call printm
lea dx, string3
mov ah, 9
int 21 H
mov ah, 0
mov al, bl
; 输出bl
call printm
MOV AH, 4 CH
INT 21 H
printm proc
push ax
push cx
push dx
; in: ax
; do while 除法
; dh 10
; dl 计数器 cnt
mov dh, 10
mov dl, 0
divbegin:
div dh
push ax; ah 余数
mov ah, 0
inc dl
cmp al, 0
jne divbegin
mov dh, dl; dh cnt
print_begin:
cmp dh, 0
je printm_end
pop cx
dec dh
mov dl, ch
add dl, '0'
mov ah, 2
int 21 h
jmp print_begin
printm_end:
pop dx
pop cx
pop ax
ret
printm endp
CODES ENDS
END START
以有符号数输出
DATAS SEGMENT
string1 db 'input num' , 13 , 10 , '$'
DATAS ENDS
STACKS SEGMENT
; 此处输入堆栈段代码
STACKS ENDS
CODES SEGMENT
ASSUME CS: CODES, DS: DATAS, SS: STACKS
START:
MOV AX, DATAS
MOV DS, AX
mov bx, - 2559
call printneg
MOV AH, 4 CH
INT 21 H
printneg PROC ; bx
add bx, 0
jns printneg_abolute; 正数
mov dl, '-'
mov ah, 2
int 21 h
neg bx
printneg_abolute:
mov ax, bx
call printm
ret
printneg ENDP
printm proc
push ax
push cx
push dx
; do while 除法
; dh 10
; dl cnt
mov dh, 10
mov dl, 0
divbegin:
div dh
push ax; ah 余数
mov ah, 0
inc dl
cmp al, 0
jne divbegin
mov dh, dl; dh cnt
print_begin:
cmp dh, 0
je printm_end
pop cx
dec dh
mov dl, ch
add dl, '0'
mov ah, 2
int 21 h
jmp print_begin
printm_end:
pop dx
pop cx
pop ax
ret
printm endp
CODES ENDS
END START