Skip to content
systemcoding.dev
Assembly

Print Strings in Real Mode With BIOS int 0x10

Print strings and hex values in 16-bit real mode with BIOS int 0x10, using teletype function 0x0E and write-string function 0x13, in reusable NASM routines.

7 min read
Summarize in AI
Flat illustration of a boot chain of stages, each handing over to the next, with the Linux, Intel and Arm logos.

The first thing a bootloader needs is a way to say something. A debugger is not available yet, nothing else is running and the screen is the only output. The BIOS provides it through software interrupt 0x10, the video services, which a real-mode program calls by loading a function number into ah and executing int 0x10. This post builds a small set of reusable NASM 2.16 routines on top of it: print a zero-terminated string, print a byte and a word in hexadecimal, set the cursor, clear the screen and write a string to a given row and column with one call. The routines are the ones a boot sector uses for its own diagnostics, and each fits in a few dozen bytes, which matters when the whole sector is 512.

The functions you will use

The video interrupt has many sub-functions, each selected by ah. The ones that matter for text:

ahFunctionInputs
0x00Set video modeal = mode, 0x03 for 80x25 text
0x02Set cursor positionbh = page, dh = row, dl = column
0x03Read cursor positionbh = page; returns dh, dl
0x06Scroll window up, or clearal = lines (0 clears), bh = attribute, ch,cl,dh,dl = corners
0x09Write character and attribute at cursoral = char, bh = page, bl = attribute, cx = count
0x0ETeletype outputal = char, bh = page, bl = color (graphics modes)
0x13Write stringal = mode, bh = page, bl = attribute, cx = length, dh,dl = position, es:bp = string

The reference for these is the BIOS interrupt documentation in the PC BIOS listings, and Ralf Brown's Interrupt List collects the register conventions. Function 0x0E is the one to start with because it handles the cursor, carriage return, line feed and backspace for you, and scrolls the screen when the cursor passes the bottom.

A string printer

print.asmnasm
; print_string: print the zero-terminated string at ds:si
; clobbers: ax, bx, si
print_string:
    mov  ah, 0x0E
    xor  bh, bh
.next:
    lodsb
    test al, al
    jz   .done
    int  0x10
    jmp  .next
.done:
    ret

Set ah and bh once outside the loop, because the BIOS preserves them on return in nearly every implementation, though the interface does not formally guarantee it. If you meet a BIOS that clobbers them, move the two mov instructions inside the loop. The routine takes the address in si and uses the default segment ds, so ds must be set up first, as in the previous post. Call it like this:

nasm
    mov  si, msg
    call print_string
    ...
msg: db "Booting...", 13, 10, 0

Printing hexadecimal

Debug output is mostly numbers. A 16-bit value is four hex digits, each from a nibble. The routine below rotates the value left four bits at a time, masks the low nibble and looks it up in a table:

print.asmnasm
; print_hex: print ax as 4 hex digits
; clobbers: ax, bx, cx, dx
print_hex:
    mov  cx, 4
    mov  dx, ax
.digit:
    rol  dx, 4
    mov  bx, dx
    and  bx, 0x000F
    mov  al, [hexdigits + bx]
    mov  ah, 0x0E
    int  0x10
    loop .digit
    ret

hexdigits: db "0123456789ABCDEF"

rol dx, 4 brings the top nibble to the bottom, so the first iteration prints the most significant digit. Copying dx to bx and masking leaves an index from 0 to 15, and [hexdigits + bx] is a legal 16-bit addressing form since bx can serve as a base register. The mask also leaves bh zero, which is the display page the teletype call expects, so no separate clearing is needed. loop decrements cx and jumps while it is not zero. Printing the value 0xBEEF produces:

Text
BEEF

A byte version loads the value into al, shifts, and prints two digits; a common trick is to make print_hex the only routine and call it with the byte in ax, accepting the leading 00.

Clearing the screen and moving the cursor

The BIOS has two ways to clear the screen. Setting the video mode again clears it as a side effect:

nasm
    mov  ax, 0x0003      ; ah = 0 set mode, al = 3 for 80x25 color text
    int  0x10

It also resets the cursor to the top left. The alternative is the scroll function, which can clear a rectangle with a chosen color attribute:

nasm
    mov  ax, 0x0600      ; ah = 6 scroll up, al = 0 means clear the window
    mov  bh, 0x1F        ; attribute: white on blue
    xor  cx, cx          ; top-left corner (row 0, col 0)
    mov  dx, 0x184F      ; bottom-right corner (row 24, col 79)
    int  0x10

The attribute byte has the background color in the high nibble and the foreground in the low nibble, so 0x1F is white (F) on blue (1). Then place the cursor with function 0x02:

nasm
    mov  ah, 0x02
    xor  bh, bh
    mov  dh, 5           ; row
    mov  dl, 10          ; column
    int  0x10

Printing afterward starts at row 5, column 10. Rows and columns count from zero.

Write a string at a position in one call

Function 0x13 writes a whole string of known length at a chosen position with an attribute, which saves the loop. It reads the string from es:bp, not ds:si:

nasm
    mov  ax, 0x1301      ; ah = 0x13, al = 1: update cursor, string has only characters
    mov  bh, 0           ; page
    mov  bl, 0x0A        ; attribute: light green on black
    mov  cx, msg_len     ; length in bytes
    mov  dh, 2           ; row
    mov  dl, 4           ; column
    mov  bp, msg         ; es:bp points at the string
    int  0x10
...
msg:     db "Stage 1 ready"
msg_len: equ $ - msg

The equ $ - msg idiom lets the assembler compute the length. Mode al = 1 moves the cursor to the end of the string, and mode 0 leaves it. Remember that es must equal the segment of the string, and a bootloader that zeroed ds should zero es also. Two BIOS quirks to know: some very old BIOSes did not implement function 0x13, and it is a source of failures in unusual emulators, so the teletype loop is the more portable choice. And on some BIOSes the call destroys bp, so reload it if you call twice.

Writing to video memory directly

The alternative to the BIOS is to write characters to the text buffer yourself, at segment 0xB800. It is faster and works in protected mode, which has no BIOS. Each cell is two bytes, so row r and column c are at offset (r * 80 + c) * 2:

nasm
    mov  ax, 0xB800
    mov  es, ax
    mov  byte [es:0], 'A'
    mov  byte [es:1], 0x07

The first byte is the character and the second the attribute. Direct writes are what you move to when the code switches to 32-bit mode and the BIOS is gone, and the next stage's print routine works this way.

Common mistakes

Calling int 0x10 with ds not set means lodsb reads from the wrong segment and prints junk. Forgetting the terminating zero makes the loop print until it meets one in the following bytes. Using 0x0E after entering protected mode crashes or hangs, because the interrupt vector table at address 0 belongs to real mode. Mixing up ds:si for the teletype loop and es:bp for function 0x13 is the classic one. And printing a character with bl holding a color matters only in graphics modes; in text mode the attribute comes from the cell, and function 0x0E writes with the existing attribute.

Putting the routines together

A boot sector that uses them has a small, predictable shape: set up the segments and stack, clear the screen, print a banner with print_string, print a status value with print_hex and then continue to the real work. A useful habit is to print the boot drive number the BIOS passed in dl, saved at the very start before anything else can overwrite it. Store it in a byte in the sector (boot_drive: db 0), load it with mov [boot_drive], dl as the first instruction after the segment setup, and print it with mov al, [boot_drive], xor ah, ah, call print_hex. On an emulator booting from the first hard disk it is 0x80, and from a floppy it is 0x00. That value is what a disk-reading routine needs in dl, so printing it early catches the common bug of reading from the wrong drive.

The size cost is worth counting. The string printer is about a dozen bytes, the hex printer about thirty and the digit table sixteen. Together they take under sixty of the 510 bytes available, so diagnostics are cheap enough to leave in until the sector is nearly full. When space runs out, the first thing to remove is the banner string, and the last is the error message, because the error message is the one that tells you what went wrong on a machine you cannot debug.

Also inKernels
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.