实模式下时间日期的获取及显示

 

在写一个操作系统时,时间和日期的显示功能是必不可少的,如果我们要写一个基于命令行的操作系统,通常要提供给用户两个有关时间日期的命令,即date和time。而在实模式下,可以方便地通过BIOS调用从CMOS获取当前时间及日期,然后加以处理并显示出来。BIOS中断0x1A是有关时间日期的调用,其中子功能0x02是获取当前时间,子功能0x04是获取当前日期:

需要注意的是,这两个BIOS调用的返回值是组合的BCD码,也就是说,在显示时我们需要先进行处理。所谓组合的BCD码,就是用二进制中的4位来表示一位十进制数,比如十进制数13,在二进制中表示为0x0D,而在组合式BCD码中表示为0x13。这样在组合式BCD码中一个字节就表示两位十进制数。

有了这些资料,再写date和time两个命令就比较轻松了,以下是WarmOS中的代码:

下面是显示时间的汇编代码:

下面是显示日期的代码,因为原理相似,不再详细注释,只注明寄存器值变化

好了,这样就可以为系统加上两个命令了,不过这只是在实模式下,在保护模式又另当别论了。

MASM (Microsoft Assembler) 是微软的汇编语言,用于编写Windows平台下的低级程序。如果你想在MASM中显示当前系统的日期,你可以使用INT 21H中断服务函数(IFS)来调用模式下的时间戳功能。下面是一个简单的示例: ```assembly ; Windows MASM32 示例 .model small .stack 100h .data msg db 'Current Date: $' fmt db '%d/%m/%y', 0Ah, '$' .code main proc ; 调用 DOS日期/时间函数 mov ah, 2Ch ; Function to get system date and time int 21h ; Call DOS interrupt ; 将得到的年月日转换成字符串并输出 mov dx, offset msg ; Point to message string call DispDate ; Display the date using our custom function exit main endp DispDate proc near push bx ; Save registers lea si, [dateStr] ; Load address of formatted date into SI mov al, cl ; AL holds the day value add al, 48 ; Convert from ASCII 0-9 to ASCII 48-57 stosb ; Store in buffer mov al, ch ; Hour in AL add al, 48 stosb ; Store in buffer mov al, dl ; Minute in AL add al, 48 stosb ; Store in buffer mov al, dh ; Second in AL add al, 48 stosb ; Store in buffer ; Add separator mov byte ptr [si], '/'; Separator between day and month inc si ; Move to next position mov al, bl ; Month in AL add al, 48 stosb ; Store in buffer mov al, bh ; Month number as character (0-based) add al, 48 stosb ; Store in buffer ; Add year separator and year mov byte ptr [si], '-'; Separator between month and year inc si ; Move to next position mov ax, [dx+4] ; Year is at offset +4 from INT 21H return value add ax, 1900 ; Shift to four-digit format mov bx, 100 ; Multiply by 100 for two-digit representation mul bx mov cx, ax ; High part of result shr cx, 4 ; Divide by 100 (remainder discarded) add al, cl ; Add high digit to low digit stosb ; Store in buffer mov byte ptr [si], '$'; End of line pop bx ; Restore registers ret DispDate endp dateStr db 13 dup ('$') ; Buffer to store formatted date times 510 - $$) db 0 ; Fill rest with zeros to make a valid DOS executable end main ``` 在这个示例中,`main` 函数首先通过 `int 21h` 调用 DOS的`2Ch`中断获取系统日期和时间,然后将结果传递给自定义的 `DispDate` 函数,该函数格式化这些值并打印到屏幕上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值