You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

自定义MBR汇编开发:如何单独修改指定行文本显示颜色?

Custom MBR with Colored Second Line

Got it, let's fix this up so your second line shows in green (or red if you prefer) while the rest stays default. Here's the modified assembly code, followed by a detailed breakdown of how it works so you can apply these concepts to future projects:

Modified Assembly Code

; Compile with: nasm -f bin test.asm -o test.bin
BITS 16
ORG 0x7c00

jmp start

; Print function: prints null-terminated string pointed to by SI, uses current BL for text attribute
print:
    push ax
    cld                     ; Clear direction flag (ensures SI increments forward)
next_char:
    mov al, [si]            ; Load next character from string
    cmp al, 0               ; Check for null terminator (end of string)
    je print_done           ; Exit function if we've finished printing
    call print_char         ; Print the current character
    inc si                  ; Move to next character in the string
    jmp next_char
print_done:
    pop ax
    ret

; Print single character using BIOS video interrupt
print_char:
    mov ah, 0x0e            ; BIOS teletype output function
    mov bh, 0x00            ; Set display page to 0 (default)
    int 0x10                ; Trigger video interrupt to print the character
    ret

start:
    mov ax, cs
    mov ds, ax              ; Set data segment to code segment (real-mode setup)

    ; Print first line with default attribute (black background, light gray foreground)
    mov bl, 0x07            ; Default attribute: 0x07 = 00000111 (BBBBFFFF format)
    mov si, msg1
    call print

    ; Print second line in GREEN (black background, green foreground)
    ; To switch to RED, change bl to 0x04 instead
    mov bl, 0x02            ; Green attribute: 0x02 = 00000010
    mov si, msg2
    call print

    ; Print third line with default attribute again
    mov bl, 0x07
    mov si, msg3
    call print

    jmp $                   ; Infinite loop to hang after printing (prevents boot garbage)

; Split messages into separate strings for per-line color control
msg1 db "You can now insert and install ", 13, 10, 0
msg2 db "your new OS ", 13, 10, 0
msg3 db "right now ", 0

times 510 - ($-$$) db 0    ; Pad binary to 510 bytes (required for MBR)
dw 0xaa55                  ; Mandatory MBR boot signature (marks disk as bootable)

How It Works (Key Assembly & BIOS Concepts)

Let's break down the critical pieces so you understand the "why" behind the code:

1. BIOS Video Interrupt 0x10 (Function 0x0e)

We use the BIOS int 0x10 interrupt with ah=0x0e (Teletype Output) to print characters to the screen. This function relies on these parameters:

  • ah=0x0e: Tells BIOS we want to print a character with automatic line wrapping/scrolling
  • al: The ASCII character we want to print
  • bh: Display page number (almost always 0 for the default boot screen)
  • bl: Text attribute (controls color in text mode, which is the default when booting an MBR)

2. Text Attribute Byte Format

The bl register holds an 8-bit value that defines text color in text mode. It's split into two 4-bit sections: BBBBFFFF

  • BBBB: Background color (0=black, 1=blue, 2=green, 3=cyan, 4=red, 5=magenta, 6=brown, 7=light gray)
  • FFFF: Foreground color (same values as background, plus 8-15 for bright variants)
  • Default value 0x07 = black background + light gray foreground (the standard boot screen color)
  • Green foreground: 0x02 (black background + green text)
  • Red foreground: 0x04 (black background + red text)

3. Code Modifications Explained

  • Split the message: We broke your original single string into three separate null-terminated strings so we can apply different color attributes to each line.
  • Per-line color control: Before printing each string, we set bl to the desired attribute. For the second line, we use 0x02 (green) or 0x04 (red), then reset to 0x07 for the third line to match the default.
  • Safe register handling: We added push ax/pop ax in the print function to preserve the ax register's value, which is a best practice in assembly to avoid overwriting data other parts of the code might need.

Deployment Steps

  1. Compile the code with NASM:

    nasm -f bin test.asm -o test.bin
    
  2. Replace the first sector of your VMware virtual disk:
    Use the dd command (works on Linux/macOS/WSL) to write the .bin file to the first 512 bytes of your VM's disk:

    dd if=test.bin of=your_vm_disk.vmdk bs=512 count=1 conv=notrunc
    

    Note: Replace your_vm_disk.vmdk with the actual path to your VMware virtual disk file.

  3. Boot the VM, and you'll see the second line in green (or red if you changed the bl value) while the rest stays the default gray.

内容的提问来源于stack exchange,提问作者Ultiii_Waste

火山引擎 最新活动