How to center text in TASM?
Categories:
Centering Text in TASM: A Guide for x86 DOS Assembly

Learn how to precisely center text on the screen in TASM (Turbo Assembler) for x86 DOS environments, covering basic principles and practical code examples.
Centering text in a console application might seem trivial in high-level languages, but in x86 DOS assembly using TASM, it requires a direct understanding of screen memory, cursor positioning, and string manipulation. This guide will walk you through the necessary steps and provide a complete code example to achieve perfectly centered text on your DOS screen.
Understanding the DOS Screen and Cursor
In DOS, the screen is typically a text-mode display, often 80 columns wide and 25 rows high. Each character position has a corresponding memory location. To center text, we need to know the screen's width and the length of the string we want to display. The cursor's position is controlled using BIOS interrupts or direct video memory access. For simplicity and compatibility, we'll primarily use BIOS interrupt INT 10h
for cursor control.
flowchart TD A[Start] B{Get Screen Width} C{Get String Length} D[Calculate Start Column] E[Set Cursor Position] F[Display String] G[End] A --> B B --> C C --> D D --> E E --> F F --> G
Flowchart for Centering Text Logic
Calculating the Starting Column
The core of centering text lies in calculating the correct starting column. If the screen is SCREEN_WIDTH
characters wide and your string is STRING_LENGTH
characters long, the number of empty spaces on either side of the string will be (SCREEN_WIDTH - STRING_LENGTH) / 2
. This value gives us the starting column for our centered text. Remember that screen columns are usually 0-indexed.
; Assume SCREEN_WIDTH = 80, STRING_LENGTH = 15
; Calculation: (80 - 15) / 2 = 65 / 2 = 32 (integer division)
; Starting column = 32
MOV AX, SCREEN_WIDTH
SUB AX, STRING_LENGTH
MOV CX, 2
DIV CX ; AX now holds the starting column
Assembly snippet for calculating the starting column.
Full TASM Example for Centering Text
This example demonstrates a complete TASM program that defines a string, calculates its length, determines the starting column, sets the cursor, and displays the string centered on the screen. It uses INT 10h
for cursor positioning and INT 21h
for string output.
.MODEL SMALL
.STACK 100h
.DATA
myString DB 'Hello, TASM World!', 0
stringLength DW ?
screenWidth DW 80 ; Standard 80-column screen
screenHeight DW 25 ; Standard 25-row screen
targetRow DB 12 ; Row to center the text on (0-indexed)
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
; Calculate string length
MOV SI, OFFSET myString
MOV CX, 0
countLoop:
CMP BYTE PTR [SI], 0
JE endCount
INC CX
INC SI
JMP countLoop
endCount:
MOV stringLength, CX
; Calculate starting column
MOV AX, screenWidth
SUB AX, stringLength
MOV BX, 2
DIV BX ; AX now contains the starting column
MOV DH, targetRow ; Row (0-indexed)
MOV DL, AL ; Column (0-indexed)
MOV BH, 0 ; Page number (usually 0)
; Set cursor position (INT 10h, AH=02h)
MOV AH, 02h
INT 10h
; Display the string (INT 21h, AH=09h)
MOV DX, OFFSET myString
MOV AH, 09h
INT 21h
; Wait for a key press before exiting
MOV AH, 01h
INT 21h
; Exit to DOS
MOV AH, 4Ch
INT 21h
MAIN ENDP
END MAIN
Complete TASM program to center a string on the screen.
TASM yourfile.asm
and TLINK yourfile.obj
. Then run yourfile.exe
from your DOS environment or emulator.1. Define Your String
Declare your string in the .DATA
segment, ensuring it's null-terminated (e.g., DB 'My Text', 0
).
2. Calculate String Length
Write a loop to iterate through your string until the null terminator is found, incrementing a counter to get the length. Store this length in a variable.
3. Determine Screen Dimensions
For standard text modes, assume 80 columns and 25 rows. If you need dynamic detection, you can use INT 10h, AH=0Fh
to get video mode information.
4. Calculate Starting Column
Use the formula (screenWidth - stringLength) / 2
to find the 0-indexed starting column. Store the result in DL
for cursor positioning.
5. Set Cursor Position
Load the desired row into DH
and the calculated column into DL
. Set BH
to 0 (for video page 0). Call INT 10h
with AH=02h
.
6. Display the String
Load the offset of your string into DX
. Call INT 21h
with AH=09h
to print the null-terminated string.