The
data generated by the DB, DW, and DD directives is always stored in the code segment, just like the code
generated by other built-in assembly statements. To generate uninitialized or initialized data in the data segment,
you should use Delphi var or const declarations.
Some examples of DB, DW, and DD directives follow.
asm
DB
FFH { One byte }
DB 0,99
{ Two bytes }
DB 'A'
{ Ord('A') }
DB 'Hello world...',0DH,0AH { String followed by CR/LF }
DB 12,'string' { Delphi style string }
DW 0FFFFH { One word }
DW 0,9999
{ Two words }
DW 'A'
{ Same as DB 'A',0 }
DW 'BA'
{ Same as DB 'A','B' }
DW MyVar
{ Offset of MyVar }
DW MyProc
{ Offset of MyProc }
DD 0FFFFFFFFH { One double-word }
DD 0,999999999 { Two double-words }
DD 'A'
{ Same as DB 'A',0,0,0 }
DD 'DCBA'
{ Same as DB 'A','B','C','D' }
DD MyVar
{ Pointer to MyVar }
DD MyProc
{ Pointer to MyProc }
end;
When an identifier precedes a DB, DW , or DD directive, it causes the declaration of a byte-, word-, or double-word-
sized variable at the location of the directive. For example, the assembler allows the following:
ByteVar DB ?
WordVar DW ?
IntVar DD ?
.
.
.
MOV AL,ByteVar
MOV BX,WordVar
MOV ECX,IntVar
The built-in assembler doesn't support such variable declarations. The only kind of symbol that can be defined in an
inline assembly statement is a label. All variables must be declared using Delphi syntax; the preceding construction
can be replaced by
var
229
ByteVar: Byte;
WordVar: Word;
IntVar: Integer;
.
.
.
asm
MOV AL,ByteVar
MOV BX,WordVar
MOV ECX,IntVar
end;
SMALL and LARGE can be used determine the width of a displacement:
MOV EAX, [LARGE $1234]
This instruction generates a 'normal' move with a 32-bit displacement ($00001234).
MOV EAX, [SMALL $1234]
The second instruction will generate a move with an address size override prefix and a 16-bit displacement ($1234).
SMALL can be used to save space. The following example generates an address size override and a 2-byte address
(in total three bytes)
MOV EAX, [SMALL 123]
as opposed to
MOV EAX, [123]
which will generate no address size override and a 4-byte address (in total four bytes).
Two additional directives allow assembly code to access dynamic and virtual methods: VMTOFFSET and
DMTINDEX.
VMTOFFSET retrieves the offset in bytes of the virtual method pointer table entry of the virtual method argument
from the beginning of the virtual method table (VMT). This directive needs a fully specified class name with a method
name as a parameter (for example, TExample.VirtualMethod), or an interface name and an interface method name.
DMTINDEX retrieves the dynamic method table index of the passed dynamic method. This directive also needs a
fully specified class name with a method name as a parameter, for example, TExample.DynamicMethod. To invoke
the dynamic method, call System.@CallDynaInst with the (E)SI register containing
the value obtained from
DMTINDEX.
Note:
Methods with the
message directive are implemented as dynamic methods and can also be called using the
DMTINDEX technique. For example:
TMyClass = class
procedure x; message MYMESSAGE;
end;
The following example uses both DMTINDEX and VMTOFFSET to access dynamic and virtual methods:
230
program Project2;
type
TExample = class
procedure DynamicMethod; dynamic;
procedure VirtualMethod; virtual;
end;
procedure TExample.DynamicMethod;
begin
end;
procedure TExample.VirtualMethod;
begin
end;
procedure CallDynamicMethod(e: TExample);
asm
// Save ESI register
PUSH ESI
// Instance pointer needs to be in EAX
MOV EAX, e
// DMT entry index needs to be in (E)SI
MOV ESI, DMTINDEX TExample.DynamicMethod
// Now call the method
CALL System.@CallDynaInst
// Restore ESI register
POP ESI
end;
procedure CallVirtualMethod(e: TExample);
asm
// Instance pointer needs to be in EAX
MOV EAX, e
// Retrieve VMT table entry
MOV EDX, [EAX]
// Now call the
method at offset VMTOFFSET
CALL DWORD PTR [EDX + VMTOFFSET TExample.VirtualMethod]
end;
var
e: TExample;
begin
e := TExample.Create;
try
CallDynamicMethod(e);
CallVirtualMethod(e);
finally
e.Free;
end;
231