打印CR0寄存器的内容

Print the contents of the CR0 register

我正在制作自己的引导加载程序,它切换到保护模式(32 位),然后打印 CR0 寄存器(用于打开保护模式的寄存器)的内容。 我需要在汇编上编写程序。

    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

这将以二进制表示形式显示 CR0。它使用与您的问题相同的输出方法:

    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

同样的事情,但这次是十六进制表示。再次使用与您问题中相同的输出方法:

    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

有关使用查找的解决方案 table 请参阅:


通常您会将这些转换写在一个子例程中,您可以为各种数字重复调用该子例程。然而,由于这是引导加载程序代码,您可能只需要这个显示,因此当前的方法可能是最好的(最小代码大小)。