The Challenge of Text Output

Displaying a single ASCII character (A′, 5′, `!′) requires the OS to perform several complex steps:

  1. Cursor Management: Find the current screen position (X, Y) where the character should appear.
  2. Font Lookup: Find the graphic pattern (the 8x8 pixel data) for that character.
  3. Drawing: Copy the font pattern onto the screen memory (Part 23).
  4. Advance Cursor: Move the cursor position to the next character cell (X+1).

The PutChar Routine (System Call)

Every OS provides a core routine, often called `PutChar′, which is the system call used by programs to print a single character.

PutChar Logic:

PUT_CHAR:
    PUSH AF, HL, DE, BC   ; Save all registers (context)
    
    ; Assume the character to print is in register C.
    
    ; 1. Calculate Font Address: C is used as an index into the font data
    LD   HL, FONT_DATA_BASE ; HL ← Address where the font data starts
    LD   A, C
    SUB  32                 ; Subtract 32 (ASCII space offset)
    LD   C, A               ; C ← Font index (0 for space, 1 for '!', etc.)
    
    LD   A, 8               ; Font data is 8 bytes high
    CALL MULTIPLY_BY_A       ; Routine to calculate index × 8
    
    ADD  HL, DE             ; HL ← FONT_DATA_BASE + (Index × 8)
    
    ; 2. Calculate Screen Address: Get the address of the current cursor position
    CALL CURSOR_TO_SCREEN_ADDR ; DE ← Screen address for drawing
    
    ; 3. Draw Character: Copy the 8 bytes of font data (HL) to the screen (DE)
    LD   BC, 8              ; BC ← 8 bytes to copy
    LDIR                    ; High-speed copy
    
    ; 4. Advance Cursor: Update the OS's internal cursor position (X ← X + 1)
    CALL ADVANCE_CURSOR
    
    POP  BC, DE, HL, AF
    RET                     ; Return from the system call

String Output (PutString)

String output routines simply call **PutChar′** repeatedly in a loop until they encounter a **Null Terminator** (00H′).

String Output Logic:

PUT_STRING:
    ; Assume HL holds the address of the ASCII string
    
STRING_LOOP:
    LD   A, (HL)            ; A ← Current character
    INC  HL                 ; Advance string pointer
    OR   A                  ; Set flags based on A (tests if A is 00H)
    JP   Z, STRING_END      ; If Z=1, end of string (Null Terminator)
    
    LD   C, A               ; C ← Character to print
    CALL PUT_CHAR           ; Print the character
    JP   STRING_LOOP        ; Continue loop
    
STRING_END:
    RET

Scroll and Wrap

The console output system must also handle:

  • Line Feed (10): Move the cursor down one row.
  • Carriage Return (13): Reset the cursor X-position to 0.
  • Screen Wrap/Scroll: If the cursor moves past the last row, the entire screen must scroll up one line (Part 27).