Author Topic: Printing current year  (Read 6732 times)

Offline sledge

  • Jr. Member
  • *
  • Posts: 19
Printing current year
« on: August 10, 2011, 09:09:13 PM »
Code: [Select]
org 100h

;print msg
mov ah, 9
mov dx, msg
int 21h

;store current year at register CX
mov ah, 2Ah
int 21h

;attempt to print year
mov ah, 9
mov dx, cx
int 21h

mov ah, 4Ch
int 21h
msg db "Current Year: ", '$'

I coded this hoping to print the current year (2011).
How can I make this work?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Printing current year
« Reply #1 on: August 11, 2011, 12:17:26 AM »
int 21h/9 expects the address of a $-terminated string in dx, not a number. Dos doesn't have any "display a number" function. Try calling this instead: "mov ax, cx" first, of course)

Code: [Select]
    push ax
    push bx
    push cx
    push dx

    mov bx, 10          ; divide by ten
    xor cx, cx          ; zero our counter
    xor dx, dx          ; clear dx for the div
    div bx              ; dx:ax/bx -> ax quotient, dx remainder
    push dx             ; save remainder
    inc cx              ; bump digit counter
    or ax, ax           ; is quotient zero?
    jnz .push_digit     ; no, do more

    mov ah, 2           ; print character subfunction
    pop dx              ; get remainder back
    add dl, '0'         ; convert to ascii character
    int 21h             ; print it
    loop .pop_digit     ; cx times

    pop dx
    pop cx
    pop bx
    pop ax

I suppose I'll attach the file with hex and binary routines in it, too...

(you can "just call printf"... but I don't think it'll do binary. :) )