Comprehensive Guide to Number Output in Assembly Language: From DOS Interrupts to Character Conversion

Nov 26, 2025 · Programming · 9 views · 7.8

Keywords: Assembly Language | Number Output | DOS Interrupts

Abstract: This technical paper provides an in-depth exploration of number output implementation in x86 assembly language, focusing on DOS interrupt int 21h usage techniques, detailed character conversion algorithms, and complete code examples demonstrating both decimal and hexadecimal output implementations. The article covers real-mode programming environment, register operation principles, and error handling mechanisms, offering comprehensive solutions for assembly language learners.

Fundamentals of Number Output in Assembly Language

In x86 assembly language programming, number output is a fundamental yet critical operation. Unlike high-level languages, assembly language does not provide built-in output functions, requiring developers to directly invoke system services or write low-level code to implement output functionality.

DOS Interrupt Service Applications

In real-mode environments, DOS provides powerful interrupt services. Among these, int 21h is the most commonly used system call interrupt, with different functions specified by setting the AH register.

Single character output represents the simplest output method:

mov dl, 'A'    ; Set output character
mov ah, 2      ; Set function number
int 21h        ; Call DOS interrupt

Number to Character Conversion Algorithms

For numerical output, the core challenge lies in converting binary values to displayable ASCII characters. This requires designing specialized conversion algorithms.

Hexadecimal conversion is relatively straightforward, as each hexadecimal digit corresponds to 4 binary bits:

; AL contains the value to output
mov bl, al     ; Save original value
shr al, 4      ; Get high 4 bits
call convert_hex_digit
mov dl, al
mov ah, 2
int 21h        ; Output high nibble

mov al, bl     ; Restore original value
and al, 0Fh    ; Get low 4 bits
call convert_hex_digit
mov dl, al
mov ah, 2
int 21h        ; Output low nibble

convert_hex_digit:
    cmp al, 10
    jl .digit
    add al, 'A' - 10 - '0'
.digit:
    add al, '0'
    ret

Decimal Conversion Implementation

Decimal conversion is more complex, requiring successive division and remainder processing:

; Convert AL value to decimal string and output
mov bl, al     ; Save value
mov cx, 0      ; Clear counter

.div_loop:
    mov ah, 0   ; Clear AH
    mov dl, 10
    div dl      ; AX / 10, quotient in AL, remainder in AH
    push ax     ; Save remainder
    inc cx      ; Increment counter
    cmp al, 0   ; Check if quotient is zero
    jne .div_loop

.print_loop:
    pop dx      ; Pop remainder
    mov dl, dh  ; Remainder in DH
    add dl, '0' ; Convert to ASCII
    mov ah, 2
    int 21h     ; Output character
    loop .print_loop

BIOS Interrupt Alternative Solutions

In environments without DOS dependency, BIOS interrupt int 10h can be used for output:

; Using BIOS teletype output
mov al, 'A'    ; Output character
mov ah, 0Eh    ; Function number
mov bh, 0      ; Page number
int 10h        ; Call BIOS interrupt

Complete Example Code

Combining the specific scenario from the Q&A data, the complete numerical calculation and output code is as follows:

org 100h

section .text
start:
    ; Perform calculation
    mov al, 10
    add al, 15   ; AL = 25
    
    ; Output calculation result
    call print_decimal
    
    ; Program termination
    mov ah, 4Ch
    int 21h

print_decimal:
    mov bl, al
    mov cx, 0
    
.convert:
    mov ah, 0
    mov dl, 10
    div dl
    push ax
    inc cx
    cmp al, 0
    jne .convert
    
.output:
    pop dx
    mov dl, dh
    add dl, '0'
    mov ah, 2
    int 21h
    loop .output
    ret

Error Handling and Edge Cases

In practical applications, various edge cases must be considered:

Performance Optimization Recommendations

For performance-sensitive applications, the following optimization strategies can be employed:

By deeply understanding these technical details, developers can efficiently implement various numerical output requirements in assembly language, laying a solid foundation for low-level system programming.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.