; ---------------------------------------------------------------------
; SHOWCREG.ASM -- To show contents of control registers
; ---------------------------------------------------------------------
; May-29-2009, by Tenasaku.
; This program is meant to be run by DOS on i386 (or higher).
; To assemble, say: nasm -f bin -o showcreg.com showcreg.asm
; ---------------------------------------------------------------------
BITS 16
ORG 0x100
;
start:
        mov     ax,cs
        mov     ds,ax
        mov     di,_storeCR0
; ---------------------------------------------------------------------
; Store contents of control registers to memory...
; ---------------------------------------------------------------------
        mov     eax,cr0
        mov     dword [di],eax
        add     di,4
; ---------------------------------------------------------------------
; CR1 is reserved for future extension.  Access is prohibited.
; ---------------------------------------------------------------------
;        mov     eax,cr1
;        mov     dword [di],eax
; ---------------------------------------------------------------------
        add     di,4
        mov     eax,cr2
        mov     dword [di],eax
        add     di,4
        mov     eax,cr3
        mov     dword [di],eax
        add     di,4
        mov     eax,cr4
        mov     dword [di],eax
; report CR0
        mov     ax,'0'
        call    print_prefix
        mov     si,_storeCR0
        call    print_dword
        call    print_CRLF
; ---------------------------------------------------------------------
; CR1 is reserved for future extension.  Access is prohibited.
; ---------------------------------------------------------------------
;        mov     ax,'1'
;        call    print_prefix
;        mov     si,_storeCR1
;        call    print_dword
;        call    print_CRLF
; ---------------------------------------------------------------------
; report CR2
        mov     ax,'2'
        call    print_prefix
        mov     si,_storeCR2
        call    print_dword
        call    print_CRLF
; report CR3
        mov     ax,'3'
        call    print_prefix
        mov     si,_storeCR3
        call    print_dword
        call    print_CRLF
; report CR4
        mov     ax,'4'
        call    print_prefix
        mov     si,_storeCR4
        call    print_dword
        call    print_CRLF
; happy end...
        mov     ax,0x4C00
        int     0x21
; ---------------------------------------------------------------------
; Data Area
; ---------------------------------------------------------------------
_storeCR0       dd      0xFFFFFFFF
_storeCR1       dd      0xFFFFFFFF
_storeCR2       dd      0xFFFFFFFF
_storeCR3       dd      0xFFFFFFFF
_storeCR4       dd      0xFFFFFFFF
_prefix_str1    db      "CR",0x24
_prefix_str2    db      " = 0x",0x24
_hexDigit       db      "0123456789ABCDEF"
; ---------------------------------------------------------------------
; Subroutines
; ---------------------------------------------------------------------
print_dword:
        push    si
        push    cx
        add     si,3
repeat:
        std
        lodsb
        call    printHexByte
        loop    repeat
exit_repeat:
        pop     cx
        pop     si
        ret
;
print_CRLF:
        push    ax
        mov     al,0x0D
        call    putchar
        mov     ax,0x0A
        call    putchar
        pop     ax
        ret
;
print_prefix:
        push    ax
        mov     ah,9
        mov     dx,_prefix_str1
        int     0x21
        pop     ax
        call    putchar
        mov     cx,4
        mov     ah,9
        mov     dx,_prefix_str2
        int     0x21
        ret
;
putchar:
        push    ax
        push    dx
        mov     dl,al
        mov     ah,2
        int     0x21
        pop     dx
        pop     ax
        ret
;
printHexNib:
        push    ax
        push    bx
        push    si
        mov     si,_hexDigit
        and     ax,0x000F
        mov     bx,ax
        mov     al,byte [cs:bx+si]
        call    putchar
        pop     si
        pop     bx
        pop     ax
        ret
;
printHexByte:
        push    bp
        push    ax
        mov     bp,sp
        shr     ax,4
        and     ax,0x0F
        call    printHexNib
        mov     ax,[ss:bp]
        and     ax,0x0F
        call    printHexNib
        pop     ax
        pop     bp
        ret


