1

I'm trying to make commands that return time and date in a 16 bit real mode OS. When I read from the RTC, however, some wierd values are returned. According to the RTC, there seems to be 90 seconds per minute and 90 minutes per hour.

Below, you see a snippet of code which should print between 0 and 59 seconds, but prints between 0 and 89:

get_seconds:
  xor ax, ax        ; clear AX
  mov al, 0x00      ; get seconds
  out 0x70, al      ; set index
  in al, 0x71       ; load index to AL
  call print_decimal        ; print contents of AX as decimal

print_decimal:
  lea si, [decimal_str+9]   ; point SI to last byte in buffer
  mov cx, 10        ; 10 = divisor for base 10 printing

 .loop
  xor dx, dx        ; clear DX
  div cx            ; AX = AX / 10, remainder in DX
  add dl, '0'       ; convert remainder to ASCII
  dec si            ; store digits from right to left
  mov [si], dl      ; store remainder as ASCII in the buffer
  test ax,ax        ; check if AX = 0
  jz .print         ; if AX = 0, print decimal-string
  jmp .loop 

 .print
  mov bx, si        ; set BX to last stored ASCII-digit
  call print_string     ; regular int 10h string printer

 .done 
  ret

decimal_str: times 10 db 0  ; buffer
bjarke15
  • 85
  • 5
  • 4
    The default mode for the RTC is BCD. – Jester Mar 27 '18 at 23:38
  • 4
    Have you read [RTC@OSDev.org](https://wiki.osdev.org/RTC)? – zx485 Mar 27 '18 at 23:40
  • I've just briefly looked at your code. What kind of things are returned? Does it print funny ASCII characters out? If it does, you need to make something that displays it as a printable number. – Steve Woods Mar 28 '18 at 14:24
  • I have read the OSDev article on RTC and also the one on reading from CMOS, which I used for reference when writing my code. My code has no problems printing the BCD from RTC. It even takes a real minute for the second counter to carry to minutes. It just counts to 90 instead of 60. – bjarke15 Mar 28 '18 at 15:26
  • However, there seems to be a jump from 9 s to 16 s, which hints at some decimals being mistaken for hex. I'll look into it. – bjarke15 Mar 28 '18 at 15:34

1 Answers1

2

I found the problem. Dividing AX by 10 treats it as a hexadecimal while it is actually BCD. So to fix my problem, I interchanged:

mov cx, 10

For:

mov cx 16
bjarke15
  • 85
  • 5