Search code examples
assemblyx86-16tasmdosbox

How to print a string centered (vertical and horizontal) in video mode?


I learn to use set video mode and print a string on it.

.model small
.stack 200h
.code

org     100h

mulai:
        mov     ax, @data
        mov     ds, ax

; video mode

        xor     ah, ah
        mov     al, 03h
        int     10h

; print string

        mov     ah, 0eh
        mov     al, "!"
        int     10h

end mulai

Assembling and linking using TASM and TLINK :

tasm foo.asm
tlink foo

Question

How I can print a string centered (vertical and horizontal) ?


Output :

enter image description here

Expected output :

+-------------------------+
|                         |
|                         |
|                         |
|            !            |
|                         |
|                         |
|                         |
|                         |
+-------------------------+

Solution

  • When a text video mode is active

    For outputting a character at a specific location on the screen, you can set the cursor using the BIOS.SetCursorPosition function 02h. You'll have to specify the column number in the DL register, and the row number in the DH register. And because the graphics card supports outputting on up to 8 different pages when you're in video mode 03h, you'll have to specify that page in the BH register.

    The text video mode 03h has 80 columns and 25 rows, therefore the center is at column 40 (28h) and row 12 (0Ch).

    mov dx, 0C28h  ; Row=12 (in DH), Column=40 (in DL)
    mov bh, 0      ; Page=0
    mov ah, 02h    ; BIOS.SetCursorPosition
    int 10h
    

    Hereafter the BIOS.Teletype function will output the character at (40,12) and leave the cursor at (41,12) ready for your next output of a character...

    mov bh, 0      ; Page=0
    mov ax, 0E21h  ; BIOS.Teletype (in AH), Character=33 (in AL)  33 is ASCII of "!"
    int 10h
    

    When a graphics video mode is active

    Here also, for outputting a character at a specific location on the screen, you can set the cursor using the BIOS.SetCursorPosition function 02h. You'll have to specify the column number in the DL register, and the row number in the DH register. And because the graphics card supports outputting on just 1 page when you're in video mode 13h, you'll have to specify 0 in the BH register.

    The graphics video mode 13h has 320 pixels in the horizontal direction and 200 pixels in the vertical direction. A pixel can be shown in any of 256 colors. Although this screen is All Points Addressable (APA), displaying characters is just as easy as in the text video modes. You can use the same output functions.

    The graphics video mode 13h has 40 columns and 25 rows, therefore the center is at column 20 (14h) and row 12 (0Ch).

    mov dx, 0C14h  ; Row=12 (in DH), Column=20 (in DL)
    mov bh, 0      ; Page=0
    mov ah, 02h    ; BIOS.SetCursorPosition
    int 10h
    

    Hereafter the BIOS.Teletype function will output the character at (20,12) and leave the cursor at (21,12) ready for your next output of a character...

    mov bx, 000Fh  ; Page=0 (in BH), Color=15 (in BL)             15 is BrightWhite
    mov ax, 0E21h  ; BIOS.Teletype (in AH), Character=33 (in AL)  33 is ASCII of "!"
    int 10h