"景先生毕设|www.jxszl.com

10个汇编小程序

2023-09-12 15:40编辑: www.jxszl.com景先生毕设
               10个汇编小程序
1.写一段子程序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
2.设有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
main endp
s3      ends
        end     main
3.编写子程序嵌套结构的程序,把整数分别用二进制和八进制显示出来
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
4.在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
main endp
s3      ends
        end     main
5.从键盘上输入文本文件:“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
main endp
s3      ends
        end     main
6.从键盘上输入一个十进制数,以十六进制数显示出来。要求子程序用寄存器参数传送方法
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     cl,4
        rol     bx,cl
        mov     al,bl
        and     al,0fh
        add     al,30h
        cmp     al,3ah
        jl      printit
        add     al,7h
printit:
        mov     dl,al
        mov     ah,2
        int     21h
        dec     ch
        jnz     rotate
        ret
binihex endp
crlf    proc    near
        mov     dl,0dh
        mov     ah,2
        int     21h
        mov     dl,0ah
        mov     ah,2
        int     21h
        ret
crlf    endp
s3      ends
        end     main
7.试编制一个程序,把bx寄存器中的二进制数用十六进制数的形式在屏幕上显示出来
s1      segment stack
        dw      100     dup(?)
top     label   word
s1      ends
s3      segment
        assume  cs:s3,ss:s1
main    proc    far
        mov     ax,s1
        mov     ss,ax
        lea     sp,top
        mov     bx,16
        mov     ch,4
rotate:
        mov     cl,4
        rol     bx,cl
        mov     al,bl
        and     al,0fh
        add     al,30h
        cmp     al,3ah
        jl      printit
        add     al,7h
printit:
        mov     dl,al
        mov     ah,2
        int     21h
        dec     ch
        jnz     rotate
       
        mov     ah,4ch
        int     21h
main    endp
s3      ends
        end     main
       
8.一直数组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
9.设在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
10.从键盘输入一系列字符(以回车键结束),并按字母、数字和其他字符分类计数,最后显示这三类的计数结果
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

原文链接:http://www.jxszl.com/biancheng/python/446225.html