The ScanFormatString function

The ScanFormatString function reads data from the standard input stream and writes the data to the location specified by an argument. Each argument must be a pointer to a variable of the type matching the type specifier in the format.

Parameters: AX: pointer to the format string. The other arguments (pointers to the variables) 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 (Example 1: %c)

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    askUser   db "Enter a character: \0"
    fmtoutput db "\nYou entered: %c\0"
    fmtinput  db "%c"
    char      db ?
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:[askUser]    ; Load the address of the variable askUser into the ds:ax register
        call    WriteString         ; Call the WriteString procedure (ax:askUser)

        lea     ax, ds:[char]       ; Load the address of the variable char into the ds:ax register
        push    ax                  ; 2nd: save the ax register value on the stack
        lea     ax, ds:[fmtinput]   ; 1st: Load the address of the format string fmtinput into the ds:ax register
        call    ScanFormatString    ; Call the ScanFormatString procedure (ax:fmtinput, char)
        add     sp, 2               ; Align the stack

        mov     al, char            ; Copy the value of the variable char into the al register
        push    ax                  ; 2nd: save the ax register value on the stack
        lea     ax, ds:[fmtoutput]  ; 1st: Load the address of the format string fmtoutput into the ds:ax register
        call    PrintFormatString   ; Call the PrintFormatString procedure (ax:fmtoutput, char)
        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

Enter a character: A
You entered: A

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

Program source code (Example 2: %s %s %s)

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    askUser     db "Enter your full name: \0"
    fmtoutput   db "\nYour full name is %s %s %s\0"
    fmtinput    db "%s %s %s"
    first_name  db 20 dup(0)
    middle_name db 20 dup(0)
    last_name   db 20 dup(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:[askUser]      ; Load the address of the variable askUser into the ds:ax register
        call    WriteString           ; Call the WriteString procedure (ax:askUser)

        lea     ax, ds:[middle_name]  ; Load the address of the variable middle_name into the ds:ax register
        push    ax                    ; 4th: Save the ax register value on the stack
        lea     ax, ds:[first_name]   ; Load the address of the variable first_name into the ds:ax register
        push    ax                    ; 3rd: Save the ax register value on the stack
        lea     ax, ds:[last_name]    ; Load the address of the variable last_name into the ds:ax register
        push    ax                    ; 2nd: save the ax register value on the stack
        lea     ax, ds:[fmtinput]     ; 1st: Load the address of the format string fmtinput into the ds:ax register
        call    ScanFormatString      ; Call the ScanFormatString procedure (ax:fmtinput, ...)
        add     sp, 6                 ; Align the stack

        lea     ax, ds:[middle_name]  ; Load the address of the variable middle_name into the ds:ax register
        push    ax                    ; 4th: Save the ax register value on the stack
        lea     ax, ds:[first_name]   ; Load the address of the variable first_name into the ds:ax register
        push    ax                    ; 3rd: Save the ax register value on the stack
        lea     ax, ds:[last_name]    ; Load the address of the variable last_name into the ds:ax register
        push    ax                    ; 2nd: save the ax register value on the stack
        lea     ax, ds:[fmtoutput]    ; 1st: Load the address of the format string fmtoutput into the ds:ax register
        call    PrintFormatString     ; Call the PrintFormatString procedure (ax:fmtoutput, ...)
        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

Enter your full name: Dovgopolov Evgeny Sergeevich
Your full name is Dovgopolov Evgeny Sergeevich

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

Program source code (Example 3: (&))

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    askUser   db "Enter your full name: \0"
    fmtoutput db "\nYour full name is %s\0"
    fmtinput  db "%s"
    full_name db 50 dup(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:[askUser]    ; Load the address of the variable askUser into the ds:ax register
        call    WriteString         ; Call the WriteString procedure (ax:askUser)

        lea     ax, ds:[full_name]  ; Load the address of the variable full_name into the ds:ax register
        push    ax                  ; 2nd: save the ax register value on the stack
        lea     ax, ds:[fmtinput]   ; 1st: Load the address of the format string fmtinput into the ds:ax register
        call    ScanFormatString    ; Call the ScanFormatString procedure (ax:fmtinput, full_name)
        add     sp, 2               ; Align the stack

        lea     ax, ds:[full_name]  ; Load the address of the variable full_name into the ds:ax register
        push    ax                  ; 2nd: save the ax register value on the stack
        lea     ax, ds:[fmtoutput]  ; 1st: Load the address of the format string fmtoutput into the ds:ax register
        call    PrintFormatString   ; Call the PrintFormatString procedure (ax:fmtoutput, full_name)
        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

Enter your full name: Dovgopolov&Evgeny&Sergeevich
Your full name is Dovgopolov Evgeny Sergeevich

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

Program source code (Example 4: %s, %c, %. and %)

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    askUser   db "Enter your name, sex and age: \0"
    fmtoutput db "NAME: %.4s\nSEX: %.4c\nAGE: %d\0"
    fmtinput  db "%s %c %d"
    user_name db 20 dup(0)
    user_sex  db ?
    user_age  dw ?
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:[askUser]    ; Load the address of the variable askUser into the ax register
        call    WriteString         ; Call the WriteString procedure (ax:askUser)

        lea     ax, ds:[user_age]   ; Load the address of the variable user_age into the ds:ax register
        push    ax                  ; 4th: Save the ax register value on the stack
        lea     ax, ds:[user_sex]   ; Load the address of the variable user_sex into the ds:ax register
        push    ax                  ; 3rd: Save the ax register value on the stack
        lea     ax, ds:[user_name]  ; Load the address of the variable user_name into the ds:ax register
        push    ax                  ; 2nd: save the ax register value on the stack
        lea     ax, ds:[fmtinput]   ; 1st: Load the address of the format string fmtinput into the ds:ax register
        call    ScanFormatString    ; Call the ScanFormatString procedure (ax:fmtinput, ...)
        add     sp, 6               ; Align the stack

        call    ClearScreen         ; Call the ClearScreen procedure to clear the screen

        push    user_age            ; 4th: Save the value of the variable user_age on the stack
        mov     al, user_sex        ; Copy the value of the variable user_sex into the al register
        call    ToUpperChar         ; Call the ToUpperChar procedure (al:user_sex)
        push    ax                  ; 3rd: Save the ax register value on the stack
        lea     ax, ds:[user_name]  ; Load the address of the variable user_name into the ds:ax register
        call    ToUpperString       ; Call the ToUpperString procedure (ax:user_name)
        push    ax                  ; 2nd: save the ax register value on the stack
        lea     ax, ds:[fmtoutput]  ; 1st: Load the address of the format string fmtoutput into the ds:ax register
        call    PrintFormatString   ; Call the PrintFormatString procedure (ax:fmtoutput, ...)
        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

Enter your name, sex and age: Evgeny m 36

NAME:     EVGENY
SEX:     M
AGE: 36

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

Program source code (Example 5: input according to the format)

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    askUser   db "Enter your name: \0"
    fmtoutput db "\nYour name is %s\0"
    fmtinput  db "My name is %s\0"
    user_name db 20 dup(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:[askUser]    ; Load the address of the variable askUser into the ds:ax register
        call    WriteString         ; Call the WriteString procedure (ax:askUser)

        lea     ax, ds:[user_name]  ; Load the address of the variable user_name into the ds:ax register
        push    ax                  ; 2nd: save the ax register value on the stack
        lea     ax, ds:[fmtinput]   ; 1st: Load the address of the format string fmtinput into the ds:ax register
        call    ScanFormatString    ; Call the ScanFormatString procedure
        add     sp, 2               ; Align the stack

        lea     ax, ds:[user_name]  ; Load the address of the variable user_name into the ds:ax register
        push    ax                  ; 2nd: save the ax register value on the stack
        lea     ax, ds:[fmtoutput]  ; 1st: Load the address of the format string fmtoutput into the ds:ax register
        call    PrintFormatString   ; Call the PrintFormatString procedure
        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

Enter your name: My name is Evgeny
Your name is Evgeny

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

Program source code (Example 6: working with a structure)

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

PERSON struc
    m_name    db 20 dup(0)
    m_surname db 20 dup(0)
    m_age     dw ?
PERSON ends

data segment public
    EVGENII PERSON <>
    SERGEII PERSON <>
data ends

data segment public
    u_name    db "Enter your name: \0"
    u_surname db "\nEnter your surname: \0"
    u_age     db "\nEnter your age: \0"
    fmtstr    db "\n\nName: %.4s\nSurname: %s\nAge: %d\n\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     si, ds:[EVGENII]      ; Load the address of EVGENII into ds:si

        lea     ax, ds:[u_name]       ; Load the address of the variable u_name into the ds:ax register
        call    WriteString           ; Call the WriteString procedure (ax:u_name)
        call    SetPersonName         ; Call SetPersonName(si)

        lea     ax, ds:[u_surname]    ; Load the address of the variable u_surname into the ds:ax register
        call    WriteString           ; Call the WriteString procedure (ax:u_surname)
        call    SetPersonSurname      ; Call SetPersonSurname(si)

        lea     ax, ds:[u_age]        ; Load the address of the variable u_age into the ds:ax register
        call    WriteString           ; Call the WriteString procedure (ax:u_age)
        call    SetPersonAge          ; Call SetPersonAge(si)

        call    GetPersonAge          ; Call GetPersonAge() := AX
        push    ax                    ; 4th: Save the ax register value on the stack
        call    GetPersonSurname      ; Call GetPersonSurname() := AX
        push    ax                    ; 3rd: Save the ax register value on the stack
        call    GetPersonName         ; Call GetPersonName() := AX
        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, 6                 ; Align the stack

        call    PrintNewLine

        lea     si, ds:[SERGEII]      ; Load the address of SERGEII into ds:si

        lea     ax, ds:[u_name]       ; Load the address of the variable u_name into the ds:ax register
        call    WriteString           ; Call the WriteString procedure (ax:u_name)
        call    SetPersonName         ; Call SetPersonName(si)

        lea     ax, ds:[u_surname]    ; Load the address of the variable u_surname into the ds:ax register
        call    WriteString           ; Call the WriteString procedure (ax:u_surname)
        call    SetPersonSurname      ; Call SetPersonSurname(si)

        lea     ax, ds:[u_age]        ; Load the address of the variable u_age into the ds:ax register
        call    WriteString           ; Call the WriteString procedure (ax:u_age)
        call    SetPersonAge          ; Call SetPersonAge(si)

        call    GetPersonAge          ; Call GetPersonAge() := AX
        push    ax                    ; 4th: Save the ax register value on the stack
        call    GetPersonSurname      ; Call GetPersonSurname() := AX
        push    ax                    ; 3rd: Save the ax register value on the stack
        call    GetPersonName         ; Call GetPersonName() := AX
        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
        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

    SetPersonName proc                ; Start of the SetPersonName procedure
        push    ax
        push    si

    data segment
        set_name db "%s", 0
    data ends

        lea     ax, [si].m_name       ; Load the address of the variable m_name into the ds:ax register
        push    ax                    ; 2nd: save the ax register value on the stack
        lea     ax, ds:[set_name]     ; 1st: Load the address of the format string set_name into the ds:ax register
        call    ScanFormatString      ; Call the ScanFormatString procedure (ax:set_name, ...)
        add     sp, 2                 ; Align the stack

        pop     si
        pop     ax
        ret                           ; Return from the procedure
    SetPersonName endp                ; End of the SetPersonName procedure

    SetPersonSurname proc             ; Start of the SetPersonSurname procedure
        push    ax
        push    si

    data segment
        set_surname db "%s", 0
    data ends

        lea     ax, [si].m_surname    ; Load the address of the variable m_surname into the ds:ax register
        push    ax                    ; 2nd: save the ax register value on the stack
        lea     ax, ds:[set_surname]  ; 1st: Load the address of the format string set_surname into ds:ax
        call    ScanFormatString      ; Call the ScanFormatString procedure (ax:set_surname, ...)
        add     sp, 2                 ; Align the stack

        pop     si
        pop     ax
        ret                           ; Return from the procedure
    SetPersonSurname endp             ; End of the SetPersonSurname procedure

    SetPersonAge proc                 ; Start of the SetPersonAge procedure
        push    ax
        push    si

    data segment
        set_age db "%d", 0
    data ends

        lea     ax, [si].m_age        ; Load the address of the variable m_age into the ds:ax register
        push    ax                    ; 2nd: save the ax register value on the stack
        lea     ax, ds:[set_age]      ; 1st: Load the address of the format string set_age into the ds:ax register
        call    ScanFormatString      ; Call the ScanFormatString procedure (ax:set_age, ...)
        add     sp, 2                 ; Align the stack

        pop     si
        pop     ax
        ret                           ; Return from the procedure
    SetPersonAge endp                 ; End of the SetPersonAge procedure

    GetPersonName proc                ; Start of the GetPersonName procedure
        lea     ax, [si].m_name       ; Load the address of [si].m_name into the ax register
        ret                           ; Return from the procedure
    GetPersonName endp                ; End of the GetPersonName procedure

    GetPersonSurname proc             ; Start of the GetPersonSurname procedure
        lea     ax, [si].m_surname    ; Load the address of [si].m_surname into the ax register
        ret                           ; Return from the procedure
    GetPersonSurname endp             ; End of the GetPersonSurname procedure

    GetPersonAge proc                 ; Start of the GetPersonAge procedure
        mov     ax, [si].m_age        ; Load the address of [si].m_age into the ax register
        ret                           ; Return from the procedure
    GetPersonAge endp                 ; End of the GetPersonAge procedure

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

Enter your name: Evgeny
Enter your surname: Dovgopolov
Enter your age: 37

Name:     Evgeny
Surname: Dovgopolov
Age: 37

Enter your name: Sergey
Enter your surname: Ivanov
Enter your age: 59

Name:     Sergey
Surname: Ivanov
Age: 59

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