14

I'm learning about x86 inline assembly programming.

I wanted to write mov ecx, FFFFFFBB, however the compiler isn’t recognizing it. How should hex numbers like that be written in inline assembler code?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Arcytoi
  • 163
  • 1
  • 3
  • 8

4 Answers4

20

It depends on the flavour of your assembler.

  • AT&T: movl $0xFFFFFFBB, %ecx
  • Intel: mov ecx, 0FFFFFFBBh

FYI, AT&T syntax is used by assemblers such as the GNU Assembler, whereas NASM and most of others use Intel's one.

Benny
  • 4,095
  • 1
  • 26
  • 27
15

See the tag wiki for links to assembler manuals, and lots of other stuff.


Different x86 assemblers support one or both of these syntaxes for hex constants:

  • 0xDEADBEEF: NASM (and compat), GNU as, FASM, MSVC inline asm (but not MASM), emu8086.
  • 0DEADBEEFh: NASM (and compat), FASM, MASM, TASM, emu8086.

DOS/Windows-only assemblers often only support the ...h syntax.
Portable assemblers typically support the 0x... syntax, or both.

Note the leading 0: Numeric constants always have to start with a decimal digit to distinguish them from symbol names. (How do I write letter-initiated hexadecimal numbers in masm code? is specifically about that, for trailing-h style.)

Also note that assemblers, like C compilers, can evaluate expressions at assemble time, so you can write foo & 0xF (if foo is an assembler constant, defined with foo equ 0xABC or something). You can even add/subtract from labels (which are link-time constants, not assemble-time), so stuff like mov eax, OFFSET label - 20 still assembles to a mov r32, imm32 mov-immediate instruction, just with a different 32-bit immediate.


From the NASM manual's section on constants:

Some examples (all producing exactly the same code):

    mov     ax,200          ; decimal 
    mov     ax,0200         ; still decimal 
    mov     ax,0200d        ; explicitly decimal 
    mov     ax,0d200        ; also decimal 
    mov     ax,0c8h         ; hex 
    mov     ax,$0c8         ; hex again: the 0 is required 
    mov     ax,0xc8         ; hex yet again 
    mov     ax,0hc8         ; still hex 
    mov     ax,310q         ; octal 
    mov     ax,310o         ; octal again 
    mov     ax,0o310        ; octal yet again 
    mov     ax,0q310        ; octal yet again 
    mov     ax,11001000b    ; binary 
    mov     ax,1100_1000b   ; same binary constant 
    mov     ax,1100_1000y   ; same binary constant once more 
    mov     ax,0b1100_1000  ; same binary constant yet again 
    mov     ax,0y1100_1000  ; same binary constant yet again

Most assemblers also allow character literals, like '0' for ASCII zero. Or even '0123' for four ASCII digits packed into a 32bit integer. Some support escape sequences (\n'), some (like YASM) don't. NASM only supports escape-sequences inside backquotes, not double quotes.


Other platforms:

ARM assembler: 0xDEADBEEF works.

I think 0x... is typical. the 0...h is mostly a DOS thing.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • _MASM_ assemblers by itself doesn't understand 0x prefix even on modern versions of Microsft's assemblers. If you are compiling inline assembly code in VC/VC++ it should accept it within in an `__asm` statement. I don't know an authoritative source, but that has been my experience with _MASM_ / Visual C/C++ for quite a few years. – Michael Petch May 11 '16 at 15:53
  • 3
    I did confirm that the last version of VC++ that produced 16-bit code (1.52c released in 1993) supported `0x` as a prefix with inline assembly. I took the latest VS 2015's _MASM_ and it failed to assemble an assembly file using `0x` prefix and aborts with `error A2206: missing operator in expression` – Michael Petch May 11 '16 at 16:09
  • @MichaelPetch: Thanks for checking on that. Updated my answer to not be biased against the DOS-style `0...h` required syntax, even though I personally don't like it. – Peter Cordes May 11 '16 at 20:44
2

It depends on your assembler, but a common notation for hex literals is 0FFFFFFBBh.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
1

Hex numbers are generally always represented with a leading 0x, so you'd use 0xFFFFFFBB.

Marc B
  • 356,200
  • 43
  • 426
  • 500