C代码中调用Assembly的函数,Assembly函数对寄存器的使用

Register usageand data alignment for16-bit models

When interfacing to 16-bit memory models,assembly language functions can change the values in AX, BX, CX, DX, or ES.Functions must preserve the values in SI, DI, BP, SP, SS, CS, and DS. The direction flag must always be set to forward.

Data should be aligned along 16-bit boundaries to maximize speed on 16-bit buses.

Register usageand data alignment for32-bit models

When interfacing to 32-bit memory models,assembly language functions can change the values in EAX, ECX, EDX, or ES.Functions must preserve the values in EBX, ESI, EDI, EBP, ESP, SS, FS, CS, and DS. The direction flag must always be set to forward.

Data should be aligned along 32-bit boundaries to maximize speed on 32-bit buses.

Function Return Values for 32-Bit Models

Near pointers, ints, unsigned ints, longs, and unsigned longs are returned in EAX. Chars are returned in AL; shorts are returned in AX. Far pointers are returned in DX, EAX, where DX contains the segment and EAX contains the offset. long longs are returned in EDX, EAX.

When C Linkage is in effect, floats are returned in EAX and doubles in EDX, EAX, where EDX contains the most significant 32 bits and EAX contains the least significant 32 bits. When C++ linkage is in effect, the compiler creates a temporary copy of the variable on the stack and returns a pointer to it. Both these techniques are reentrant.

1-byte structs are returned in AL, 2-byte structs in AX, and 4-byte structs in EAX. With larger structures, the compiler creates a temporary copy of the variable on the stack and returns a (reentrant) pointer to it.

For 32-bit C++ code, where a struct has no constructors or destructors declared for it, 1-byte structs are returned in AL, 2-byte structs in AX, 4-byte structs in EAX, and 8-byte structs in EDX: EAX.

The Easy Way to Call Assembly Code from C++

In many cases in which you want to call an assembly language routine from a C++ function, you can use the following method, which does not require you to worry about function-naming or parameter-passing conventions:

extern "C"{  int assembler_routine(int x);}

原文链接: https://www.cnblogs.com/jack204/archive/2011/09/09/2172729.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/32023

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 上午9:16
下一篇 2023年2月8日 上午9:17

相关推荐