编程,以“年/月/日 时:分:秒” 的格式,显示当前的日期、时间。
注意:cmos ram 中存储者系统的配置信息,除了保存时间信息的单元外,不要想其他单元写入内容,否则将引起一些系统错误。
代码如下(为了显而易见,我没有额外精简代码,但实际上代码可以优化,简单优化过的代码放在第二部分)
assume cs:code
code segment
start: mov al,9
out 70h,al
in al,71h
mov ah,al
mov cl,4
shr ah,cl ;十位
and al,00001111b ;个位
add ah,30h
add al,30h
mov bx,0b800h
mov es,bx
mov es:[12*160+35*2+0],ah
mov byte ptr es:[12*160+35*2+1],2
mov es:[12*160+35*2+2],al
mov byte ptr es:[12*160+35*2+3],2 ;年份
mov byte ptr es:[12*160+35*2+4],'/'
mov al,8
out 70h,al
in al,71h
mov ah,al
mov cl,4
shr ah,cl ;十位
and al,00001111b ;个位
add ah,30h
add al,30h
mov es:[12*160+35*2+6],ah
mov byte ptr es:[12*160+35*2+7],3
mov es:[12*160+35*2+8],al
mov byte ptr es:[12*160+35*2+9],3 ;月份
mov byte ptr es:[12*160+35*2+10],'/'
mov al,7
out 70h,al
in al,71h
mov ah,al
mov cl,4
shr ah,cl ;十位
and al,00001111b ;个位
add ah,30h
add al,30h
mov es:[12*160+35*2+12],ah
mov byte ptr es:[12*160+35*2+13],4
mov es:[12*160+35*2+14],al
mov byte ptr es:[12*160+35*2+15],4 ;日
mov al,4
out 70h,al
in al,71h
mov ah,al
mov cl,4
shr ah,cl ;十位
and al,00001111b ;个位
add ah,30h
add al,30h
mov es:[12*160+35*2+20],ah ;18空格,20开始
mov byte ptr es:[12*160+35*2+21],5
mov es:[12*160+35*2+22],al
mov byte ptr es:[12*160+35*2+23],5 ;时
mov byte ptr es:[12*160+35*2+24],':'
mov al,2
out 70h,al
in al,71h
mov ah,al
mov cl,4
shr ah,cl ;十位
and al,00001111b ;个位
add ah,30h
add al,30h
mov es:[12*160+35*2+26],ah
mov byte ptr es:[12*160+35*2+27],6
mov es:[12*160+35*2+28],al
mov byte ptr es:[12*160+35*2+29],6 ;分
mov byte ptr es:[12*160+35*2+30],':'
mov al,0
out 70h,al
in al,71h
mov ah,al
mov cl,4
shr ah,cl ;十位
and al,00001111b ;个位
add ah,30h
add al,30h
mov es:[12*160+35*2+32],ah
mov byte ptr es:[12*160+35*2+33],7
mov es:[12*160+35*2+34],al
mov byte ptr es:[12*160+35*2+35],7 ;秒
mov ax,4c00h
int 21h
code ends
end start
运行结果
稍微优化的代码(把重复的部分封装成一块子程序)
assume cs:code
code segment
start: mov bx,0b800h
mov es,bx
mov al,9
mov si,0
call improve
mov byte ptr es:[12*160+35*2+4],'/'
mov al,8
add si,6
call improve
mov byte ptr es:[12*160+35*2+10],'/'
mov al,7
add si,6
call improve
mov al,4
add si,8
call improve
mov byte ptr es:[12*160+35*2+24],':'
mov al,2
add si,6
call improve
mov byte ptr es:[12*160+35*2+30],':'
mov al,0
add si,6
call improve
mov ax,4c00h
int 21h
improve:out 70h,al
in al,71h
mov ah,al
mov cl,4
shr ah,cl ;十位
and al,00001111b ;个位
add ah,30h
add al,30h
mov es:[12*160+35*2+si],ah
mov byte ptr es:[12*160+35*2+si+1],2
mov es:[12*160+35*2+si+2],al
mov byte ptr es:[12*160+35*2+si+3],2
ret
code ends
end start
运行结果