exit: lea dx,crlf mov ah,9 int 21h lea dx,h2 int 21h
mov si,0 mov bl,10 mov ax,cx
l4: div bl
mov dl,ah mov dh,0 push dx inc si mov ah,0 cmp al,0 jnz l4
l5: pop dx add dl,30h mov ah,2 int 21h dec si cmp si,0 jnz l5
mov ah,4ch int 21H
main endp s3 ends
end main
11.有一个首地址为mem的10个字的数组,试编程序删除数组中所有为零的项,并将后续项向前压缩,最后将数组的剩余部分补上零
s1 segment stack
dw 100h dup(?) top label word s1 ends
s2 segment
mem dw 0,1,0,3,0,0,4,5,6,0,'$' crlf db 0dh,0ah,24h s2 ends
s3 segment
assume cs:s3,ds:s2,ss:s3 main proc far mov ax,s1 mov ss,ax lea sp,top mov ax,s2 mov ds,ax mov si,0 mov di,2
repeat: cmp di,20 jz exit mov bx,mem[si] mov dx,mem[di] cmp bx,0 jnz next xchg bx,dx mov mem[si],bx mov mem[di],dx
next:cmp mem[si],0 jz l
add si,2 l: add di,2 jmp repeat
exit: mov ah,4ch int 21H
main endp s3 ends
end main ;以下是该算法描述
;定义两个指针,当前指针si和检查指针di,先把si指针指向第一个字得到的值bx, ;di指向第二个字得到的值dx。若bx为0,di自加2,否则,则交换这两字,
;若此时si指向字为0,则di, 否则,di,si都自己加2。如此循环直到di指向最后一个字。 ;这样就把所有非零字至于前面了。
12.从键盘上输入一串字符(用回车键结束,使用10号功能调用)放在string中,是编制一个程序测试字符串中是否存在数字。如有,则把cl的第五位置1,否则将该位置置0
s1 segment stack
dw 100h dup(?) top label word s1 ends
s2 segment
h1 db 'Please input a string: ','$' h2 db 'cx: ','$' crlf db 0dh,0ah,24h string db 50,?,50 dup('$') s2 ends
s3 segment
assume cs:s3,ds:s2,ss:s3 main proc far mov ax,s1 mov ss,ax lea sp,top mov ax,s2 mov ds,ax and cl,0dfh ;先假设无数字,置0 lea dx,h1 mov ah,9 int 21h
lea dx,string mov ah,10d int 21h mov si,2
l2: cmp string[si],'$' jz exit mov dl,string[si] cmp dl,30h jb l cmp dl,39h jnb l or cl,20h ;有数字,置1 jmp exit
l: inc si jmp l2
exit: lea dx,crlf mov ah,9 int 21h lea dx,h2 int 21h mov ax,cx call print jmp atend
print proc near
mov cl,10 mov si,0
repeat: div cl
mov dl,ah add dl,30h
mov dh,0 push dx inc si mov ah,0
cmp al,0 jnz repeat
l3: pop dx mov ah,2 int 21h dec si cmp si,0 jnz l3
ret print endp
atend: mov ah,4ch int 21H
main endp s3 ends
end main
13.在首地址为data的字数组中,存放了10个16位字数据,试编写一个程序,求出平均值放在ax寄存器中,并求出数组中有多少个数小于此平均值,将结果放在bx寄存器中(f分别考虑有符号数、无符号数情况)
s1 segment stack
dw 100h dup(?) top label word s1 ends
s2 segment
data dw -1,0,1,-2,2,3,-3,-4,4,15,'$' crlf db 0dh,0ah,24h s2 ends
s3 segment
assume cs:s3,ds:s2,ss:s3 main proc far mov ax,s1 mov ss,ax lea sp,top mov ax,s2 mov ds,ax mov si,0 mov ax,0
l3: cmp data[si],'$' jz exit mov dx,data[si] cmp dx,0 jnl l1 neg dx sub ax,dx jmp l2 l1: add ax,dx l2: add si,2 jmp l3
exit: mov cl,10 ;平均值放到ax中 div cl mov ah,0 mov si,0