48

I am trying to understand how the assembly language works for a micro-computer architecture class, and I keep facing different syntaxes in examples:

sub $48, %esp
mov %eax, 32(%esp)

What do these codes mean? What is the 32 operand an addition to the esp register?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
juliensaad
  • 2,019
  • 2
  • 20
  • 27

5 Answers5

45

Thats not Intel syntax, its AT&T syntax, also called GAS syntax.

the $ prefix is for immediates (constants), and the % prefix is for registers (they are required1).

For more about AT&T syntax, see also the [att] tag wiki.


1 Unless the noprefix option is specified, see here & here. But usually noprefix is only used with .intel_syntax noprefix, to get MASM-like syntax.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Necrolis
  • 25,836
  • 3
  • 63
  • 101
  • Thanks a lot, I did not know where to look for this – juliensaad Feb 08 '12 at 15:54
  • 2
    the `%` prefix is not required if using `noprefix` directive http://stackoverflow.com/questions/549347/how-to-use-address-constants-in-gcc-x86-inline-assembly – phuclv Jun 19 '14 at 01:51
  • @LưuVĩnhPhúc: Interesting, I've see that directive before, probably cause its extremely poorly documented :( https://sourceware.org/binutils/docs/as/i386_002dVariations.html – Necrolis Jun 19 '14 at 06:55
  • that's funny but I've never seen percent mark % being used. – Incerteza Oct 27 '14 at 02:54
18

Compared to Intel syntax, AT&T syntax has many differences

  • $ signifies a constant (integer literal). Without it the number is an absolute address
  • % denotes a register
  • The source/destination order is reversed
  • () is used for memory reference, like [] in Intel syntax

So the above snippet is equivalent to

sub esp, 48         ; esp -= 48
mov [esp+32], eax   ; store eax to the value at the address `esp + 32`
phuclv
  • 37,963
  • 15
  • 156
  • 475
  • 1
    Hi - I'm not sure this actually answers the question... what are the $ and % ? – Taryn East Jun 19 '14 at 01:43
  • 1
    @TarynEast they are prefixes for immidiates and registers like Necrolis has said – phuclv Jun 19 '14 at 01:45
  • 1
    Your post showed up in the "low quality" review queue... for not having enough explanation in it. My recommendation was based on that... Feel free to edit it to say what you have said above ;) – Taryn East Jun 19 '14 at 01:53
3

Yes, "32(%esp)" indicates an offset of 32 from %esp.

aqua
  • 617
  • 4
  • 12
  • 1
    GAS calls it "Memory References", documented at: https://sourceware.org/binutils/docs-2.18/as/i386_002dMemory.html x86 concept summarized brilliantly at: https://en.wikipedia.org/wiki/X86#Addressing_modes – Ciro Santilli OurBigBook.com May 10 '15 at 20:44
1

As @Necrolis said, that's written in AT&T syntax. It means:

subtract 48 from the register esp (the stack pointer).
store the contents of eax to the four bytes starting at (esp + 32).
Stephen Canon
  • 103,815
  • 19
  • 183
  • 269
0

This is AT&T syntax for x86. In AT&T % generally denotes a register while $ is reserved for immediates. If you omit th $ the assembler would interpret the 48 as an address.

Gunther Piez
  • 29,760
  • 6
  • 71
  • 103