Every
built-in assembler expression has a type or, more correctly, a size, because
the assembler regards the type of an expression simply as the size of its
memory location. For example, the type of an Integer variable is four,
because it occupies 4 bytes. The built-in assembler performs type checking
whenever possible, so in the instructions
var
QuitFlag: Boolean;
OutBufPtr: Word;
...
asm
MOV AL,QuitFlag
MOV BX,OutBufPtr
end;
the assembler checks that the size of QuitFlag
is one (a byte), and that the size of OutBufPtr is two (a word). The
instruction
MOV DL,OutBufPtr
produces an error because DL is a
byte-sized register and OutBufPtr is a word. The type of a memory
reference can be changed through a typecast; these are correct ways of writing
the previous instruction:
MOV DL,BYTE PTR OutBufPtr
MOV DL,Byte(OutBufPtr)
MOV DL,OutBufPtr.Byte
These MOV
instructions all refer to the first (least significant) byte of the OutBufPtr
variable.
In some
cases, a memory reference is untyped. One example is an immediate value
enclosed in square brackets:
MOV
al, [Buffer]
MOV
cx, [Buffer]
MOV
edx, [Buffer]
The
built-in assembler permits both of these instructions, because the expression
[100H] has no type -- it just means “the contents of address 100H in the data
segment,” and the type can be determined from the first operand (byte for AL,
word for BX). In cases where the type can’t be determined from another operand,
the built-in assembler requires an explicit typecast:
INC BYTE PTR [ECX]
IMUL WORD PTR [EDX]
The
following table summarizes the predefined type symbols that the built-in
assembler provides in addition to any currently declared Object Pascal types.
|
Symbol |
Type |
|
BYTE |
1 |
|
WORD |
2 |
|
DWORD |
4 |
|
QWORD |
8 |
|
TBYTE |
10 |
每个内建汇编表达式一个类型,更为正确的说法是,一个尺寸,因为汇编编译器只是简单地通过表达式的尺寸来判断其类型。例如,Integer变量的类型是4,因为它占用4个字节。内建汇编程序在任何可能的时候都执行类型检查,因此在下列指令中
var
QuitFlag: Boolean;
OutBufPtr: Word;
...
asm
MOV AL,QuitFlag
MOV BX,OutBufPtr
end;
汇编程序检查出QuitFlag的尺寸是1(1个字节),OutBufPtr的尺寸是2(2个字节)。如下指令
MOV DL,OutBufPtr
将产生一个错误,因为DL是一个单字节尺寸的寄存器而OutBufPtr是一个单字。内存引用的类型可以通过类型转换被改变,因此上面指令的正确写法应为:
MOV DL,BYTE PTR OutBufPtr
MOV DL,Byte(OutBufPtr)
MOV DL,OutBufPtr.Byte
这些MOV指令都引用了OutBufPtr变量的第一个字节(最低的有效部分)。
在某些情况下,内存引用是无类型的。例如封装在方括号中的立即值:
MOV
al, [Buffer]
MOV
cx, [Buffer]
MOV
edx, [Buffer]
这些指令都可以在内建汇编程序中执行,因为表达式 [100H] 没有类型 – 意思是“数据段中地址为 100H 内容,”并且其类型可以由第一个操作数(如果是AL,则类型为字节;如果是BX,则类型为单字)来决定。这时,不能从另一个操作数来确定类型,内建汇编程序需要一个清除的类型转换:
INC BYTE PTR [ECX]
IMUL WORD PTR [EDX]
下列表摘要列出了内建汇编程序中预定义类型符号以及在当前Object Pascal中额外定义的符号:
|
符号 |
类型 |
|
BYTE |
1 |
|
WORD |
2 |
|
DWORD |
4 |
|
QWORD |
8 |
|
TBYTE |
10 |