以秒为单位获取 UNIX 时间戳

Get UNIX timestamp in seconds

如何获取 8086 asm 上的当前 UNIX 时间?我在互联网上找不到任何信息。我知道它是 32 位的,所以它必须填充 2 个寄存器。 编辑:OS 是 DOS(我正在使用 Dosbox)

How can I get the current ... time on 8086 asm?

问题是:"What operating system are you using?"

A CPU 没有任何实时时钟。甚至早期的 x86 PC 也没有。操作系统(或BIOS)知道如何获取系统时间。

How can I get the current UNIX time on 8086 asm?

如果您的操作系统不支持 UNIX 时间但支持 "date" (Y,M,D,h,m,s) 的时间,则只有 很难:

这里是伪代码:

; Calculate the number of days from 1970/1/1 to 1/1 of this year
; not including the 29ths of February
time = (year-1970)*365

; Calculate the number of 2/29s (leap years) passed since
; 1970/1/1 until today (including 2/29 of this year if this
; year is a leap year!)
leapyears = year-1969
if month > 2:
    ; It is March or later; the 29th of February (if any)
    ; has already passed this year.
    leapyears = leapyears + 1
end-if

; Here "leapyears" has the following value:
;   1970: 1 or 2 => x/4 = 0
;   1971: 2 or 3 => x/4 = 0
;   1972: 3 or 4 => x/4 = 0 or 1
;   1973: 4 or 5 => x/4 = 1
;   ...
;   2019: 50 or 51 => x/4 = 12
;   2020: 51 or 52 => x/4 = 12 or 13
;   2021: 52 or 53 => x/4 = 13
;   ...

; This is a division by 4. You can do it using two SHR 1
; instructions: "SHR register, 1"
leapyears = leapyears SHR 2

; Add the result to the number of days from 1970/1/1
; until today
time = time + leapyears

; Look up the number of days from 1/1 of this year until
; the 1st of this month; not including 2/29 (if any)
; You could do this the following way:
;    LEA BX, lookupTable
;    ADD BX, month (in a 16-bit register)
;    MOV AX, [BX]
; Or:
;    LEA BX, lookupTable
;    ADD BL, month (in an 8-bit register)
;    ADC BH, 0
;    MOV AX, [BX]
yearDays = lookupTable[month]

; And add this number to the number of days
time = time + yearDays

; Add the day-of-month to the number of days since 1970/1/1
time = time + dayOfMonth - 1

; Convert days to seconds (note: We assume: No leap seconds)
; and add hours, minutes and seconds (note: We assume: GMT time zone)
time = 24 * time
time = time + hours
time = 60 * time
time = time + minutes
time = 60 * time
time = time + seconds

---
lookupTable:
    0         # illegal
    0         # Days between 1/1 and 1/1
    31        # Days between 2/1 and 1/1
    31+28     # Days between 3/1 and 1/1
    31+28+31  # Days between 4/1 and 1/1
    ...

我相信您能够将伪代码转换为汇编程序 - 如果您真的需要这样做的话。