Originale-mail to me for new edition

 

Differences between Object Pascal and assembler expressions

 

The most important difference between Object Pascal expressions and built-in assembler expressions is that assembler expressions must resolve to a constant value -- a value that can be computed at compile time. For example, given the declarations

const

  X = 10;

  Y = 20;

var

  Z: Integer;

the following is a valid statement.

asm

  MOV     Z,X+Y

end;

Because both X and Y are constants, the expression X + Y is a convenient way of writing the constant 30, and the resulting instruction simply moves of the value 30 into the variable Z. But if X and Y are variables

var

  X, Y: Integer;

-- the built-in assembler cannot compute the value of X + Y at compile time. In this case, to move the sum of X and Y into Z you would use

asm

  MOV     EAX,X

  ADD     EAX,Y

  MOV     Z,EAX

end;

In an Object Pascal expression, a variable reference denotes the contents of the variable. But in an assembler expression, a variable reference denotes the address of the variable. In Object Pascal the expression X + 4 (where X is a variable) means the contents of X plus 4, while to the built-in assembler it means the contents of the word at the address four bytes higher than the address of X. So, even though you’re allowed to write

asm

  MOV     EAX,X+4

end;

this code doesn’t load the value of X plus 4 into AX; instead, it loads the value of a word stored four bytes beyond X. The correct way to add 4 to the contents of X is

asm

  MOV     EAX,X

  ADD     EAX,4

end;

 

Topic groups

 

See also

About expressions

Expressions: Overview

 

 

译文

 

Object Pascal和汇编表达式间的不同

 

Object Pascal表达式和内建汇编表达式之间最重要的不同之处在于,汇编表达式必须分解为一个常量值,该值在编译时可以被计算。例如,给出如下声明

const

  X = 10;

  Y = 20;

var

  Z: Integer;

下面的语句是合法的:

asm

  MOV     Z,X+Y

end;

因为XY都是常量,表达式X + Y是写常量30的便利方法,因此指令简单地把值30传送到变量Z中。而如果XY是变量,

var

  X, Y: Integer;

-- 那么内建汇编程序不能计算X + Y的值。这时,传送XY的和到Z中就需要使用如下代码:

asm

  MOV     EAX,X

  ADD     EAX,Y

  MOV     Z,EAX

end;

Object Pascal表达式中,变量引用表示变量的内容(contents)。而在汇编表达式中,变量引用表示变量的地址。在Object Pascal中,表达式X + 4(这里的X是一个变量)表示X4的内容,而对于内建汇编程序中,它表示一个单字的内容,该单字位于比X的地址高4个字节的地址处。因此,即使可以正确书写如下代码(语法正确且编译通过)

asm

  MOV     EAX,X+4

end;

上面的代码也不能将X4的值加载到AX寄存器中;上面的代码的含义是,加载了一个单字的值,该单字存储在距离X的地址有四个字节远的位置。把4加到X的内容中的正确方法是

asm

  MOV     EAX,X

  ADD     EAX,4

end;

 

主题组

 

相关主题

表达式(语法元素)

表达式:概述