masm for windows 显示系统日期源程序
时间: 2024-11-25 16:15:17 浏览: 7
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` 函数,该函数格式化这些值并打印到屏幕上。
阅读全文