1

In AT&T Assembly Syntax, literal values must be prefixed with a $ sign

But, in Memory Addressing, literal values do not have $ sign

for example:

mov %eax, -100(%eax)

and

jmp 100 
jmp $100, $100

are different.

My question is why the $ prefix so confusing?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847

2 Answers2

2

jmp 100 is a jump to absolute address 100, just like jmp my_label is a jump to the code at my_label. EIP = 100 or EIP = the address of my_label.

(jmp 100 assembles to a jmp rel32 with a R_386_PC32 relocation, asking the linker to fill in the right relative offset from the jmp instruction's own address to the absolute target.)

So in AT&T syntax, you can think of jmp x as sort of like an LEA into EIP.

Or another way to think of it is that code-fetch starts from the specified memory location. Requiring a $ for an immediate wouldn't really make sense, because the machine encoding for direct near jumps uses a relative displacement, not absolute. (http://felixcloutier.com/x86/JMP.html).

Also, indirect jumps use a different syntax (jmp *%eax register indirect or jmp *(%edi, %ecx, 4) memory indirect), so a distinction between immediate vs. memory isn't needed.


But far jump is a different story.

jmp ptr16:32 and jmp m16:32 are both available in 32-bit mode, so you do need to distinguish between ljmp *(%edi) vs. ljmp $100, $100.

Direct far jump (jmp far ptr16:32) does take an absolute segment:offset encoded into the instruction, just like add $123, %eax takes an immediate encoded into the instruction.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • semi-related: [What does an asterisk \* before an address mean in x86-64 AT&T assembly?](https://stackoverflow.com/q/9223756) for more about indirect jumps; `jmp *100` is what disambiguates from `jmp 100`. – Peter Cordes Dec 11 '22 at 21:39
0

Question: My question is why the prefixed $ so confused ?

$ prefix is used to load/use the value as is.

example:

 movl $5, %eax #copy value 5 to eax
 addl $10,%eax # add 10 + 5 and store result in eax

$5, $10 are values (constants) and are not take from any external source like register or memory

In Memory addressing, Specifically "Direct addressing mode" we want to use the value stored in particular memory location.

example:

movl 20, %eax

The above would get the value stored in Memory location at 20.

Practially since memory locations are numbered in hexadecimal (0x00000000 to 0xfffffffff), it's difficult to specify the memory locations in hexadecimals in instructions. So we assign a symbol to the location

Example:

.section .data
mydata:
long 4 

.section .text
.globl _start
_start
movl mydata, %eax

In the above code, mydata is symbolic representation given a particular memory location where value "4" is stored.

I hope the above clears your confusion.

Niranjan M.R
  • 343
  • 1
  • 6
  • 23