0

I wrote the following assembly program to get the opcode of various registers:

.section .text
.globl _start
_start:
add $1,%eax                                                          
add $1,%ebx                                                          
add $1,%ecx                                                          
add $1,%edx                                                          
add $1,%esp                                                             
add $1,%ebp                                                             
add $1,%esi                                                          
add $1,%edi                                                          
int $0x80

I compile and link with:

gcc -c prog.s && ld prog.o

and the object dump is:


0000000000400078 &lt_start&gt:
  400078:   83 c0 01                add    $0x1,%eax
  40007b:   83 c3 01                add    $0x1,%ebx
  40007e:   83 c1 01                add    $0x1,%ecx
  400081:   83 c2 01                add    $0x1,%edx
  400084:   83 c4 01                add    $0x1,%esp
  400087:   83 c5 01                add    $0x1,%ebp
  40008a:   83 c6 01                add    $0x1,%esi
  40008d:   83 c7 01                add    $0x1,%edi
  400090:   cd 80                   int    $0x80

I would expect %ebx to have opcode c1, which is is not the case. So my question is: why is c3 the opcode of %ebx for these add instrucions?

builder-7000
  • 7,131
  • 3
  • 19
  • 43
  • 5
    cross-site duplicate of [Why are first four x86 GPRs named in such unintuitive order?](https://retrocomputing.stackexchange.com/q/5121). x86 register codes go EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI. – Peter Cordes Oct 15 '18 at 05:20
  • 5
    And BTW, that's not the opcode, that's the ModRM byte that encodes the operand (addressing mode and register) for [`83 /0` `add r/m32, imm8`](http://felixcloutier.com/x86/ADD.html). The `/r` field of the ModRM byte is `0`, used as extra opcode bits. – Peter Cordes Oct 15 '18 at 05:25
  • Those links were very helpful, thanks. – builder-7000 Oct 15 '18 at 05:32
  • 1
    related, with some information on the order: [Do we also refer to the registers RAX, RBX etc as R1, R2 and so on?](https://stackoverflow.com/q/9129933/995714). In nasm you can get the number in the modrm byte as expected if you use r0, r1, r2... with the option `%use altreg` – phuclv Oct 15 '18 at 07:43

0 Answers0