微机原理与系统设计实验

  没啥好说的,总结我电一波实验题目,关于汇编的……


 题目要求

实验一 EMU8086使用及8086指令系统

 · 编写一个简单的程序:将“This is my first ASM program-姓名(汉语拼音各人的姓名)”放在DS=1000H,BX=0000H开始的存储器单元中,然后将该内容搬移到BX=0100H开始的单元中,最后将该字符串通过DOS功能调用显示在屏幕上。

实验二  数制转换与数值运算编程

· 2.1 将ASCII码表示的十进制数转换为二进制数。
· 2.2 BCD码转换为二进制数
· 2.3 两个非压缩BCD数加法程序。
· 2.4 从键盘上输入任意两个不大于2位数的正整数,计算其乘积。结果在屏幕上显示。

实验三 顺序、分支、循环、子程序设计

· 3.1 教材P74例1。m=6,n=4,w=7。结果Q也放在内存中。
· 3.2 教材P75例3。
· 3.3 从键盘上输入1个数,判断其奇偶性,如果是奇数,屏幕上显示”It is odd”,否则显示”It is even”。
· 3.4 从键盘上输入N个字符(N<16),求这N个字符中’A’的个数,并将’A’的个数显示在屏幕上。
· 3.5 已知BUF1中有N1个按从小到大的顺序排列的互不相等的无符号数,BUF2中有N2个从小到大的顺序排列的互不相等的无符号数。编写程序将BUF1和BUF2中的数合并到BUF3中,使在BUF3中存放的数互不相等且按从小到大的顺序排列。

实验四  综合性汇编程序设计

· 4.1  计算S=1+2×3+3×4+4×5+…+N(N+1),直到N(N+1)项大于200为止。
· 4.2  求N!。N为键盘输入的不大于8的正整数。
· 4.3  从键盘输入一行字符(以回车符结束),并按字母、数字及其它字符分类计数,最后显示出这3个计数结果。

程序清单及结果:

1.

 1 name "mycode"
 2 org 100h
 3     ; set segment register:
 4     mov ax, 1000h
 5     mov ds, ax
 6     mov bx,0000h
 7     ; print "This is my first ASM program-WangYuBo"
 8     mov [02h], 'T'
 9     mov [04h], 'h'
10     mov [06h], 'i'
11     mov [08h], 's'
12     mov [0ah], ' '
13     mov [0ch], 'i'
14     mov [0eh], 's'
15     mov [10h], ' '
16     mov [12h], 'm'
17     mov [14h], 'y'
18     mov [16h], ' '
19     mov [18h], 'f'
20     mov [1ah], 'i'
21     mov [1ch], 'r'
22     mov [1eh], 's'
23     mov [20h], 't'
24     mov [22h], ' '
25     mov [24h], 'A'
26     mov [26h], 'S'
27     mov [28h], 'M'
28     mov [2ah], ' '
29     mov [2ch], 'p'
30     mov [2eh], 'r'
31     mov [30h], 'o'
32     mov [32h], 'g'
33     mov [34h], 'r'
34     mov [36h], 'a'
35     mov [38h], 'm'
36     mov [3ah], '-'
37     mov [3ch], 'W'
38     mov [3eh], 'a'
39     mov [40h], 'n'
40     mov [42h], 'g'
41     mov [44h], 'Y'
42     mov [46h], 'u'
43     mov [48h], 'B'
44     mov [4ah], 'o'
45     mov cx,0025h ;一共37个字符
46     mov bx,0100h
47     mov si,0002h          
48     ;string copy
49     copy:
50         mov ax,ds:[si]
51         mov ds:[bx+si],ax
52         add si,2
53     loop copy
54     mov bx,0102h
55     mov cx,0025h
56     ;printf string
57     printf:    
58         mov dl,[bx]
59         mov ah,02h
60         int 21h
61         add bx,2
62     loop printf          
63     mov ah,1
64     int 21h
65 
66     mov ah,4ch
67     int 21h
68     ret
View Code

        

2.1

 1 DATA        SEGMENT
 2 index        DB  30H,30H,32H,35H,36H
 3 DATA        ENDS
 4 
 5 CODE        SEGMENT
 6      ASSUME CS:CODE, DS:DATA
 7 START:    
 8     MOV AX, DATA
 9      MOV DS, AX
10      MOV SI, OFFSET index
11      MOV CX,5
12 FOR1:
13      MOV AL,[SI]
14      SUB AL,30H
15      MOV [SI],AL
16      INC SI
17 LOOP FOR1
18      MOV AX,0000h
19      MOV BX,0000h
20      LEA SI,index
21 
22     MOV DX,10000
23     MOV AL,[SI]
24     IMUL DX
25     MOV BX,AX
26 
27       MOV DX,1000
28       INC SI
29       MOV AL,[SI]
30       IMUL DX
31       ADD BX,AX
32 
33        MOV DX,100
34       INC SI
35       MOV AL,[SI]
36       IMUL DX
37       ADD BX,AX
38 
39       MOV DX,10
40       INC SI
41       MOV AL,[SI]
42       IMUL DX
43       ADD BX,AX
44 
45       INC SI
46       MOV AL,[SI]
47       ADD BX,AX
48 
49       MOV AX,BX
50     
51     ;MOV AX,4C00h
52     ;INT 21h
53     
54 CODE     ENDS
55       END START
View Code

            

2.2

 1 code segment 
 2     ASSUME CS:CODE
 3 start:
 4     ;store data at ds:[3500h]~ds:[3507h]
 5     mov si,3500h
 6     mov [si],01
 7     mov [si+1],02
 8     mov [si+2],03
 9     mov [si+3],04
10     mov [si+4],05
11     mov [si+5],06
12     mov [si+6],07
13     mov [si+7],08
14     mov cx,4
15     mov di,3510h
16     mov ax,0000h
17     mov bx,000Ah
18     mov dx,0000h
19 for:
20     mov al,ds:[si]
21     imul bl
22     mov dx,ds:[si+1]
23     and dx,00FFh
24     adc ax,dx
25     mov ds:[di],al
26     add si,2
27     inc di
28 loop for
29     mov ax, 4c00h
30     int 21h    
31 ends
32 end start
View Code

            

2.3 

 1 code segment 
 2     ASSUME CS:CODE
 3 start:     
 4     mov [0],01010110b    ;56h
 5     mov [1],00h
 6     mov [2],01111000b    ;78h 十进制和为134
 7     mov [3],00h
 8     mov ax,[0]            ; ax = 0056h
 9     mov bx,[2]            ; bx = 0078h 
10     mov cx,000Ah
11     mov dx,0000h          ;累加和
12     push ax
13     push bx
14     and ax,00F0h
15     and bx,00F0h
16     add ax,bx
17     shr ax,4                    ;dx = 000c h
18     imul cx
19     mov dx,ax
20     pop bx
21     pop ax
22     and ax,000Fh
23     and bx,000Fh
24     add ax,bx
25     add dx,ax      ;dx store the sum in hex
26     ;----------- print dx with demical number --------
27     mov ax,dx
28     mov cx,0 
29     mov bx,10
30 disp1:
31     mov dx,0
32     div bx 
33     push dx
34     inc cx
35     or ax,ax
36 jne disp1 
37 disp2: 
38     mov ah,2
39     pop  dx
40     add  dl, 30H
41     int 21h
42     loop disp2
43     ;-----------the end------------
44     mov ax,4c00h
45     int 21h
46 ends
47 end start
View Code

        

2.4

 1 data segment
 2     str1 db 'please input a number to a:$'
 3     str2 db 'please input a number to b:$'
 4     str3 db 'result c=a*b=$'
 5     a dw ?
 6     b dw ?
 7     c dw ?
 8 data ends
 9 
10 code segment
11 assume cs:code,ds:data
12     main proc far
13     start:
14         mov ax,data
15         mov ds,ax
16         lea dx,str1 ;ouput str1
17         mov ah,9h
18         int 21h
19         call input  ;input a
20         mov a,bx
21         lea dx,str2 ;ouput str2
22         mov ah,9h
23         int 21h
24         call input  ;input b
25         mov b,bx
26         lea dx,str3 ;ouput str3
27         mov ah,9h
28         int 21h
29         mov ax,a
30         aad
31         mov bx,ax
32         mov ax,b
33         aad
34         mul bx                   ;ax = a*b
35         mov word ptr c,ax   ;c = a*b
36         mov di,0ah             ;di = 10
37         mov cx,0
38     ToTen:            ;convert to decimal
39         div di
40         push dx
41         inc cx
42         cmp ax,0
43         je print
44         cwd
45         jmp ToTen
46     print:            ;output result
47         pop dx
48         add dl,30h
49         mov ah,2h
50         int 21h
51     loop print
52         ret
53     main endp
54     input proc
55         mov bx,0
56         inputa:
57         mov ah,1    ;first char
58         int 21h
59         sub al,30h
60         mov bl,al
61         mov ah,1        ;second char
62         int 21h
63         cmp al,0dh  ;compare if al==Enter
64         je exit
65         sub al,30h
66         mov cl,8     ;shift operation
67         rol bx,cl
68         mov bl,al
69         jmp exit
70         exit:
71             call crlf
72         ret
73     input endp
74     ;print next line
75     crlf proc near
76         mov dl,0ah
77         mov ah,2h
78         int 21h
79         mov dl,0dh
80         mov ah,2h
81         int 21h
82         ret
83     crlf endp
84 
85 code ends
86     end start
View Code

            

            

3.1

 1 data segment
 2 address db 06h,04h,07h 
 3 string1 db ' m=6,n=4,w=7 $'
 4 string2 db ' m * n - w = $'
 5 data ends
 6 
 7 code segment
 8     ASSUME CS:CODE,DS:data
 9     main:
10         mov ax,data
11         mov ds,ax
12         mov si,offset address
13         mov al,[si]     ;al = m = 6
14         mov bl,[si+1]   ;bl = n = 4
15         mul bl          ;al = m*n
16         push ax
17         lea dx,string1 ;output string1
18         mov ah,09h
19         int 21h
20         call crlf
21         pop ax
22         mov bx,0        ;bl = 0
23         mov bl,[si+2]   ;bl = w = 7
24         sub ax,bx       ;ax = m * n - w
25         push ax
26         lea dx,string2 ;output string2
27         mov ah,09h
28         int 21h
29         pop ax
30         ;print demical result
31         mov cx,0
32         mov bx,10
33     disp1:
34           mov dx,0
35         div bx     ;如果除数是16位,AX储存商,DX储存余数
36         push dx 
37         inc cx
38         or ax,ax
39         jne disp1
40     disp2:
41         mov ah,2
42         pop  dx
43         add  dl, 30H
44         int 21h
45         loop disp2
46         mov ax,4c00h
47         int 21h
48 
49     crlf proc near
50         push dx
51         mov dl,0ah
52         mov ah,2h
53         int 21h
54         mov dl,0dh
55         mov ah,2h
56         int 21h
57         pop dx
58         ret
59     crlf endp
60     code ends
61 end main
View Code

           

3.2

  1 shuju segment
  2     DATA  db 94,61,86,86,53,84,81,63,45,80,58,81,75,95, 70,85,49,46,85,77,87,99,94,66,78,48,86,65,76,89,74,93,73,100,54,78,75,53,86,50,53,68,63,86,78,62,100,56,48,63,63,52,80,46,100,69,49,95,93,54,75,82,95,51,88,100,82,82,54,96,63,79,66,94,78,81,95,86,95,67
  3 BUFFER 4 dup(0)
  4 str0 db "The 80 scores are: $"
  5 str1 db "#Number of score above 90 points: $"
  6 str2 db "#Number of score between 70 points and 89 points: $"
  7 str3 db "#Number of score between 60 points and 69 points: $"
  8 str4 db "#Number of score above 60 points: $"
  9 shuju ends
 10 
 11 code segment
 12     assume CS:code,DS:shuju
 13     main:
 14         mov ax,shuju
 15         mov ds,ax
 16         mov dx,0000h
 17         mov bx,0000h
 18         mov cx,80
 19         mov si,offset DATA
 20         mov di,offset BUFFER
 21         nine:
 22             mov al,[si]
 23             cmp al,90
 24             jc eight
 25             inc dh
 26             jmp stor
 27         eight:
 28             cmp al,70
 29             jc six
 30             inc dl
 31             jmp stor
 32         six:
 33             cmp al,60
 34             jc five
 35             inc bh
 36             jmp stor
 37         five:
 38             inc bl
 39         stor:
 40             inc si
 41         loop nine
 42 
 43         mov [di],dh
 44         mov [di+1],dl
 45         mov [di+2],bh
 46         mov [di+3],bl
 47 
 48         lea dx,str0 ;output string0
 49         mov ah,09h
 50         int 21h
 51         mov cx,80
 52         lea si,DATA
 53     show: 
 54         mov ax,0
 55         mov al,[si]
 56         call printf
 57         call space
 58         inc si
 59     loop show 
 60         call crlf
 61         lea dx,str1
 62         mov ah,09h
 63         int 21h
 64         mov ax,0
 65         mov al,[di]
 66         call printf
 67         call crlf
 68         lea dx,str2
 69         mov ah,09h
 70         int 21h
 71         mov ax,0
 72         mov al,[di+1]
 73         call printf
 74         call crlf
 75         lea dx,str3
 76         mov ah,09h
 77         int 21h
 78         mov ax,0
 79         mov al,[di+2]
 80         call printf
 81         call crlf
 82         lea dx,str4
 83         mov ah,09h
 84         int 21h
 85         mov ax,0
 86         mov al,[di+3]
 87         call printf
 88         call crlf
 89         mov ax,4c00h
 90         int 21h
 91 
 92     printf proc near
 93         push cx
 94         push bx
 95         push dx
 96         mov cx,0
 97         mov bx,10
 98     disp1:
 99         mov dx,0
100         div bx
101         push dx
102         inc cx
103         or ax,ax
104         jne disp1
105     disp2:
106         mov ah,2
107         pop  dx 
108         add  dl,30H
109         int 21h
110         loop disp2
111         ;call crlf
112         pop dx
113         pop bx
114         pop cx
115         ret
116     printf endp
117     crlf proc near
118         push ax
119         push dx
120         mov dl,0ah
121         mov ah,2h
122         int 21h
123         mov dl,0dh
124         mov ah,2h
125         int 21h
126         pop dx
127         pop ax
128         ret
129     crlf endp
130 
131     space proc near
132         push ax
133         push dx
134         mov dl,20h
135         mov ah,2h
136         int 21h
137         pop dx
138         pop ax
139         ret
140     space endp
141     code ends
142 end main
View Code

         

3.3

 1 assume cs:code,ds:data
 2 
 3 data segment
 4   string0 db 'Welcome to here!Input a number: $'
 5   string1 db 'It is odd $'
 6   string2 db 'It is even $'
 7   string3 db 'Inlegal character!$'
 8 data ends
 9 
10 code segment
11 start:
12     mov ax,data
13     mov ds,ax
14     lea dx,string0 ;output string0
15     mov ah,09h
16     int 21h
17 
18     mov cx,10
19 input:
20     mov ah,01h
21     int 21h
22     cmp al,39h
23     ja inlegal
24     cmp al,0dh ;enter \r\n
25     je outside
26     mov bl,al
27 loop input
28 inlegal:   
29     call CRLF
30     lea dx,string3 ;output string3
31     mov ah,09h
32     int 21h
33     jmp tail
34 outside:
35     call CRLF
36     and bl,01h
37     cmp bl,1
38     jne even
39 odd:
40     lea dx,string1
41     mov ah,09h
42     int 21h
43     jmp tail
44 even:
45     lea dx,string2
46     mov ah,09h
47     int 21h
48 
49 tail:
50     mov ax,4c00h
51     int 21h
52 
53     CRLF PROC NEAR
54         PUSH AX
55         PUSH DX
56         MOV DL,0AH
57         MOV AH,2H
58         INT 21H
59         MOV DL,0DH
60         MOV AH,2H
61         INT 21H
62         POP DX
63         POP AX
64         RET
65     CRLF ENDP
66 
67 code ends
68 end start
View Code

         

         

         

3.4

 1 data segment 
 2   string1 db 'please input a string: $'
 3   string2 db 'the number of char a: $'
 4 data ends
 5 
 6 code segment
 7     main proc far
 8     assume cs:code,ds:data
 9    start:
10         mov ax,data
11         mov ds,ax
12         mov cx,16
13         lea dx,string1 ;output string1
14         mov ah,09h
15         int 21h
16         mov dx,0   ;dl store the number of 'a'
17     write:
18         mov ah,1h
19         int 21h
20         cmp al,0dh ;enter \r\n
21         je print
22         cmp al,41h ;'a'
23         jne next
24         inc dl     ;dl++
25     next:
26     loop write
27     
28     print:
29         call crlf
30         push dx
31         lea dx,string2
32         mov ah,09h
33         int 21h
34         pop dx
35         add dl,30h
36         mov ah,02h
37         int 21h
38 
39         mov ax,4c00h
40         int 21h
41     main endp
42 
43     crlf proc near
44         push dx
45         mov dl,0ah
46         mov ah,2h
47         int 21h
48         mov dl,0dh
49         mov ah,2h
50         int 21h
51         pop dx
52         ret
53     crlf endp
54 code ends
55 end start
View Code

       

3.5

  1 data segment 
  2     BUF1 db 1,2,3,5,7,9     
  3     BUF2 db 0,2,4,6,8
  4     BUF3 db 32 dup(204)
  5     N1     db 6
  6     N2     db 5
  7     string db 'Array merged completed!$'
  8 data ends
  9 
 10 code segment
 11     ASSUME CS:CODE,DS:DATE
 12     start:
 13         mov ax,data
 14         mov ds,ax
 15         lea si,BUF1 ;pointer of buffer1
 16         lea di,BUF2 ;pointer of buffer2
 17         lea bx,BUF3 ;pointer of buffer3
 18         mov ax,0
 19         mov cx,0
 20         mov dx,0
 21         mov ah,N1 ;number of N1
 22         mov al,N2 ;number of N2
 23         mov cl,N1
 24         add cl,N2 ;number of N1+N2
 25         mov dx,word ptr N2 ;number of N1+N2-1
 26         dec dx
 27         mov dh,0
 28         add dl,ah
 29 
 30         mov ax,word ptr N1 ;number of N1-1
 31         dec ax
 32         mov ah,0
 33     while:
 34         cmp si,ax
 35         ja b2
 36         cmp di,dx
 37         ja b1
 38         push dx
 39         mov dx,[di]
 40         cmp [si],dl
 41         pop dx
 42         ja b2        ;buf1[si] > buf2[di]
 43         jb b1         ;buf1[si] < buf2[di]
 44         push dx
 45         push ax
 46         mov dx,[si]
 47         mov [bx],dl;buf1[si] == buf2[di] 
 48         add dl,30h
 49         mov ah,2h
 50         int 21h
 51         pop ax
 52         pop dx
 53         inc si
 54         inc di
 55         inc bx
 56         dec cx
 57         jmp tail
 58     b2:    
 59         push dx
 60         push ax
 61         mov dx,[di]
 62         mov [bx],dl
 63         add dl,30h
 64         mov ah,2h
 65         int 21h
 66         pop ax
 67         pop dx
 68         inc bx
 69         inc di
 70         jmp tail
 71     b1:
 72         push dx
 73         push ax
 74         mov dx,[si]
 75         mov [bx],dl
 76         add dl,30h 
 77         mov ah,2h
 78         int 21h
 79         pop ax
 80         pop dx
 81         inc bx
 82         inc si
 83     tail:
 84     loop while
 85         call crlf
 86         lea dx,string
 87         mov ah,09h
 88         int 21h
 89         
 90         mov ax,4c00h
 91         int 21h
 92 
 93     crlf proc near
 94         push dx
 95         mov dl,0ah
 96         mov ah,2h
 97         int 21h
 98         mov dl,0dh
 99         mov ah,2h
100         int 21h
101         pop dx
102         ret
103     crlf endp
104 code ends
105 end start
View Code

       

4.1

 1 data segment
 2 string0 db 'Now value of n*(n+1)= 1,and value of S= 1$'
 3 string1 db 'Now value of n*(n+1)= $'
 4 string2 db ',and value of S= $'  
 5 string3 db 'Eventually value of S= $' 
 6 data ends
 7 ; calculate 1 + 2*3 + 3*4 + 4*5 +......+n*(n+1) until n*(n+1)>200
 8 code segment
 9 start:
10     ;-------initialization-------
11     mov ax,data
12     mov ds,ax
13     lea dx,string0
14     mov ah,09h
15     int 21h
16     call crlf
17     mov ax,2  ; ax=2
18     mov bx,ax
19     inc bx    ; bx=3 : bx-1 = ax
20     mov cx,15 ; 大概循环15次
21     mov dx,1  ; dx=1 : dx store the value S
22 for:
23     imul bl   ; al = n*(n+1)
24     cmp al,200
25     ja outside
26     add dx,ax ; dx = S
27     push bx
28     push cx
29     push dx
30     push ax
31     lea dx,string1
32     mov ah,09h
33      int 21h
34     pop ax
35     call print
36     ;push ax
37     lea dx,string2
38     mov ah,09h
39      int 21h
40      ;pop ax
41      pop dx
42      mov ax,dx
43      push dx
44      call print
45      call crlf
46      pop dx
47      pop cx
48      pop bx
49     mov ax,bx
50     inc bx
51 loop for
52 outside:
53     mov ax,dx
54     push ax
55     lea dx,string3 ;output string3
56      mov ah,09h
57      int 21h
58      pop ax
59     call print
60     mov ax, 4c00h
61     int 21h
62 
63 crlf:
64     PUSH AX
65     PUSH DX
66      MOV DL,0AH
67      MOV AH,2H
68      INT 21H
69      MOV DL,0DH
70      MOV AH,2H
71      INT 21H
72      POP DX
73      POP AX
74      RET
75 print: 
76     push ax
77     mov cx,0
78     mov bx,10
79 disp1:
80      mov dx,0
81 div bx
82 push dx
83     inc cx
84     or ax,ax
85     jne disp1
86 disp2:
87     mov ah,2
88     pop  dx
89     add  dl, 30H
90     int 21h
91 loop disp2
92     pop ax
93     ret 
94     
95 code ends
96     end start
View Code

       

4.2

 1 data segment
 2   string0 db 'Welcome to here!Input a number: $'
 3   string1 db 'Inlegal character! $'
 4   string2 db 'Result of N! = $'
 5 data ends
 6 
 7 code segment
 8     assume cs:code,ds:data
 9     start:
10         mov ax,data
11         mov ds,ax
12         lea dx,string0
13         mov ah,09h
14         int 21h
15         mov ah,01h ;input number N
16          int 21h
17          call CRLF
18          cmp al,38h ; N <= 8
19         ja inlegal
20         cmp al,30h ; N >= 0
21         jb inlegal
22         je part01
23         cmp al,31h
24         je part01
25         sub al,30h
26         mov cx,0
27         mov cl,al
28         dec cl     ;cx = N-1
29         mov ax,1
30         mov bx,2
31         mov dx,0   ; store the value of N!
32     mul:
33         imul bx
34         mov dx,ax
35         inc bx
36     loop mul
37         push dx    ; dx = N!
38         lea dx,string2
39         mov ah,09h
40         int 21h
41         pop ax     ; ax = N!
42         call print
43     tail:
44         mov ax, 4c00h
45         int 21h
46     part01:
47         lea dx,string2
48         mov ah,09h
49         int 21h
50         mov ax,1
51         call print
52         jmp tail
53     inlegal:
54         lea dx,string1 ;output string1
55           mov ah,09h
56           int 21h
57           jmp tail
58     print:
59         push ax
60         mov cx,0
61         mov bx,10
62     disp1:
63           mov dx,0
64         div bx
65         push dx
66         inc cx
67         or ax,ax
68         jne disp1
69     disp2:
70         mov ah,2
71         pop  dx
72         add  dl,30H
73         int 21h
74     loop disp2
75         pop ax
76         ret
77     CRLF:
78         PUSH AX
79         PUSH DX
80          MOV DL,0AH
81          MOV AH,2H
82          INT 21H
83          MOV DL,0DH
84          MOV AH,2H
85          INT 21H
86         POP DX
87          POP AX
88           RET
89 ends
90 end start
View Code

       

       

4.3       

  1 data segment
  2     string0 db '     Welcome to here!Input a string: $'
  3     string1 db '**********Count of alphabet: $'
  4     string2 db '**********Count of number: $'
  5     string3 db '**********Count of other character: $'
  6     count_alph db 0
  7     count_num db 0
  8     count_char db 0
  9 ends
 10 
 11 code segment
 12     assume cs:code,ds:data
 13 start:
 14     mov ax, data
 15     mov ds, ax
 16     lea si,count_alph
 17     lea dx,string0 ;output string0
 18      mov ah,09h
 19      int 21h
 20     mov cx,40
 21 input:
 22      mov ah,01h
 23      int 21h
 24      cmp al,0dh ;enter \r\n
 25      je printf_count
 26     cmp al,48  ;'0'
 27     jb add_char
 28     cmp al,58  ;'9'+1
 29     jb add_num
 30     cmp al,65  ;'A'
 31     jb add_char
 32     cmp al,91  ;'Z'+1
 33     jb add_alph
 34     cmp al,97  ;'a'
 35     jb add_char
 36     cmp al,123 ;'z'+1
 37     jb add_alph
 38     jmp add_char
 39 inside:
 40 loop input
 41 printf_count:
 42     call crlf
 43     call crlf
 44     lea dx,string1 ;output string1
 45      mov ah,09h
 46      int 21h
 47      mov al,[si]
 48      mov ah,0
 49      call print
 50      call crlf
 51      call crlf
 52      lea dx,string2 ;output string2
 53      mov ah,09h
 54      int 21h
 55      mov al,[si+1]
 56      mov ah,0
 57      call print
 58      call crlf
 59      call crlf
 60      lea dx,string3 ;output string3
 61      mov ah,09h
 62      int 21h
 63      mov al,[si+2]
 64      mov ah,0
 65      call print
 66 tail:
 67     mov ax, 4c00h
 68     int 21h
 69 crlf:
 70     PUSH AX
 71     PUSH DX
 72      MOV DL,0AH
 73      MOV AH,2H
 74      INT 21H
 75      MOV DL,0DH
 76      MOV AH,2H
 77      INT 21H
 78      POP DX
 79      POP AX
 80      RET
 81 print:
 82     push ax
 83     mov cx,0 
 84     mov bx,10
 85 disp1:
 86      mov dx,0
 87     div bx
 88     push dx
 89     inc cx
 90     or ax,ax
 91     jne disp1
 92 disp2:
 93     mov ah,2
 94     pop  dx 
 95     add  dl, 30H
 96     int 21h
 97 loop disp2 
 98     pop ax
 99     ret
100 add_alph:
101     push dx
102     mov dl,[si]
103     inc dl
104     mov [si],dl
105     pop dx
106     jmp inside
107 add_num:
108     push dx
109     mov dl,[si+1]
110     inc dl
111     mov [si+1],dl
112     pop dx
113     jmp inside
114 add_char:  
115     push dx
116     mov dl,[si+2]
117     inc dl
118     mov [si+2],dl
119     pop dx
120     jmp inside
121 ends
122 end start
View Code

       


  上面很多题目都涉及到了dos输出,其实还挺有意思的。下面的代码是把ax寄存器中保存的十六进制值打印出来的函数printf:

code segment
start:     
    mov ax,245
    call printf     
    mov ax,1234
    call printf
    mov ax,2222
    call printf
    
    mov ax, 4c00h
    int 21h 
    
    printf proc near 
        push cx
        push bx
        
        push dx
        mov cx,0 
        mov bx,10
    disp1:
        mov dx,0
        div bx     ;如果除数是16位,AX储存商,DX储存余数
        push dx 
        inc cx
        or ax,ax
        jne disp1  ;ax!=0 then jump to disp1
    disp2:         ;stack top 2-4-5 stack bottom ,cx=3 means 3 elements in stack 
        mov ah,2
        pop  dx 
        add  dl, 30H  ;print :02h->32h ('2') 04h->34h ('4') 05h->35h ('5')    
        int 21h
        loop disp2  
        call crlf
        pop dx
        pop bx
        pop cx
        ret   
    printf endp 
    
    crlf proc near   
        push ax
        push dx
        mov dl,0ah
        mov ah,2h
        int 21h
        mov dl,0dh
        mov ah,2h
        int 21h
        pop dx
        pop ax
        ret
    crlf endp
    
code ends
end start 

          

   输入一个小于100的正整数(两位以内)用了input函数,关键在于指令aad:

code segment
start:        
    mov ax,0
    call input
    mov ax,bx  
    aad
    call printf
    
    mov ax, 4c00h ; exit to operating system.
    int 21h      
    
    printf proc near 
        push cx
        push bx
        
        push dx
        mov cx,0 
        mov bx,10
    disp1:
        mov dx,0
        div bx     ;如果除数是16位,AX储存商,DX储存余数
        push dx 
        inc cx
        or ax,ax
        jne disp1  ;ax!=0 then jump to disp1
    disp2:         ;stack top 2-4-5 stack bottom ,cx=3 means 3 elements in stack 
        mov ah,2
        pop  dx 
        add  dl, 30H  ;print :02h->32h ('2') 04h->34h ('4') 05h->35h ('5')    
        int 21h
        loop disp2  
        call crlf
        pop dx
        pop bx
        pop cx
        ret   
    printf endp
    
    input proc
        mov bx,0
        mov ah,1    ;first char
        int 21h
        sub al,30h
        mov bl,al
        mov ah,1    ;second char
        int 21h
        cmp al,0dh  ;compare if al==Enter
        je exit
        sub al,30h
        mov cl,8     ;shift operation
        rol bx,cl
        mov bl,al
        jmp exit
        exit: 
            call crlf
        ret
    input endp
    
    crlf proc near
        mov dl,0ah  
        mov ah,2h
        int 21h
        mov dl,0dh  
        mov ah,2h
        int 21h
        ret
    crlf endp  
ends
end start 

                          

posted @ 2019-11-23 17:22  爱吃砂糖橘的白龙  阅读(5129)  评论(0编辑  收藏  举报