Previous: 2.3.1. Asm Statement To the Table of Contents Next: 2.3.3. Code Procedure
2.3.1. Asm Statement Table of Contents 2.3.3. Code Procedure

- 2.3.2. -
Table of Contents
2. TMT Pascal Language Description
2.3. Built-in Assembler
2.3.2. Assembler Procedure


2.3.2. Assembler Procedure


The built-in assembler can also be used to write entire procedures in assembler language. Such procedures should have the assembler keyword appended after a procedure header.
function MultBy9(X: Longint):Longint; assembler;
asm
  MOV  EAX,[X]
  LEA  EAX,[EAX*8+EAX]
end;
The function above used i80386 index scaling feature to implement very fast multiplication by 9.

The assembler procedures differ from the standard Pascal procedures in the following ways:

No Return variable
There is no return variable. You must return the function results in an appropriate register. More precisely, Structured variables
Structured arguments (i.e. strings, objects, records) are not copied into the local variables. They should be treated as var parameters.

Stack Frame
Assembler procedures have no stack frames if they have no arguments and no local symbols. Generally, the stack frame supplied by the built-in assembler is
 PUSH   EBP         // Appears if locals + params >0
 MOV    EBP,ESP     // Appears if locals + params >0
 SUB    ESP, locals // Appears if locals + params >0
 ...
 LEAVE              // Appears if locals + params >0
 RETN   params      // Always appears
Here Locals is the total size of local parameters, Params is the total size of procedure parameters.

Register Preservations
Assembler code should preserve the following registers: DS, CS, SS, ES, EBP, and ESP. All other registers can be destroyed. Notice the inclusion of the ES register. TMT Pascal always assumes that ES is equal to DS.

Furthermore, you should not change segment, page, and interrupt tables, as well as the control, debug and test registers, unless you are thoroughly familiar with 386 protected mode architecture. The privileged instructions like LGDT and LIDT are supported by built-in assembler. However, avoid using them unless you know exactly what you are doing.


Previous: 2.3.1. Asm Statement To the Table of Contents Next: 2.3.3. Code Procedure
2.3.1. Asm Statement Table of Contents 2.3.3. Code Procedure

- 2.3.2. -