Register usage and data alignment for 16-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 usage and data alignment for 32-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);
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-29
  • 2021-07-29
  • 2021-11-20
  • 2022-12-23
  • 2022-12-23
  • 2022-02-26
猜你喜欢
  • 2021-12-11
  • 2021-06-10
  • 2022-12-23
  • 2021-12-06
  • 2021-11-06
  • 2022-01-05
  • 2022-02-05
相关资源
相似解决方案