• Aucun résultat trouvé

Literal Constants

Dans le document CODE WRITE GREAT (Page 48-52)

3.3.2 80x86 General-Purpose Registers

3.4 Literal Constants

Most assemblers support literal numeric, character, and string constants.

Unfortunately, just about every assembler out there uses a different syntax for literal constants. The following subsections describe the syntax for the assemblers we’ll be using in this book.

3.4.1 Binary Literal Constants

All assemblers provide the ability to specify base-2 (binary) literal constants.

Few compilers emit binary constants, so you probably won’t see these values in the output a compiler produces, but you may see them in handwritten assembly code.

3.4.1.1 Binary Literal Constants in HLA

Binary literal constants in HLA begin with the special percent character (%) followed by one or more binary digits (0 or 1). Underscore characters may appear between any two digits in a binary number. By convention, HLA programmers separate each group of four digits with an underscore. For example:

%1011

%1010_1111

%0011_1111_0001_1001

%1011001010010101

3.4.1.2 Binary Literal Constants in Gas

Binary literal constants in Gas begin with the special 0b prefix followed by one or more binary digits (0 or 1). For example:

0b1011 0b10101111

0b0011111100011001 0b1011001010010101

3.4.1.3 Binary Literal Constants in MASM and TASM

Binary literal constants in MASM/TASM consist of one or more binary digits (0 or 1) followed by the special b suffix. For example:

1011b 10101111b

0011111100011001b 1011001010010101b

3.4.2 Decimal Literal Constants

Decimal constants in most assemblers take the standard form—a sequence of one or more decimal digits without any special prefix or suffix. This is one of the two common numeric formats that compilers emit, so you will often see decimal literal constants when reading compiler output.

3.4.2.1 Decimal Literal Constants in HLA

HLA allows you to optionally insert underscores between any two digits in a decimal number. HLA programmers generally use underscores to separate groups of three digits in a decimal number. For example, take the following numbers:

123 1209345

In HLA a programmer could insert underscores as shown here:

1_024 1_021_567

3.4.2.2 Decimal Literal Constants in Gas, MASM, and TASM

Gas, MASM, and TASM use a string of decimal digits (the standard “computer”

format for decimal values). For example:

123 1209345

MASM, TASM, and Gas (unlike HLA) do not allow embedded under-scores in decimal literal constants.

3.4.3 Hexadecimal Literal Constants

Hexadecimal (base-16) literal constants are the other common numeric format you’ll find in assembly language programs (especially those that compilers emit).

3.4.3.1 Hexadecimal Literal Constants in HLA

Hexadecimal literal constants in HLA consist of a string of hexadecimal digits (0..9,a..f, or A..F) with a $ prefix. Underscores may optionally appear between any two hexadecimal digits in the number. By convention, HLA programmers separate sequences of four digits with underscores.

For example:

$1AB0

$1234_ABCD

$dead

3.4.3.2 Hexadecimal Literal Constants in Gas

Hexadecimal literal constants in Gas consist of a string of hexadecimal digits (0..9,a..f, or A..F) with a 0x prefix. For example:

0x1AB0 0x1234ABCD 0xdead

3.4.3.3 Hexadecimal Literal Constants in MASM and TASM

Hexadecimal literal constants in MASM/TASM consist of a string of hexa-decimal digits (0..9,a..f, or A..F) with an h suffix. The values must begin with a decimal digit (0 if the constant would normally begin with a digit in the range a..f). For example:

1AB0h 1234ABCDh 0deadh

3.4.4 Character and String Literal Constants

Character and string data are also common data types that you’ll find in assembly programs. MASM and TASM do not differentiate between literal character or string constants. HLA and Gas, however, use a different internal representation for characters and strings, so the distinction between the two literal constant forms is very important in those assemblers.

3.4.4.1 Character and String Literal Constants in HLA

Literal character constants in HLA take a couple of different forms. The most common form is a single printable character surrounded by a pair of apostrophes, such as 'A'. To specify an apostrophe as a literal character constant, HLA requires that you surround a pair of apostrophes by apos-trophes (''''). Finally, you may also specify a character constant using the#symbol followed by a binary, decimal, or hexadecimal numeric value that specifies the ASCII code of the character you want to use.

For example:

'a' '''' ' '

#$d

#10

#%0000_1000

String literal constants in HLA consist of a sequence of zero or more characters surrounded by quotes. If a quote must appear within a string constant, the string constant will contain two adjacent quotes to represent a quote character within the string.

For example:

"Hello World"

"" -- The empty string

"He said ""Hello"" to them"

"""" -- string containing one quote character 3.4.4.2 Character and String Literal Constants in Gas

Character literal constants in Gas consist of an apostrophe followed by a single character. For example:

'a '' '!

String literal constants in Gas consist of a sequence of zero or more characters surrounded by quotes. String literal constants in Gas use the same syntax as C strings. You use the \ escape sequence to embed special characters in a Gas string. For example:

"Hello World"

"" -- The empty string

"He said \"Hello\" to them"

"\"" -- string containing one quote character

3.4.4.3 Character/String Literal Constants in MASM and TASM

Character and string literal constants in MASM/TASM take the same form:

a sequence of one or more characters surrounded by either apostrophes or quotes. These assemblers do not differentiate character constants and string constants. For example:

'a'

"'" - An apostrophe character '"' - A quote character

"Hello World"

"" -- The empty string 'He said "Hello" to them'

3.4.5 Floating-Point Literal Constants

Floating-point literal constants in assembly language typically take the same form you’ll find in HLLs (a sequence of digits, possibly containing a decimal point, optionally followed by a signed exponent). For example:

3.14159 2.71e+2 1.0e-5 5e2

Dans le document CODE WRITE GREAT (Page 48-52)