24个汇编实例小程序

更新时间:2024-01-28 01:14:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

24个汇编小程序

题目列表:

逆序输出字符串“BASED ADDRESSING”

从键盘上输入两个数,分别放到x,y单元,求出它们的和

试编写一段程序,要求在长度为10h的数组中,找出大于42h的无符号数的个数并存入地址为up开始区域,找出小于42h的无符号数的个数并存入地址为down的开始区域

键盘输入一段字符串,其中小写字母以大写字母输出,其他字符不变输出

从键盘上就收一个小写字母,找出它的前导字符和后续字符,在顺序显示这三个字符

把一个包含20个数据的数组M分成两组:正整数组P和负整数组N,分别把这两个数组中的数据的个数显示出来

求出首地址为data的100个字数组中的最小偶数,并把它放在ax中

输入两船字符串string1和string2,并比较两个字符串是否相等,相等就显示“match”,否则显示“no match”

从键盘接收一个四位的十六进制数,并在终端显示与它等值的二进制数

从键盘输入一系列以$为结束符的字符串,然后对其中的非数字字符计数,并显示计数结果

有一个首地址为mem的100个字的数组,试编程序删除数组中所有为零的项,并将后续项向前压缩,最后将数组的剩余部分补上零

从键盘上输入一串字符(用回车键结束,使用10号功能调用)放在string中,是编制一个程序测试字符串中是否存在数字。如有,则把cl的第五位置1,否则将该位置置0

在首地址为data的字数组中,存放了100h的16位字数据,试编写一个程序,求出平均值放在ax寄存器中,并求出数组中有多少个数小于此平均值,将结果放在bx寄存器中(f分别考虑有符号数、无符号数情况)

一直数组A包含15个互不相等的整数,数组B包含20个互不相等的整数。试编制一个程序,把既在A中又在B中出现的整数存放于数组C中

设在A、B和D单元中分别存放着三个数。若三个数都不是0,则求出三个数的和并存放在S单元,若其中有一个数为0,则把其它两个单元也清零。请编写此程序

从键盘输入一系列字符(以回车键结束),并按字母、数字和其他字符分类计数,最后显示这三类的计数结果

已定义两个整数变量A和B,试编写程序完成以下功能

(1)若两个树种有一个是奇数,则将奇数存入A中,偶数存入B中 (2)若两个数均为奇数,则将两个数加1后存回原变量 (3)若两个数均为偶数,则两个变量均不变

写一段子程序skiplines,完成输出空行的功能。空行的行数由用户在主程序中通过键盘输入,并将行数放在ax寄存器中

设有10个学生成绩分别是76, 69,84,73,88,99,63,100和80。试编写一个子程序统计60-69分,70-79分,80-89分,90-99分和100分的人数,并分别放到S6,S7,S8,S9,S10单元中

编写子程序嵌套结构的程序,把整数分别用二进制和八进制显示出来

在D盘根目录建立一个文件abc.txt,第一次向文件写入“123456”六个字符,第二次增加“abcdefg”几个字符

从键盘上输入文本文件:“d:\\temp.txt”的内容后,然后新建一个文件“d:\\temp2.txt”,把前一个文件的所有内容复制到后一个文件中

从键盘上输入一个十进制数,以十六进制数显示出来。要求子程序用寄存器参数传送方法

试编制一个程序,把bx寄存器中的二进制数用十六进制数的形式在屏幕上显示出来

代码:

1.逆序输出字符串“BASED ADDRESSING”

s1 segment stack ;定义栈段s1

dw 100 dup(?) ;定义栈空间为100 top label word ;top指向栈顶 s1 ends

s2 segment ;定义数据段s2 s db 'BASED ADDRESSING','$' ;定义字符串s S2 ends

s3 segment ;定义代码段s3 assume cs:s3,ds:s2,ss:s1

main proc far

mov ax,s1 ;栈初始化—— mov ss,ax lea sp,top ;——栈初始化

mov ax,s2 ;数据段初始化—— mov ds,ax ;——数据段初始化

mov si,15

l: mov dl,s[si] ;dl获取字符串s的最后一个(从零开始的第十五个字符) mov ah,2 ;调用int 21h 2号功能输出dl上的值 int 21h

dec si ;寄存器减一,准备获取下一个字符 cmp si,0 ja l

mov ah,4ch ;终止 int 21h main endp s3 ends

end main

2.从键盘上输入两个数,分别放到x,y单元,求出它们的和

s1 segment stack

dw 100h dup(?) top label word s1 ends

s2 segment

h1 db 'Please input x:','$' ;提示输入 h2 db 'Please input y:','$' ;提示输入 h3 db 'z=x+y:','$' ;提示输出

crlf db 0dh,0ah,24h ;定义回车换行 x dw ? y dw ? 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 ;——初始化

lea dx,h1 ;int 21h 9号功能输出“提示输入x”的字符串 mov ah,9 int 21h

xor bx,bx ;bx清零,即把bx置零

InputX: mov ah,1 ;输入一个字符 int 21h

cmp al,0dh ;判断时候为“回车”字符 jz exit1 ;如果是回车字符就跳转到exit1 cmp al,30h ;和30h(即字符0的asii值)比较 jl exit1 ;如果输入字符小于'0',跳转到exit1 cmp al,39h ;和39h(即字符9的ascii值)比较 jg exit1 ;如果输入字符大于'9',跳转到exit1

sub al,30h ;al减去30h,输入字符转化成数字(从这一行开始到后面的add bx,ax为输入字符转化为数字的处理方法) cbw ;al扩充为ax xchg ax,bx mov cx,10 mul cx xchg ax,bx

add bx,ax ;sub al,30h开始到这一行为输入字符转化为数字的处理方法

jmp InputX

exit1: mov x,bx ;把输入的存于bx的放到x中 lea dx,crlf mov ah,9 int 21h lea dx,h2 mov ah,9 int 21h xor bx,bx

InputY: ;和InputX类似,输入y mov ah,1 int 21h cmp al,0dh jz exit2 cmp al,30h jl exit2

cmp al,39h jg exit2 sub al,30h cbw

xchg ax,bx mov cx,10 mul cx xchg ax,bx add bx,ax jmp InputY

exit2: mov y,bx ;把输入的存于bx的放到y中 mov bx,x

add bx,y ;此时bx为两数加和

lea dx,crlf mov ah,9 int 21H lea dx,h3 mov ah,9 int 21h

xor si,si ;si清零,用作计数

mov ax,bx ;把和放到ax上

l4: mov cl,10 ;把和连续除以10知道和变为零,余数依次进栈 div cl 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

3.是编写一段程序,要求在长度为10的数组中,找出大于42h的无符号数的个数并存入地址为up开始区域,找出小于42h的无符号数的个数并存入地址为down的开始区域,并分别显示up、down数组的个数和数组内的数字

s1 segment stack

dw 100h dup(?) top label word s1 ends

s2 segment

h1 db 'the num of up array and the up array are(prints in decimalism) :','$'

h2 db 'the num of down array and the down array are(prints in decimalism) :','$' crlf db 0dh,0ah,24h

array db 0,50h,11h,61h,22h,72h,33h,73h,41h,74h,'$' ;定义数组array

up db 10 dup(?) ;定义up数组 down db 10 dup(?) ;定义down数组 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,1

mov bp,1

repeat: cmp array[si],42h ;把array数组中小于42h的值放到down数组里,大于42h的值放到up数组里面

jb downarray mov dl,array[si] mov up[bp],dl inc si cmp si,10 jz exit1

inc bp

jmp repeat

downarray: mov dl,array[si]

mov down[di],dl inc si cmp si,10 jz exit2 inc di

jmp repeat

exit1: sub di,1 jmp exit

exit2: sub bp,1

exit: mov dx,bp ;把分配好的up数组和down数组在其有效数字后面添加'$',便于后面的输出结束 mov up[0],dl inc bp mov up[bp],'$' mov dx,di

mov down[0],dl inc di

mov down[di],'$'

mov cl,10 mov si,0 lea dx,h1 mov ah,9 int 21h

PrintUparray:

cmp up[si],'$' jz next mov al,up[si]

call print inc si

jmp PrintUparray next: lea dx,crlf mov ah,9 int 21h

lea dx,h2 int 21h xor si,si

PrintDownArray:

cmp down[si],'$' jz atend mov al,down[si] call print inc si

jmp

print proc mov rediv: mov div mov mov push inc cmp jnz

break: pop add mov int dec cmp jnz

mov mov int ret print endp

atend: mov int

main endp s3 ends

end

PrintDownArray near ;print为输出十进制输出某个数的子程序 di,0 ah,0 cl dl,ah dh,0 dx di al,0 rediv dx dl,30h ah,2 21h di di,0 break dl,' ' ah,2 21H ah,4ch 21H main 4.键盘输入一段字符串,其中小写字母以大写字母输出,其他字符不变输出

s1 segment stack

dw 100h dup(?) top label word s1 ends

s2 segment

h1 db 'Please input a string:','$' h2 db 'The changed string is:','$' crlf db 0dh,0ah,24h temp db ? 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

lea dx,h1 mov ah,9 int 21h mov si,0

l: mov ah,1 int 21h cmp al,0dh jz exit cmp al,'a' jl putin cmp al,'z' jg putin sub al,20h ;把小写字符变为大写字符

putin: mov temp[si],al ;把字符放到temp数组里 inc si jmp l

exit: lea dx,crlf ;输出tmp数组

mov ah,9 int 21h lea dx,h2 mov ah,9 int 21h

inc si

mov temp[si],'$' lea dx,temp mov ah,9 int 21h

mov ah,4ch int 21H main endp s3 ends

end main

5.从键盘上就收一个小写字母,找出它的前导字符和后续字符,在顺序显示这三个字符

s1 segment stack

dw 100h dup(?) top label word s1 ends

s2 segment

h1 db 'Please input a lowercase: ','$' h2 db 'The the three chars are: ','$' 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

l: lea dx,h1 mov ah,9 int 21h

mov ah,1 int 21h cmp al,'a' jl l cmp al,'z' jg l

mov cl,al

lea dx,crlf mov int lea dx,h2 mov ah,9 int 21h

dec mov mov int

mov mov int

inc mov mov ah,2 int 21h

mov mov int

inc mov mov int 21h

mov int main endp s3 ends

end

ah,9 21H cl

dl,cl ah,2 21h dl,' ' ah,2 21h cl

dl,cl dl,' ' ah,2 21h cl

dl,cl ah,2 ah,4ch 21H main ;输出前导字符 输出该字符 输出后导字符 ; ; 6.把一个包含20个数据的数组M分成两组:正整数组P和负整数组N,分别把这两个数组中的数据的个数显示出来

s1 segment stack

dw 100h dup(?) top label word s1 ends

s2 segment

h1 db 'the positive number is: ','$' h2 db 'the negative number is: ','$' crlf db 0dh,0ah,24h array dw 50h,-11h,61h,-22h,72h,-33h,73h,-41h,74h,21h,67h,-90h,73h,77h,-1h,-89h,-11h,61h,-22h,20h,'$' 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 bx,0 mov si,0

l: mov dx,array[si] cmp dx,0

jl addlow ;有符号数比较用jl add si,2 cmp si,40 jz exit jmp l

addlow: inc bx add si,2

cmp si,40 jz exit jmp l

exit: lea dx,h2 mov ah,9

int 21h

mov ax,bx call print

lea dx,crlf mov ah,9 int 21h lea dx,h1 mov ah,9 int 21h mov ax,20

sub ax,bx 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

l2: pop dx mov ah,2 int 21h dec si cmp si,0 jnz l2

ret print endp

atend: mov ah,4ch int 21H

;打印数字字符的子程序

main endp s3 ends

end main

7.打印输出首地址为data的20个字数组中的最小偶数

s1 segment stack

dw 100h dup(?) top label word s1 ends

s2 segment

h1 db 'the min even number is: ','$' crlf db 0dh,0ah,24h

data dw 50,-11,61,-22,72,-33,73,-41,74,21,67,-90,73,77,-1,-89,-11,61,-22,20,'$' s2 ends

s3 segment

assume cs:s3,ds:s2,ss:s1 main proc far mov ax,s1 mov ss,ax lea sp,top mov ax,s2 mov ds,ax

mov bx,65534 ;bx存最小数,初始令最小值置为65534 mov si,0 mov cl,100 mov dl,2

l2: mov ax,data[si] cmp ax,0 jnl l4

neg ax ;如果是负数,则求补

l4: div cl

mov al,ah mov ah,0 div dl cmp ah,0

jnz l1 cmp bx,data[si] ;比较最小值和数组中的每个数 jl l1 ;如果数组中的数大于最小值跳转到l1 mov bx,data[si] ;如果数组中的数小于最小值则将其赋给最小值

l1: add si,2 cmp si,40 jz exit jmp l2

exit: lea dx,h1 mov ah,9 int 21h

cmp bx,0 jnl l5 neg bx

mov dl,'-' mov ah,2 int 21h

l5: mov ax,bx

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

8.输入两船字符串string1和string2,并比较两个字符串是否相等,相等就显示“match”,否则显示“no match”

s1 segment stack

dw 100h dup(?) top label word s1 ends

s2 segment

h1 db 'Please input the first string: ','$' h2 db 'Please input the second string: ','$' h3 db 'MATCH','$' h4 db 'NO MATCH','$' crlf db 0dh,0ah,24h str1 db 50,?,50 dup('$') str2 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

lea dx,h1 mov ah,9

int 21h lea dx,str1 mov ah,0ah int 21h lea dx,crlf mov ah,9 int 21h lea dx,h2 int 21h lea dx,str2 mov ah,0ah int 21h lea dx,crlf mov ah,9 int 21h

mov dl,str1+1 cmp dl,str2+1 jnz l

mov si,2

l2: mov dl,str1[si] cmp dl,str2[si] jnz l inc si cmp si,50 jz l3 jmp l2

l: lea dx,h4 mov ah,9 int 21h

l3: lea dx,h3 mov ah,9 int 21h

mov ah,4ch int 21H

;str1+1为str1实际的字符个数 ;str2+1为str2实际的字符个数 ;输出不匹配信息 ;输出匹配信息

main endp s3 ends

end main

9.从键盘接收一个四位的十六进制数,并在终端显示与它等值的二进制数

s1 segment stack

dw 100h dup(?) top label word s1 ends

s2 segment

h1 db 'Please input a hexadecimal number: ','$' h2 db 'The number is printed in binary number: ','$' temp db 17 dup('$') 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

repeat: lea dx,h1 mov ah,9 int 21h mov bx,0 mov cx,4

newchar: ;接收新字符 mov ah,1 int 21h cmp al,30h jb repeat cmp al,46h jg repeat cmp al,39h

jnb l1 ;如果输入字符大于9跳到l1

sub al,30h jmp l2

l1: cmp al,41h

jb repeat ;如果输入字符小于A,则输入错误,跳到repeat sub al,37h ;输入字符为A~Z,故相应地要减37h jmp l2

l2: cbw ;l2为把输入字符转化为数值 xchg ax,bx mov dx,10h mul dx xchg ax,bx add bx,ax ;loop newchar dec cx cmp cx,0 jnz newchar

lea dx,crlf mov ah,9 int 21h lea dx,h2 int 21h

mov si,0

mov cx,10h ;cx作计数器,即待会要循环16次

l5: rol bx,1 ;bx循环左移以为,最高位进标志位 jc l3 ;若标志位为1则跳转到l3 mov temp[si],'0' jmp l4

l3: mov temp[si],'1' l4: inc si loop l5

lea dx,temp mov ah,9 int 21h

mov ah,4ch int 21H

main endp

s3 ends

end main

10从键盘输入一系列以$为结束符的字符串,然后对其中的非数字字符计数,并显示计数结果

s1 segment stack

dw 100h dup(?) top label word s1 ends

s2 segment

h1 db 'Please input a string: ','$'

h2 db 'The number of the chars that is not digit:','$' 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

lea dx,h1 mov ah,9 int 21h

mov cx,0

l2: mov ah,1 int 21h cmp al,'$' jz exit cmp al,30h jb l

cmp al,39h jnb l jmp l2

l: inc cx jmp l2

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

mov bx,0

l5: cmp data[si],'$' ;小于平均值的数的个数放到bx中 jz exit2

cmp ax,data[si] jz l6 jl l6 inc bx

l6: add si,2 jmp l5

exit2: mov ah,4ch int 21H

main endp s3 ends

end main

14.一直数组A包含15个互不相等的整数,数组B包含20个互不相等的整数。试编制一个程序,把既在A中又在B中出现的整数存放于数组C中并显示C中的数值

;两层循环比较得出两个数组中相同的数值 s1 segment stack

dw 100h dup(?) top label word s1 ends

s2 segment

a dw 1h,2h,3h,4h,5h,6h,7h,8h,9h,10h,11h,12h,13h,14h,15h,'$' b dw 21h,22h,23h,24h,25h,6h,18h,19h,10h,11h,12h,34h,14h,53h,31h,32h,33h,36h,7h,67h,'$' c dw 16 dup('$')

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,0 mov bp,0

l4: cmp di,40 jz l2 jmp l3

l2: add si,2 cmp si,30 jz exit mov di,0

l3: mov ax,a[si] mov bx,b[di] cmp ax,bx jnz l mov c[bp],ax add bp,2

l: add di,2 jmp l4

exit: mov bp,0

l6: cmp c[bp],'$' jz atend mov ax,c[bp] call print add bp,2 mov dl,' ' mov ah,2 int 21h jmp l6

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

l5: pop dx mov ah,2 int 21h dec si cmp si,0 jnz l5

ret print endp

atend: mov ah,4ch int 21H

main endp s3 ends

end main

15.设在A、B和D单元中分别存放着三个数。若三个数都不是0,则求出三个数的和并存放在S单元,若其中有一个数为0,则把其它两个单元也清零。请编写此程序

s1 segment stack

dw 100h dup(?) top label word s1 ends

s2 segment

a dw 1h,'$' b dw -11h,'$' d dw 0h,'$' s dw 2 dup('$')

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 ax,a[0] mov bx,b[0] mov dx,d[0] cmp ax,0 jz l cmp bx,0 jz l cmp dx,0 jz l mov cx,0 cmp ax,0 jnl add_ax neg ax ;减法需先求补 sub cx,ax jmp l2

add_ax: add cx,ax l2: cmp bx,0 jnl add_bx neg bx sub cx,bx jmp l3

add_bx: add cx,bx l3: cmp dx,0 jnl add_dx neg dx sub cx,dx jmp l4

add_dx: add cx,dx l4: mov s[0],cx jmp exit

l: mov a[0],0 mov b[0],0 mov d[0],0

exit: mov ah,4ch int 21H

main endp s3 ends

end main

16.从键盘输入一系列字符(以回车键结束),并按字母、数字和其他字符分类计数,最后显示这三类的计数结果

s1 segment stack

dw 100h dup(?) top label word s1 ends

s2 segment

letter db 'the number of letter: ','$' digit db 'the number of digit: ','$'

others db 'the number of other chars: ','$' 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 bx,0 mov cx,0 mov dx,0

repeat: mov ah,1 int 21h cmp al,0dh jz exit cmp al,30h jb count_others cmp al,'z' jnb count_others cmp al,39h jb count_digit cmp al,'A'

jb count_others cmp al,'Z' jb count_letter cmp al,'a' jb count_others jmp count_letter

count_letter: inc bx jmp repeat

count_digit: inc cx jmp repeat

count_others: inc dx jmp repeat

exit: push cx push dx call print_crlf lea dx,letter mov ah,9 int 21h mov ax,bx call print_ax call print_crlf lea dx,others mov ah,9 int 21h pop dx ;栈,后进先出,先进后出 mov ax,dx call print_ax call print_crlf lea dx,digit mov ah,9 int 21h pop cx ;print_ax会修改cx的值,故要先把cx进栈 mov ax,cx call print_ax

jmp exit2

print_crlf proc near lea dx,crlf mov ah,9 int 21h ret

print_crlf endp

print_ax proc near

mov cl,10 mov si,0

repeat2: div cl

mov dl,ah add dl,30h

mov dh,0 push dx inc si mov ah,0

cmp al,0 jnz repeat2

l3: pop dx mov ah,2 int 21h dec si cmp si,0 jnz l3

ret print_ax endp

exit2: mov ah,4ch int 21H

main endp s3 ends

end main

17.已定义两个整数变量A和B,试编写程序完成以下功能

(1)若两个树种有一个是奇数,则将奇数存入A中,偶数存入B中 (2)若两个数均为奇数,则将两个数加1后存回原变量 (3)若两个数均为偶数,则两个变量均不变

s1 segment stack

dw 100h dup(?) top label word s1 ends

s2 segment a dw 12,'$' b dw -13,'$' 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,0 mov dx,0 mov cx,2 mov ax,a[0] cmp ax,0 jnl next neg ax

next:div cx cmp dx,1 jnz l mov si,1

l: mov dx,0 mov ax,b[0] cmp ax,0 jnl next2

neg ax

next2: div cx cmp dx,1 jnz l2 mov di,1

l2: cmp si,0 jnz l3 cmp di,0 jnz l4 jmp exit ;都是偶数,跳转到末尾

l3: cmp di,0 jnz l5 jmp exit ;a[0]为奇数,b[0]为偶数,跳转到末尾

l5: mov ax,a[0] ;a[0],b[0]为奇数,a[0]加一存回原变量 cmp ax,0 jnl l6 neg ax dec ax neg ax jmp l7 l6: inc ax l7: mov a[0],ax mov ax,b[0] ;a[0],b[0]为奇数,b[0]加一存回原变量 cmp ax,0 jnl l8 neg ax dec ax neg ax jmp l9 l8: inc ax l9: mov b[0],ax jmp exit

l4: mov ax,a[0] ;a[0]为偶数,b[0]为奇数,互换 mov bx,b[0] mov a[0],bx mov b[0],ax

exit: mov ah,4ch

int 21H

main endp s3 ends

end main

18.写一段子程序skiplines,完成输出空行的功能。空行的行数由用户在主程序中通过键盘输入,并将行数放在ax寄存器中

s1 segment stack

dw 100h dup(?) top label word s1 ends

s2 segment

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 bx,0

l: mov ah,1 int 21h cmp al,0dh jz next cbw

sub ax,30h xchg ax,bx mov cx,10 mul cx xchg ax,bx add bx,ax

jmp l

next:mov ax,bx call skiplines

mov ah,4ch int 21H

main endp

skiplines proc near

mov bx,ax repeat: cmp bx,0 jz exit

lea dx,crlf mov ah,9 int 21h

dec bx jmp repeat

exit: ret

skiplines endp

s3 ends

end main

19.设有10个学生成绩分别是76, 69,84,73,88,99,63,100和80。试编写一个子程序统计60-69分,70-79分,80-89分,90-99分和100分的人数,并分别放到S6,S7,S8,S9,S10单元中

s1 segment stack

dw 100h dup(?) top label word s1 ends

s2 segment

score dw 76,69,84,90,73,88,99,63,100,80,'$' s6 dw 0,'$' s7 dw 0,'$' s8 dw 0,'$' s9 dw 0,'$' s10 dw 0,'$' 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

repeat: cmp si,20 jz exit mov ax,score[si] cmp ax,100 jnz l mov bx,s10[0] inc bx mov s10[0],bx jmp l5 ;必须跳转到si加二那里

l: cmp ax,90 jb l2 mov bx,s9[0] inc bx mov s9[0],bx jmp l5

l2: cmp ax,80 jb l3 mov bx,s8[0] inc bx mov s8[0],bx jmp l5

l3: cmp ax,70 jb l4 mov bx,s7[0] inc bx mov s7[0],bx jmp l5

l4: mov bx,s6[0] inc bx mov s6[0],bx

l5: add si,2

jmp repeat

exit: mov ah,4ch int 21h

mainendp s3 ends

end main

20.编写子程序嵌套结构的程序,把整数分别用二进制和八进制显示出来

s1 segment stack

dw 100h dup(?) top label word s1 ends

s2 segment

data dw 16h,32h,'$' ;要显示的两个数字 crlf db 0dh,0ah,24h,'$' s2 ends

s3 segment

assume cs:s3,ds:s2,ss:s3 bando proc far mov ax,s1 mov ss,ax lea sp,top mov ax,s2 mov ds,ax mov si,0

repeat: cmp si,4 jz exit mov bx,data[si] call pairs

call print_crlf add si,2 jmp repeat

exit: mov ah,4ch int 21h

bando endp

pairs proc near mov cx,10h

l: rol bx,1 ;以二进制数输出—— jc print_one mov dl,'0' jmp print_zero

print_one: mov dl,'1'

print_zero: mov ah,2 int 21h loop l mov dl,' ' mov ah,2 int 21h mov dh,0 mov ch,4 mov cl,3 mov dl,bl and dl,7d add dl,30h push dx

rotate: ror bx,cl mov dl,bl and dl,7d add dl,30h push dx dec ch cmp ch,0 jnz rotate ror bx,1 mov dl,bl and dl,1 add dl,30h

;——以二进制数输出 ;以八进制数输出—— mov ah,2 int 21h

print: cmp ch,5 jz atend pop dx mov ah,2 int 21h inc ch jmp print ;——以八进制数输出

atend: ret

pairs endp

print_crlf proc near lea dx,crlf mov ah,9 int 21h ret

print_crlf endp

s3 ends

end bando

21.在D盘根目录建立一个文件abc.txt,第一次向文件写入“123456”六个字符,第二次增加“abcdefg”几个字符

s1 segment stack

dw 100h dup(?) top label word s1 ends

s2 segment fn db 'd:\\abc.txt',0 fh dw ? buff1 db '1','2','3','4','5','6','$' buff2 db 'a','b','c','d','e','f','$' 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 ah,3ch ;建立文件 mov cx,20h lea dx,fn int 21h jc error mov fh,ax ;获取文件代号 lea dx,buff1 mov ah,40h ;写文件 mov bx,fh mov cx,6 int 21h jc error lea dx,buff2 mov ah,40h mov bx,fh mov cx,6 int 21h jc error mov ah,3eh ;关闭文件 mov bx,fh int 21h

error:

exit: mov ah,4ch int 21h

mainendp s3 ends

end main

22.从键盘上输入文本文件:“d:\\temp.txt”的内容后,然后新建一个文件“d:\\temp2.txt”,把

前一个文件的所有内容复制到后一个文件中

s1 segment stack

dw 100h dup(?) top label word s1 ends

s2 segment

f1 db 'd:\\temp1.txt',0 fd1 dw ?

f2 db 'd:\\temp2.txt',0 fd2 dw ?

buff1 db 50,?,50 dup(?) buff2 db 50 dup('$')

h1 db 'Please input a string: ','$' 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 ah,3dh lea dx,f1 int 21h jnc l jmp error l: mov fd1,ax

lea dx,h1 mov ah,9 int 21h lea dx,buff1 mov ah,0ah int 21h

lea dx,buff1[2]

mov ah,40h ;写文件 mov bx,fd1 mov ch,0

mov cl,buff1+1 int 21h jc error

mov ah,3ch ;建立文件 lea dx,f2 mov cx,20h int 21h jc error mov fd2,ax

mov ah,42h mov al,00 mov cx,0 mov dx,0 mov bx,fd1 int 21h

mov ah,3fh lea dx,buff2[2] mov bx,fd1 mov ch,0

mov cl,buff1+1 int 21h jc error

mov ah,40h lea dx,buff2[2] mov bx,fd2 mov ch,0

mov cl,buff1+1 int 21h jc error

mov ah,3eh mov bx,fd1 int 21h jc error

mov ah,3eh mov bx,fd2 int 21h jc error jmp exit

;移动文件指针;读文件 ;写文件 ;关闭文件 ;关闭文件

error:

exit: mov ah,4ch int 21h

mainendp s3 ends

end main

23.从键盘上输入一个十进制数,以十六进制数显示出来。要求子程序用寄存器参数传送方法

s3 segment

assume cs:s3

main proc far

repeat:

call decibin call crlf call binihex call crlf jmp repeat

main endp

decibin proc near

mov bx,0

newchar:

mov ah,1 int 21h sub al,30h jl exit cmp al,9d jg exit cbw

xchg ax,bx mov cx,10d mul cx xchg ax,bx add bx,ax

jmp newchar

exit: ret

decibin endp

binihex proc near

mov ch,4 rotate:

mov rol mov and add cmp jl add

printit:

mov mov int dec jnz ret

binihex endp

crlf proc

mov mov int mov mov int ret

crlf endp

s3 ends

end

cl,4 bx,cl al,bl al,0fh al,30h al,3ah printit al,7h dl,al ah,2 21h ch rotate near dl,0dh ah,2 21h dl,0ah ah,2 21h main

24.试编制一个程序,把bx寄存器中的二进制数用十六进制数的形式在屏幕上显示出来

s1 segment stack

dw 100 dup(?) top label word s1 ends

s3 segment

assume cs:s3,ss:s1 main proc mov mov lea

mov mov rotate:

mov rol mov and add cmp jl add

printit:

mov mov int dec jnz

mov int

main endp s3 ends

end

far ax,s1 ss,ax sp,top bx,16 ch,4 cl,4 bx,cl al,bl al,0fh al,30h al,3ah printit al,7h dl,al ah,2 21h ch rotate ah,4ch 21h main

本文来源:https://www.bwwdw.com/article/f2nw.html

Top