0

I see the following instruction format description of ADD according to X86 SDM manual.

enter image description here

This is somehow different from what I have seen before in 8086 instruction format. There we have

OPCODE  |  DIRECTION | WIDTH         MOD | REG | R/M
  6           1          1            2     3     3

According to the figure, if we assume 04 as a one byte instruction code, then it will be 00000100. I guess Ib is the immediate byte. Therefore, the second byte will be the immediate number.

Sound confusing. Any comment?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
mahmood
  • 23,197
  • 49
  • 147
  • 242
  • 2
    Yes, it's different. x86 has various formats. Luckily you don't have to guess because the SDM has a whole _CHAPTER 2 INSTRUCTION FORMAT_ and also section _3.1 INTERPRETING THE INSTRUCTION REFERENCE PAGES_ – Jester Jun 29 '19 at 10:08
  • OK. Also according to the manual `Adds the destination operand (first operand) and the source operand (second operand) and then stores the result in the destination operand`. As I execute `__asm__("add %ecx, %eax");` I see that the value of eax is changed and not ecx. – mahmood Jun 29 '19 at 10:43
  • GDB shows `01 c8 add %ecx,%eax`. The opcode is `000000,0,1 11,001,000`. I see in the manual that 001 is ecx and 000 is eax. Therefore, ecx is considered as the source operand. It should be destination. – mahmood Jun 29 '19 at 11:10
  • 3
    at&t syntax has operands reversed. Switch your tools to intel syntax (`gcc -masm=intel` and `set disassembly-flavor intel` in `gdb`). – Jester Jun 29 '19 at 11:36
  • The OPCODE 06 is the instruction `PUSH ES`. – Sir Jo Black Jun 29 '19 at 11:53
  • 2
    @SirJoBlack the 6 means 6 bits there, it's not an actual value. – Jester Jun 29 '19 at 12:01
  • Ops, Excuse me, I had read wrong! – Sir Jo Black Jun 29 '19 at 12:07
  • The instructions in your question are the special short-form AL/AX/EAX/RAX, immediate form with no ModRM byte: the accumulator destination is implicit. See [What is the significance of operations on the register EAX having their own opcodes?](//stackoverflow.com/q/38019386) and [Tips for golfing in x86/x64 machine code](//codegolf.stackexchange.com/a/160739) for some x86 history about why 8086 dedicated extra opcodes to AL/AX short forms of immediate instructions. – Peter Cordes Jun 29 '19 at 19:24

0 Answers0