简体   繁体   中英

Can you use dereferencing to call a pointer to a function in assembly?

Exactly how it sounds. I load the OFFSET of a procedure into a register, then try to call that register:

MOV EBX, OFFSET MyProc
CALL EBX

At first I would assume that this will call the function, however when you call a procedure you don't type CALL OFFSET MyProc , you simply type CALL MyProc . In C you can call a pointer to a function with the * operator: (*MyProc)(); . Which leads me to wonder if dereferencing the pointer to the function would call the procedure.

CALL [EBX]

However if I dereference it, masm tells me that I need to specify a size, the only possible sizes that I am aware of that I could specify are DWORD PTR , WORD PTR , and BYTE PTR , and I don't think that a procedure is of a particular size .

To sum it up, can you call a pointer to a procedure simply by directly supplying the pointer as an operand to the call instruction, or would you have to dereference the pointer in the call instruction?

Thanks

Why not CALL OFFSET MyProc - because that would be annoying to type every time, and the inconsistent syntax didn't bother MASM creators much (consider the mov eax,var1 vs mov eax,[ebx] , both dereferencing memory).

The call [ebx] would fetch the value stored at ebx address and use that as final target address, so in your case it would try to interpret the first instructions of procedure as target address, and jump who-know-where (probably causing illegal access crash from OS).

The required size in such case is not classic integer size, but jump/call addresses size, like NEAR PTR and FAR PTR , which affects how many bytes from memory will be used ( NEAR PTR in 32b mode is 32b wide vs 16b in real mode (just offset part), FAR PTR is 32b in real mode (16b offset + 16b segment), and 48b in 32b protected mode (32b offset + 16b segment, which works more like selector or something, I never actually needed to fully understand this one, so consult your favourite x86 documentation/book for details).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM