Microsoft KB Archive/34377

= How to Implement a Function Pointer in MASM =

Article ID: 34377

Article Last Modified on 11/19/2003

-

APPLIES TO


 * Microsoft Macro Assembler 5.0
 * Microsoft Macro Assembler 5.1 Standard Edition
 * Microsoft Macro Assembler 5.1a
 * Microsoft Macro Assembler 6.0 Standard Edition
 * Microsoft Macro Assembler 6.0a
 * Microsoft Macro Assembler 6.0b

-



This article was previously published under Q34377



SUMMARY
The following sample program illustrates how to implement a function pointer using the Microsoft Macro Assembler. It takes the address of the function (in this example, the address is loaded into ES:DX) and moves it into a 4-byte variable; it then does a far call through the pointer.

In this example, it may seem odd to call a function in this manner because the function is defined locally and its name is known; however, what if the name of the function was not known? What if you were programming a device driver and all that was know was the entry point of the function? Using the following technique, a name could be given to the address of the function and the function could be called like any other function.



MORE INFORMATION
This program illustrates how to implement a function pointer in MASM. This code is similar to the code that the C compiler would generate for a C program that used a pointer to a function.

Sample Code
; Assemble options needed: none

.model small

.data fptr dd 1 dup(?)  ; set aside four bytes for function address

.code FARCALL MACRO func_ptr CALL dword ptr func_ptr ; call by 4-byte far function reference ENDM
 * MACRO Definitions
 * MACRO Definitions

DosExit MACRO MOV ax, 4C00h    ; ah = 4Ch ( dos exit interrupt) al = 0 INT 21h ENDM PUBLIC _ClrScr
 * FUNCTION Definition: function uses int 10h, function 07h to
 * initialize a window
 * initialize a window

_ClrScr PROC FAR

PUSH bp         ; save bp   MOV  bp, sp      ; get sp   PUSH bx          ; save registers PUSH cx  PUSH dx

MOV ax, 0700h   ; ah = 7,  al = 0 MOV bx, 0700h   ; bh = 7,  bl = 0 XOR cx, cx      ; cx = 0 MOV dx, 184Fh   ; dh = 24, dl = 79, decimal INT 10h

MOV ax, 0200h   ; ah = 2, al = 0 XOR bx, bx      ; bx = 0 XOR dx, dx      ; dx = 0 INT 10h

XOR ax, ax      ; function returns void POP dx          ; restore registers POP cx   POP  bx

MOV sp, bp      ; reset sp   POP  bp          ; restore bp   RET              ; return _ClrScr ENDP

BEGIN:             ; main part of the program mov dx, SEG _ClrScr mov es, dx   mov  dx, OFFSET _ClrScr mov WORD PTR fptr, dx    ; low word of fptr is the ; offset of the function mov WORD PTR fptr+2, es  ; high word of fptr is the ; segment of the function FARCALL fptr             ; call function. FARCALL is a macro ; defined above DosExit                  ; Exit to DOS. DosExit is a macro ; defined above
 * Get address of the function, put in es:dx
 * Get address of the function, put in es:dx
 * Load function address into fptr
 * Load function address into fptr
 * Call the function via a function pointer
 * Call the function via a function pointer
 * Exit to DOS
 * Exit to DOS

END BEGIN

Additional query words: kbinf 5.00 5.10 5.10a 6.00 6.00a 6.00b

Keywords: KB34377

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.