0

I am making my own bootloader which switchs to protected mode(32-bits) and then prints the contents of the CR0 register (the one used to turn on protected mode). I need to write the program on assembly.

    mov esi,hello
    mov ebx,0xb8000
.loop:
    lodsb
    or al,al
    jz halt
    or eax,0x0100
    mov word [ebx], ax
    add ebx,2
    jmp .loop
halt:
    cli
    hlt
hello: db "Hello world!",0
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 4
    You will want to print the contents of `CR0` in hexadecimal or binary. Looking at it in decimal will prove to be meaningless. – Sep Roland Dec 18 '20 at 18:25
  • The code you posted hasn't got anything to do with your question? Except that I guess they both print something... – Nate Eldredge Dec 18 '20 at 18:33
  • 2
    Appears you are asking a different question now. Where as it was displaying the CR0 register in protected mode, are you now asking how to print EAX register in 16-bit real mode? If so you should be asking a new question as the answer no longer matches the original question asked. – Michael Petch Dec 19 '20 at 06:08

1 Answers1

4

This will show CR0 in binary representation. It uses the same output method like in your question:

    mov edx, cr0
    mov ecx, 32          ; 32 bits in a dword
    mov ebx, 000B8000h
.loop:
    mov eax, 00000130h   ; BlueOnBlack "0"
    shl edx, 1           ; Top bit to the carry flag
    adc eax, 0           ; -> AL="0" or AL="1"
    mov [ebx], ax
    add ebx, 2
    dec ecx
    jnz .loop
halt:
    cli
    hlt
    jmp halt

Same thing but this time in hexadecimal representation. Again the same output method like in your question:

    mov edx, cr0
    mov ecx, 8           ; 8 nibbles (groups of 4 bits) in a dword
    mov ebx, 000B8000h
.loop:
    rol edx, 4
    mov eax, edx
    and eax, 15
    add eax, 00000130h
    cmp al, '9'          ; "0" to "9" are fine
    jbe .ok
    add eax, 7           ; This produces "A" to "F"
.ok:
    mov [ebx], ax
    add ebx, 2
    dec ecx
    jnz .loop
halt:
    cli
    hlt
    jmp halt

For a solution that uses a lookup table see:

How to convert a binary integer number to a hex string?


Normaly you would write these conversions in a subroutine that you can call repeatedly for all sorts of numbers. However since this is bootloader code where perhaps you only need this one display, the current approach could be best (smallest codesize).

Sep Roland
  • 33,889
  • 7
  • 43
  • 76