以秒为单位获取UNIX时间戳

时间:2019-03-29 16:19:54

标签: assembly dos unix-timestamp x86-16

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

1 个答案:

答案 0 :(得分:2)

  

如何获取8086 asm上的当前时间?

问题是:“您使用的是什么操作系统?”

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

  

如何获取8086 asm上的当前UNIX时间?

如果您的操作系统不支持UNIX时间,但支持以“日期”(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
    ...

如果您确实需要这样做,我确定您可以将伪代码转换为汇编器。