The GetCursorPosition function

The GetCursorPosition function retrieves the current position of the caret (cursor) on the text mode screen.

Parameters: The function takes no parameters.

Return value: AH: row number; AL: column number.

Flag state: The function does not modify the flags.

Program source code

An example program for the Intel 8086 assembler demonstrating the use of the GetCursorPosition function to obtain the current cursor position.

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    string db "Hello, World of 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

        mov     ax, 0606h          ; Copy the new cursor position into the ax register
        call    SetCursorPosition  ; Call the SetCursorPosition procedure (ah:06, al:06)

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

        call    GetCursorPosition  ; Call the GetCursorPosition procedure () := AX

        add     ah, 6              ; Add 6 to the row
        add     al, 6              ; Add 6 to the column
        call    SetCursorPosition  ; Call the SetCursorPosition procedure (ah:12, al:12)

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

        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

(The string is printed twice: first at position (6,6), then at position (12,12))

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