Search code examples
assemblyinterruptbiosemu8086systemtime

Get system time with BIOS interrupt in assembly


I want to get the time(hours, minutes, second) with the interrupt INT 1Ah/AH = 00h. I know that it keeps in CX the High-order part of clock count and in DX the Low-order part of clock count.

I searched about it, and found the following formulas convert the clock count to the time of day:

                             Hour      = Clock / 65543 (1007h)
                             Remainder = Clock MOD 65543

                             Minutes   = Remainder / 1092 (444h)
                             Remainder = Remainder MOD 1092

                             Second    = Remainder / 18.21
                             Remainder = Remainder MOD 18.21

But I am confused about how to use them, how can I get clock from CX and DX? Should I read into a variable?


Solution

  • Don't use that interrupt, use INT 1Ah/02 instead. If you insist, note that Clock is just the 32 bit value formed from CX and DX. Putting them into a variable won't help you at all. Since you need to divide it, and the DIV instruction expects dividend in DX (high) and AX (low) you just have to shuffle it around a little. You will run into problems at dividing by 18.21 though. Something like this could work, but note that the constant 1092 is not accurate either. You might end up with 60 for minutes if you are unlucky (if the clock mod 65543 happens to be above 65519, dividing by 1092 will give you 60.)

    mov ax, dx           ; low 16 bits
    mov dx, cx           ; high 16 bits
    div word [mem_65543] ; constant in memory
    mov [hours], ax
    mov ax, dx           ; work with remainder
    xor dx, dx           ; zero top bits
    div word [mem_1092]  ; constant in memory
    mov [minutes], ax
    mov ax, [mem_100]    ; constant in memory
    mul dx               ; scale by 100
    div word [mem_1821]  ; constant in memory
    mov [seconds], ax