Print String
Print a hardcoded string, from RAM, your program code,
or anywhere else!
This page covers printing out a string whose characters sit
consecutively in RAM somewhere, using a loop in x86 machine code.
We'll go over printing strings that are null-terminated
and strings that have a fixed-length.
- A null-terminated string is a string that has a null
byte, Nul, after it. The loop will stop printing characters
when it sees that byte.
- A fixed-length string is one that you know the size of
'beforehand', so you can hard-code exactly how many times the loop
will run.
Print Loop Basics
Printing characters from RAM repeatedly is going to involve retrieving
a character from RAM, storing it in al, then calling the interrupt
to print, interrupt 0x10. All of this will happen once per loop cycle.
The CPU has an SI register, which stands for "source index" for string operations. With the "data segment" register, DS, it forms the address used by certain string instructions, like LODSB, which stands for "Load String Byte." There are a few other instructions designed for strings (like STOSB, "Store String Byte") too.
LODSB does two things:
- It loads a byte from memory at the location DS:SI, and then
- it automatically increments (or decrements) the SI register, so that when you call it the next time (say in a loop), it gets the next byte.
Addressing (accessing a memory location) in x86 real mode, for
many instructions and operations, is dependent on the values of two
registers, (1) the segment register (e.g. DS)
and (2) the offset register. The actual address being
retrieved is made from taking the segment register, multiplying by
16 (or equivalently, bit shifting to the left by 4), and then adding
the offset:
addr = segment × 16 + offset
For example, if DS=0x7C and SI=0x10 then a call to LODSB would access the byte at address 0x7c10.
Each of these registers can hold 16 bits though, so the effective address is (usually) up to six nibbles long. The left 4 nibbles are determined by the segment register, and the right 4 are (also) determined by the offset register. (This means that the two in the middle are often determined by both registers.)
Whether the LODSB (and other string instructions) increment or decrement their offset register is determined by