Issue
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
Solution
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).
Answered By - Sep Roland
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.