显示当前月份到屏幕

Tutorial: 汇编基础 Category: C语言 Published: 2026-04-07 13:58:26 Views: 20 Likes: 0 Comments: 0
1. 获取当前日期(26 号)到屏幕
assume cs:codesg

; 1001  1000   0111  0110   0101   0100    0011   0010   0001   0000
; 9     8      7     6      5      4       3      2      1      0
; 年    月     日                  时              分            秒

codesg segment
      start:
                   call clear_screen

                   mov  al, 7
                   out  70h, al                              ; 写入地址7
                   in   al, 71h                              ; 获取日期 al = 26, AX = 0026

                   mov  ah, al
                   mov  cl, 4
                   shr  ah, cl                               ; 0010 0110 => 0000 0010 = ah = 02
                   and  al, 00001111B
      ;                     00000110 => 00000110 = al = 06
      ;                     AX = 0206, 要换成这种

                   add  al, 30H                              ; al 中为月份的个位数码值,变成字符
                   add  ah, 30H                              ; ah 中为月份的十位数码值,变成字符

                   mov  bx, 0B800H
                   mov  es, bx
                   mov  di, 169 * 12 + 40 * 2
                   mov  byte ptr es:[di + 0], ah
                   mov  byte ptr es:[di + 1], 00001010B      ; 上色
                   mov  byte ptr es:[di + 2], al
                   mov  byte ptr es:[di + 3], 00001010B      ; 上色

                   mov  ax, 4c00h
                   int  21h


      clear_screen:
                   push ax
                   push cx
                   push es
                   push di

                   mov  ax, 0B800H
                   mov  es, ax
                   mov  di, 0

                   mov  cx, 2000
                   mov  ax, 0700H                            ; 0700H 是空白

      _clear:      mov  es:[di], ax
                   add  di, 2
                   loop _clear

                   pop  di
                   pop  es
                   pop  cx
                   pop  ax

                   ret

codesg ends
end start
2. 实验 14

编程:以"年/月/日 时:分:秒"的格式,显示当前的日期、时间。

assume cs:code

; 1001  1000   0111  0110   0101   0100    0011   0010   0001   0000
; 9     8      7     6      5      4       3      2      1      0
; 年    月     日                  时              分            秒

data segment
             db '9/8/7 4:2:0'        ; 数据段存放端口地址和中间字符
data ends

code segment
        start:
                mov  ax,data
                mov  ds,ax                           ; 段寄存器DS指向数据段
        ; -d ds:0
        ; 0E24:0000  39 2F 38 2F 37 20 34 3A-32 3A 30 00 00 00 00 00   9/8/7 4:2:0.....
                mov  ax,0b800h
                mov  es,ax                           ; 段寄存器ES指向显示缓冲区

                mov  bx,160*12+20*2                  ; BX存放显示偏移
                mov  si,0
                mov  di,0                            ; 分别用于索引源段和目的段
                mov  cx,11                           ; 循环次数

        s:
                push cx                              ; 保护(CX)
                mov  cl,ds:[si]                      ; 取字符
                cmp  cl,30h                          ; 十进制 0
                jb   show_ch                         ; ASCII码值小于30h则转移至show_ch
                cmp  cl,57                           ; 十进制 9
                ja   show_ch                         ; ASCII码值大于57则转移至show_ch

                call get_ch
                pop  cx                              ; 恢复(CX)

                loop s
                mov  ax,4c00h
                int  21h

        show_ch:
                mov  es:[bx+di],cl
                mov  byte ptr es:[bx+di+1],2h
                inc  si                              ; 偏移1个字节取字符
                add  di,2                            ; 偏移2个字节写字符
                pop  cx                              ; 恢复(CX)
                sub  cx,1
                jmp  far ptr s                       ; 段间转移至s处,调用show_ch后跳过get_ch及后续代码
        get_ch:
                sub  cl,30h                          ; 字符转换为整数
                mov  al,cl                           ; 传入指定端口号
                out  70h,al
                in   al,71h                          ; 取对应端口的内容,高4位为十位、低4位为个位
                mov  ah,al
                mov  cl,4
                shr  ah,cl                           ; 高4位
                and  al,00001111b                    ; 低4位
                add  ah,30h                          ; 转换为十进制形式

                mov  es:[bx+di],ah
                mov  byte ptr es:[bx+di+1],2h
                add  di,2                            ; 偏移2个字节写字符
                add  al,30h
                mov  es:[bx+di],al
                mov  byte ptr es:[bx+di+1],2h
                add  di,2                            ; 偏移2个字节写字符
                inc  si
                ret                                  ; 子程序返回
code ends
end start