edwardcmh

人氣不過肥皂泡

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

软件07汇编语言程序设计课程设计参考答案

 

课程设计1:键盘输入数据的转换与显示程序

 

编写一个程序,能从键盘读入两个5位十进制数(1位符号位和4位数值位),并将这两个十进制数分别转换为二进制数,然后求其和,再将和以十进制形式进行显示。

要求:提示从键盘输入第一个数,按回车完成输入;然后提示从键盘输入第二个数,按回车完成输入。然后显示输入的两个数的和。能够从键盘输入-9999~+9999之间的数值。

提示:该程序主要练习十进制数和二进制数之间的相互转换。从键盘输入的数据一般是用十进制表示的,而这些数在计算机内存储、运算时一般用二进制,这就涉及到十进制数向二进制数的转换问题;当计算结果需要显示或打印时,又要将二进制数转换为十进制数,这又涉及到二进制数如何转换为十进制的问题。建议用两个子程序来分别完成ASCII码转换为二进制数和二进制数转换为对应十进制数的ASCII码的功能。

 

参考:教材P55  INT 21H0AH号功能的介绍。

 

;*****************************************

;proj_1.asm

;*****************************************

 

OUTCHAR MACRO CHAR

         MOV AH,2

         MOV DL,CHAR

         INT 21H

endm

        

OUTSTRING MACRO STRING

         MOV AH,9

         LEA DX,STRING

         INT 21H

endm

 

INCHAR MACRO

         MOV AH,1

         INT 21H

endm

 

INSTRING MACRO STRING

         MOV AH,10

         LEA DX,STRING

         INT 21H

endm

 

DATA SEGMENT

         STR1 DB 0DH,0AH,'Please input an integer:$'

         STR2 DB 0DH,0AH,'Please input another integer:$'

         STR3 DB 0DH,0AH,'The result is:$'

         BUF DB 6

             DB ?

             DB 6 DUP(?)

         ARRAY DW 2 DUP(0)

DATA ENDS

 

STACK SEGMENT

         DB 200 DUP(0)

STACK ENDS

 

CODE SEGMENT

         ASSUME CS:CODE,SS:STACK,DS:DATA

         BEGIN:

                   MOV AX,DATA

                   MOV DS,AX

                  

                   OUTSTRING STR1

                   INSTRING BUF

                  

                   LEA DI,ARRAY

                   CALL FAT10

                  

                   OUTSTRING STR2

                   INSTRING BUF

                  

                   CALL FAT10

                  

                   OUTSTRING STR3

                  

                   MOV AX,ARRAY

                   MOV BX,ARRAY+2

                   ADD AX,BX

                  

                   CALL F2T10

                  

         QUIT:        MOV AH,4CH

                   INT 21H

 

 

;子程序名:FAT10(From ASCII To 10)

;功能:将内存缓冲区内连续输入的ASCII码转换成十进制数,存放在ARRAY

;入口参数:DI--转换后的十进制数字存放地址

;出口参数:DI--指向最后转换的十进制数字的下一个字

         FAT10 PROC

                  

                   PUSH AX

                   PUSH BX

                   PUSH CX

                   PUSH DX

                   PUSH SI

                   CALL CONVERT

                   LEA SI,BUF+3

                   MOV AL,[SI]

                   MOV AH,0

                   MOV CL,BUF+1

                   MOV CH,0

                   SUB CX,2

                   CMP CX,0

                   JZ T1

         LOP: INC SI

                   MOV BL,[SI]

                   MOV BH,0

                   MOV DX,10

                   IMUL DX

                   ADD AX,BX

                   LOOP LOP

         T1:   CMP BUF+2,'+'

                   JZ T2

                   NEG AX

                   OR AX,8000H

         T2:   MOV [DI],AX

                   ADD DI,2

                   POP SI

                   POP DX

                   POP CX

                   POP BX

                   POP AX

                   ret

 

         FAT10 endp

 

 

;子程序名:CONVERT

;功能:将内存缓冲区内连续输入的ASCII码转换成数字

         CONVERT PROC

                  

                   PUSH CX

                   PUSH SI

                   MOV CL,BUF+1

                   MOV CH,0

                   DEC CX

                   LEA SI,BUF+3

         LOP: SUB BYTE PTR[SI],30H

                   INC SI

                   LOOP LOP

                   POP SI

                   POP CX

                   ret

 

         CONVERT endp

 

 

;子程序名:F2T10

;功能:将AX16位带符号二进制数转换成十进制数,并显示

         F2T10 PROC NEAR

             PUSH BX

             PUSH DX

             PUSH SI

             LEA SI,BUF

             OR AX,AX

             JNS PLUS           ;(AX)0PLUS

             NEG AX

             MOV [SI],BYTE PTR '-'      ;负数输出''

             INC SI

     PLUS:     MOV BX,10                ;基数10BX

               CALL RADIX               ;调用二进制转换成十进制子程序

               MOV [SI],BYTE PTR '$'

               MOV DX,OFFSET BUF

               MOV AH,9

               INT 21H                      ;显示转换后的十进制数

               POP SI

               POP DX

               POP BX

               RET 

         F2T10 ENDP

 

;子程序名:RADIX

;功能:将二进制转换成十进制

;入口参数:AX--存放待转换的十六位二进制数;BX--存放要转换的数制基数P;SI--存放P进制的字节存储区首址

;出口参数:所求P进制数各位数字的ASCII码按高位在前、低位在后的顺序存放在以SI为指针字节存储区,SI指向最后一个ASCII码的下一字节

         RADIX PROC NEAR

                   PUSH CX

             PUSH DX

             MOV CX,0

            LP1:        MOV DX,0

             DIV BX

             PUSH DX

             INC CX

             OR AX,AX

             JNE LP1

            LP2:        POP AX

             CMP AL,10

             JB K1

             ADD AL,7

           K1:          ADD AL,30H

             MOV [SI],AL

             INC SI

             DEC CX

             JNZ LP2

             POP DX

             POP CX

             RET

         RADIX ENDP

 

CODE ENDS

         END BEGIN

 

课程设计2:显示系统的当前日期和时间。

 

要求设计界面提示信息为:“Please input DATE(D) OR TIME(T) OR QUIT(Q):”,从键盘键入“D”,可显示系统日期。从键盘输入“T”,可显示系统时间。从键盘输入“Q”,退出程序。利用DOS的系统功能调用的2AH2CH功能调用,读取系统日期和时间。将日期和时间转换为相应的ASCII码字符,显示在屏幕上。

 

;*****************************************

;proj_2.asm

;*****************************************

 

OUTCHAR MACRO CHAR

         MOV AH,2

         MOV DL,CHAR

         INT 21H

         ENDM

        

OUTSTR MACRO STRING

         MOV AH,9

         LEA DX,STRING

         INT 21H

         ENDM

        

 

DATA SEGMENT

         STR1 DB 0DH,0AH,'Please input DATE(D) OR TIME(T) OR QUIT(Q):$'

         STR2 DB 0DH,0AH,'Input Error!$'

         STR3 DB 0DH,0AH,'Current Date:$'

         STR4 DB 0DH,0AH,'Current Time:$'

         BUF DB 20 DUP(?)

DATA ENDS

 

STACK SEGMENT

         DB 200 DUP(0)

STACK ENDS

 

CODE SEGMENT

         ASSUME CS:CODE,SS:STACK,DS:DATA

         BEGIN:    

                   MOV AX,DATA

                   MOV DS,AX

                  

         LOP: OUTSTR STR1

        

                   MOV AH,0

                   INT 16H

                  

                   OUTCHAR AL

                  

                   CMP AL,'D'

                   JZ DATE

                   CMP AL,'T'

                   JZ TIME

                   CMP AL,'Q'

                   JZ QUIT

                   OUTSTR STR2

                   JMP LOP

                  

         DATE:       CALL OUTDATE

                   JMP QUIT

                  

         TIME:       CALL OUTTIME

                   JMP QUIT

        

         QUIT:        MOV AH,4CH

                   INT 21H

                  

;子程序名:OUTDATE

;功能:显示系统日期

         OUTDATE PROC

                   OUTSTR STR3

                   MOV AH,2AH

                   INT 21H

                   MOV AX,CX

                   CALL F2T10

                   PUSH DX

                   OUTCHAR '-'

                   POP DX

                   MOV AL,DH

                   MOV AH,0

                   CALL F2T10

                   PUSH DX

                   OUTCHAR '-'

                   POP DX

                   MOV AL,DL

                   MOV AH,0

                   CALL F2T10

                   ret

 

         OUTDATE endp

 

;子程序名:OUTTIME

;功能:显示系统时间

         OUTTIME PROC

                   OUTSTR STR4

                   MOV AH,2CH

                   INT 21H

                   MOV AL,CH

                   MOV AH,0

                   CALL F2T10

                   PUSH DX

                   OUTCHAR ':'

                   POP DX

                   MOV AL,CL

                   MOV AH,0

                   CALL F2T10

                   PUSH DX

                   OUTCHAR ':'

                   POP DX

                   MOV AL,DH

                   MOV AH,0

                   CALL F2T10

                   ret

 

         OUTTIME endp

                  

;子程序名:F2T10

;功能:将AX16位带符号二进制数转换成十进制数,并显示

         F2T10 PROC NEAR

             PUSH BX

             PUSH DX

             PUSH SI

             LEA SI,BUF

             OR AX,AX

             JNS PLUS           ;(AX)0PLUS

             NEG AX

             MOV [SI],BYTE PTR '-'      ;负数输出''

             INC SI

     PLUS:     MOV BX,10                ;基数10BX

               CALL RADIX               ;调用二进制转换成十进制子程序

               MOV [SI],BYTE PTR '$'

               MOV DX,OFFSET BUF

               MOV AH,9

               INT 21H                      ;显示转换后的十进制数

               POP SI

               POP DX

               POP BX

               RET 

         F2T10 ENDP

 

;子程序名:RADIX

;功能:将二进制转换成十进制

;入口参数:AX--存放待转换的十六位二进制数;BX--存放要转换的数制基数P;SI--存放P进制的字节存储区首址

;出口参数:所求P进制数各位数字的ASCII码按高位在前、低位在后的顺序存放在以SI为指针字节存储区,SI指向最后一个ASCII码的下一字节

         RADIX PROC NEAR

                   PUSH CX

             PUSH DX

             MOV CX,0

            LP1:        MOV DX,0

             DIV BX

             PUSH DX

             INC CX

             OR AX,AX

             JNE LP1

            LP2:        POP AX

             CMP AL,10

             JB K1

             ADD AL,7

           K1:          ADD AL,30H

             MOV [SI],AL

             INC SI

             DEC CX

             JNZ LP2

             POP DX

             POP CX

             RET

         RADIX ENDP

 

CODE ENDS

         END BEGIN

 

课程设计3:产生随机数并计算的程序

 

编程实现产生两个随机数,并能对产生的随机数进行加法和减法运算。利用BIOS功能调用的时钟中断调用(1AH中断调用),读取时钟计数器的值,利用随时变化的时间计数值产生随机数并进行加法或减法运算。然后将产生的随机数和运算结果转换为相应的十六进制数表示的ASCII码字符,并将其显示在屏幕上。

 

提示:随机数可以通过读取时钟计数器的值来得到。时钟计数器的值可以通过BIOS中断调用1AH0号功能来实现。

MOV AH, 0

INT 1AH

功能:读时钟计数,计数速率约为每秒18.2次。

返回参数:(CX)=计数的高位部分;(DX)=计数的低位部分。如果上次读时钟到现在未超过24小时,则(AL)=0,否则(AL0

 

;*****************************************

;proj_3.asm

;*****************************************

 

OUTCHAR MACRO CHAR

         MOV AH,2

         MOV DL,CHAR

         INT 21H

         ENDM

        

OUTSTR MACRO STRING

         MOV AH,9

         LEA DX,STRING

         INT 21H

         ENDM

        

 

DATA SEGMENT

         CRLF DB 0DH,0AH,'$'

DATA ENDS

 

STACK SEGMENT

         DB 200 DUP(0)

STACK ENDS

 

CODE SEGMENT

         ASSUME CS:CODE,SS:STACK,DS:DATA

         BEGIN:    

                   MOV AX,DATA

                   MOV DS,AX

                  

                   MOV AH,0

                   INT 1AH

                  

                   CALL ADDI

                  

                   PUSH DX

                   OUTSTR CRLF

                   POP DX

                  

                   CALL SUBT

                  

         QUIT:        MOV AH,4CH

                   INT 21H

 

;子程序名:ADDI

;功能:对产生的随机数进行加法运算

;入口参数:CX--计数的高位部分,DX--计数的低位部分

;出口参数:运算结果用十六进制数显示

         ADDI PROC

                  

                   MOV AX,CX

                   CALL H_DISP

                   PUSH DX

                   OUTCHAR '+'

                   POP DX

                   MOV AX,DX

                   CALL H_DISP

                   PUSH DX

                   OUTCHAR '='

                   POP DX

                   PUSH CX

                   ADD CX,DX

                   MOV AX,CX

                   CALL H_DISP

                   POP CX

                   ret

 

         ADDI endp

 

;子程序名:SUBT

;功能:对产生的随机数进行减法运算

;入口参数:CX--计数的高位部分,DX--计数的低位部分

;出口参数:运算结果用十六进制数显示

         SUBT PROC

        

                   MOV AX,CX

                   CALL H_DISP

                   PUSH DX

                   OUTCHAR '-'

                   POP DX

                   MOV AX,DX

                   CALL H_DISP

                   PUSH DX

                   OUTCHAR '='

                   POP DX

                   SUB CX,DX

                   MOV AX,CX

                   CALL H_DISP

                   ret

 

         SUBT endp

 

 

;子程序名:H_DISP

;功能:二进制转换成十六进制显示

;入口参数:AX--要转换的二进制数

;出口参数:对应的十六进制数显示输出

         H_DISP PROC NEAR

                   PUSH AX

                   PUSH BX

                   PUSH CX

                   PUSH DX

                MOV BX,AX

                MOV CH,4        ;4位二进制为一组处理,分4

        LOP: MOV CL,4         ;每组4位二进制

                ROL BX,CL

                MOV AL,BL

                AND AL,0FH     ;保留最低4

                OR AL,30H        ;转换成ASCII

                CMP AL,3AH

                   JL DISP               ;09之间直接显示

                   ADD AL,07H     ;AF之间加7后显示

        DISP:         MOV DL,AL

                MOV AH,2

                INT 21H             ;显示

                DEC CH

                JNZ LOP

                MOV DL,'H'

                MOV AH,2

                INT 21H

                POP DX

                POP CX

                POP BX

                POP AX

                RET

         H_DISP ENDP

 

CODE ENDS

         END BEGIN

 

课程设计4:键盘输入程序。

 

(练习将二进制数转换为16进制数并显示的功能。)

利用中断类型16H调用键盘I/O接口程序,并显示由键盘中断程序送回的扫描码和ASCII码。显示的格式如下:

SCAN                  ASCII                  CHAR

1E                       61                       a

30                       62                       b

2E                        63                       c

CTRL+C可以中止程序。CTRL+C对应的ASCII码为03H

参考教材P1497-6中的子程序将二进制数以十六进制的形式在屏幕上显示出来。

 

;*****************************************

;proj_4.asm

;*****************************************

 

OUTCHAR MACRO CHAR

         MOV AH,2

         MOV DL,CHAR

         INT 21H

         ENDM

        

OUTSTR MACRO STRING

         MOV AH,9

         LEA DX,STRING

         INT 21H

         ENDM

        

 

DATA SEGMENT

         STR1 DB 'SCAN    ASCII    CHAR',0DH,0AH,'$'

         STR2 DB '      $'

         CRLF DB 0DH,0AH,'$'

DATA ENDS

 

STACK SEGMENT

         DB 200 DUP(0)

STACK ENDS

 

CODE SEGMENT

         ASSUME CS:CODE,SS:STACK,DS:DATA

         BEGIN:    

                  MOV AX,DATA

                   MOV DS,AX

                  

                   OUTSTR STR1

                  

         LOP: MOV AH,0

                   INT 16H

                  

                   CMP AL,03H

                   JZ QUIT

                  

                   CALL KEY_DISP

                   JMP LOP

                  

         QUIT:        MOV AH,4CH

                   INT 21H

 

;子程序名:KEY_DISP

;功能:显示由键盘中断程序送回的扫描码和ASCII

;入口参数:AH-键盘扫描码 AL-字符的ASCII

;出口参数:顺序显示键盘扫描码和字符的ASCII

         KEY_DISP PROC

                  

                   MOV BX,AX

                   MOV AL,BH

                   CALL H_DISP

                   OUTSTR STR2

                   MOV AL,BL

                   CALL H_DISP

                   OUTSTR STR2

                   OUTCHAR BL

                   OUTSTR CRLF

                   ret

 

         KEY_DISP endp

 

 

;子程序名:H_DISP

;功能:二进制转换成十六进制显示

;入口参数:AL--要转换的二进制数

;出口参数:对应的十六进制数显示输出

         H_DISP PROC NEAR

        

                   PUSH AX

                   PUSH BX

                MOV BL,AL

                MOV CH,2        ;4位二进制为一组处理,分4

        H_LOP:     MOV CL,4         ;每组4位二进制

                ROL BL,CL

                MOV AL,BL

                AND AL,0FH     ;保留最低4

                OR AL,30H        ;转换成ASCII

                CMP AL,3AH

                   JL DISP               ;09之间直接显示

                   ADD AL,07H     ;AF之间加7后显示

        DISP:         MOV DL,AL

                MOV AH,2

                INT 21H             ;显示

                DEC CH

                JNZ H_LOP

                MOV DL,'H'

                MOV AH,2

                INT 21H

                POP BX

                POP AX

                RET

         H_DISP ENDP

 

CODE ENDS

         END BEGIN

 

posted on 2009-07-29 14:45  edwardcmh  阅读(673)  评论(0编辑  收藏  举报