The PrintFormatString function

The PrintFormatString function takes a format string and optional arguments and produces a formatted sequence of characters for output. It supports the %c, %s, %d, %i, %o, %b, %h, %f and %a specifiers, as well as field width control.

Parameters: AX: pointer to the format string. The other arguments are passed through the stack.

Return value: The function does not return a value.

Flag state: The function does not modify the flags.

Program source code (Basic example)

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    fmtstr db "Hello, World of 8086 Assembler!\0"
data ends

code segment public                ; Start of the code segment
    assume  ds:data, cs:code
    include Handyfar.inc           ; Include the file with functions

    main proc                      ; Start of the main procedure
        call    SetDataSegment     ; Call the SetDataSegment procedure

        lea     ax, ds:[fmtstr]    ; Load the address of the variable fmtstr into the ds:ax register
        call    PrintFormatString  ; Call the PrintFormatString procedure

        xor     al, al             ; Clear the al register
        call    ExitProcess        ; Call the ExitProcess procedure
    main endp                      ; End of the main procedure

code ends                          ; End of the code segment
end main	                       ; End of file. The main procedure is the entry point

Hello, World of 8086 Assembler!

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".

Program source code (The %c specifier)

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    fmtstr db "Character: %c\0"
data ends

code segment public                ; Start of the code segment
    assume  ds:data, cs:code
    include Handyfar.inc           ; Include the file with functions

    main proc                      ; Start of the main procedure
        call    SetDataSegment     ; Call the SetDataSegment procedure

        mov     al, 'A'            ; Copy the character 'A' into the al register
        push    ax                 ; 2nd: save the ax register value on the stack
        lea     ax, ds:[fmtstr]    ; 1st: Load the address of the format string fmtstr into the ds:ax register
        call    PrintFormatString  ; Call the PrintFormatString procedure (ax:fmtstr, 'A')
        add     sp, 2              ; Align the stack

        xor     al, al             ; Clear the al register
        call    ExitProcess        ; Call the ExitProcess procedure
    main endp                      ; End of the main procedure

code ends                          ; End of the code segment
end main	                       ; End of file. The main procedure is the entry point

Character: A

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".

Program source code (The %s and %c specifiers)

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    fmtstr db "My name is %s and the first letter of my surname is '%c'.\0"
    mName  db "Evgeny\0"
data ends

code segment public                ; Start of the code segment
    assume  ds:data, cs:code
    include Handyfar.inc           ; Include the file with functions

    main proc                      ; Start of the main procedure
        call    SetDataSegment     ; Call the SetDataSegment procedure

        mov     al, 'A'            ; Copy the character 'A' into the al register
        push    ax                 ; 3rd: Save the ax register value on the stack
        lea     ax, ds:[mName]     ; Load the address of the variable mName into the ds:ax register
        push    ax                 ; 2nd: save the ax register value on the stack
        lea     ax, ds:[fmtstr]    ; 1st: Load the address of the format string fmtstr into the ds:ax register
        call    PrintFormatString  ; Call the PrintFormatString procedure (ax:fmtstr, 'Evgeny', 'A')
        add     sp, 4              ; Align the stack

        xor     al, al             ; Clear the al register
        call    ExitProcess        ; Call the ExitProcess procedure
    main endp                      ; End of the main procedure

code ends                          ; End of the code segment
end main	                       ; End of file. The main procedure is the entry point

My name is Evgeny and the first letter of my surname is 'A'.

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".

Program source code (The %d or %d16 specifiers)

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    fmtstr db "I am studying Assembler %d architecture.\0"
data ends

code segment public                ; Start of the code segment
    assume  ds:data, cs:code
    include Handyfar.inc           ; Include the file with functions

    main proc                      ; Start of the main procedure
        call    SetDataSegment     ; Call the SetDataSegment procedure

        mov     ax, 8086           ; Copy the value 8086 into the ax register
        push    ax                 ; 2nd: save the ax register value on the stack
        lea     ax, ds:[fmtstr]    ; 1st: Load the address of the format string fmtstr into the ds:ax register
        call    PrintFormatString  ; Call the PrintFormatString procedure (ax:fmtstr, 8086)
        add     sp, 2              ; Align the stack

        xor     al, al             ; Clear the al register
        call    ExitProcess        ; Call the ExitProcess procedure
    main endp                      ; End of the main procedure

code ends                          ; End of the code segment
end main	                       ; End of file. The main procedure is the entry point

I am studying Assembler 8086 architecture.

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".

Program source code (The %i16, %i32 and %d32 specifiers)

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    fmtstr db "16-bit decimal number: %i16\n"
           db "32-bit decimal number: %i32\0"
    dec16b dw 28123
    dec32b dd 536870912
data ends

code segment public                     ; Start of the code segment
    assume  ds:data, cs:code
    include Handyfar.inc                ; Include the file with functions

    main proc                           ; Start of the main procedure
        call    SetDataSegment          ; Call the SetDataSegment procedure

        mov     ax, word ptr[dec32b+2]  ; Copy the second half of the number dec32b into the ax register
        push    ax                      ; 4th: Save the ax register value on the stack
        mov     ax, word ptr[dec32b]    ; Copy the first half of the number dec32b into the ax register
        push    ax                      ; 3rd: Save the ax register value on the stack
        push    dec16b                  ; 2nd: Save the value of the variable dec16b on the stack
        lea     ax, ds:[fmtstr]         ; 1st: Load the address of the format string fmtstr into the ds:ax register
        call    PrintFormatString       ; Call the PrintFormatString procedure (ax:fmtstr, ...)
        add     sp, 6                   ; Align the stack

        xor     al, al                  ; Clear the al register
        call    ExitProcess             ; Call the ExitProcess procedure
    main endp                           ; End of the main procedure

code ends                               ; End of the code segment
end main	                            ; End of file. The main procedure is the entry point

16-bit decimal number: 28123
32-bit decimal number: 536870912

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".

Program source code (The %a specifier)

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    fmtout  db "Address of 'string2': %a\0"
    string1 db "Evgeny\0"
    string2 db "Dovgopolov\0"
data ends

code segment public                ; Start of the code segment
    assume  ds:data, cs:code
    include Handyfar.inc           ; Include the file with functions

    main proc                      ; Start of the main procedure
        call    SetDataSegment     ; Call the SetDataSegment procedure

        lea     ax, ds:[string2]   ; Load the address of the string string2 into the ds:ax register
        push    ax                 ; 2nd: save the ax register value on the stack
        lea     ax, ds:[fmtout]    ; 1st: Load the address of the format string fmtstr into the ds:ax register
        call    PrintFormatString  ; Call the PrintFormatString procedure (ax:fmtstr, 'Dovgopolov')
        add     sp, 2              ; Align the stack

        xor     al, al             ; Clear the al register
        call    ExitProcess        ; Call the ExitProcess procedure
    main endp                      ; End of the main procedure

code ends                          ; End of the code segment
end main	                       ; End of file. The main procedure is the entry point

Address of 'string2': 0025

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".

Program source code (The %d, %h, %o, %b and %f specifiers)

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    fmtstr db "DEC: %d\nHEX: %h\nOCT: %o\nBIN: %b\nFPU: %f\n\0"
    double dq 123.45
data ends

code segment public                ; Start of the code segment
    assume  ds:data, cs:code
    include Handyfar.inc           ; Include the file with functions

    main proc                      ; Start of the main procedure
        call    SetDataSegment     ; Call the SetDataSegment procedure

        fld     qword ptr[double]  ; 6th: Load double onto the top of the math coprocessor stack
        mov     ax, 1234           ; Copy the value 1234 into the ax register
        push    ax                 ; 5th: Save the ax register value on the stack
        push    ax                 ; 4th: Save the ax register value on the stack
        push    ax                 ; 3rd: Save the ax register value on the stack
        push    ax                 ; 2nd: save the ax register value on the stack
        lea     ax, ds:[fmtstr]    ; 1st: Load the address of the format string fmtstr into the ds:ax register
        call    PrintFormatString  ; Call the PrintFormatString procedure (ax:fmtstr, ...)
        add     sp, 8              ; Align the stack

        xor     al, al             ; Clear the al register
        call    ExitProcess        ; Call the ExitProcess procedure
    main endp                      ; End of the main procedure

code ends                          ; End of the code segment
end main	                       ; End of file. The main procedure is the entry point

DEC: 1234
HEX: 04D2
OCT: 2322
BIN: 0100 1101 0010
FPU: 123.45

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".

Program source code (The %* and %s specifiers)

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    fmtstr   db "My name is %* %s.\0"
    mName    db "Evgeny\0"
    mSurname db "Dovgopolov\0"
data ends

code segment public                ; Start of the code segment
    assume  ds:data, cs:code
    include Handyfar.inc           ; Include the file with functions

    main proc                      ; Start of the main procedure
        call    SetDataSegment     ; Call the SetDataSegment procedure

        lea     ax, ds:[mName]     ; Load the address of the variable mName into the ds:ax register
        push    ax                 ; 3rd: Save the ax register value on the stack
        lea     ax, ds:[mSurname]  ; Load the address of the variable mSurname into the ds:ax register
        push    ax                 ; 2nd: save the ax register value on the stack
        lea     ax, ds:[fmtstr]    ; 1st: Load the address of the format string fmtstr into the ds:ax register
        call    PrintFormatString  ; Call the PrintFormatString procedure (ax:fmtstr, ...)
        add     sp, 4              ; Align the stack

        xor     al, al             ; Clear the al register
        call    ExitProcess        ; Call the ExitProcess procedure
    main endp                      ; End of the main procedure

code ends                          ; End of the code segment
end main	                       ; End of file. The main procedure is the entry point

My name is Evgeny.

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".