Page 1 of 1

ADC Instruction Machine Code

Posted: Tue Mar 24, 2009 12:17 am
by tolgacakiroglu
Hello there.

Im working on my simple assembler. Im looking to Intel and AMD documentations;

ADC AL, imm8 : 14 ib
ADC AX, imm16 : 15 iw
ADC EAX, imm32 : 15 id

Im writing a code like that in NASM,

[BITS 32]

ADC al, 0x40
ADC ax, 0x4040
ADC eax, 0x40404040

and looking to machine codes are,

14 40 66 15 40 40 15 40 40 40 40

As you see for AL and EAX, machine codes are true but for AX, there is an extra code: 66

Also i used same code in Delphi. And disassemblied. Same result.

What is wrong? Are the documents wrong? Or compilers are wrong?

(Also this is same in ADD instruction)

Thanks in advance.

Re: ADC Instruction Machine Code

Posted: Tue Mar 24, 2009 1:14 am
by 01000101
iirc (and that's a big *if*), 0x66 is an instruction operand size prefix that tells the CPU that the next instruction will be a word (or maybe, dword) value.

Re: ADC Instruction Machine Code

Posted: Tue Mar 24, 2009 1:38 am
by madeofstaples
It's covered in the 80386's programmers reference manual, in section 16.2 which starts on the end of page 232.

Re: ADC Instruction Machine Code

Posted: Tue Mar 24, 2009 1:42 am
by Combuster

Code: Select all

ADC AX, imm16 : 15 iw
ADC EAX, imm32 : 15 id
Have you noticed that the opcodes are identical? If you were a computer, if you see 15 00 00 cc cc, is that adc eax, 0xcccc0000, or is that adc ax, 0; int3; int3?

To save you from a big list of opcodes, all word (16-bit), doubleword (32-bit), and quadword (64-bit) versions of one instruction map to the same opcode byte. The current operating mode determines which version is used when the processor sees that opcode. To still be able to use shorter sizes, you must add extra information (a prefix) to do something other than its default behaviour.

0x66 is the operand size prefix - it tells the processor to toggle between 16-bit and 32-bit register sizes for the next instruction.

Re: ADC Instruction Machine Code

Posted: Tue Mar 24, 2009 8:53 am
by Troy Martin
Note that it doesn't affect opcode 14h (ADC AL,imm8) because it doesn't share an opcode with another ADC instruction.

OT: Oooh, shiny, old intel manual!

Re: ADC Instruction Machine Code

Posted: Tue Mar 24, 2009 9:37 pm
by earlz
Just about every instruction except for ones involving segment registers requires a operand prefix to access the 16bit registers from a 32bit environment. Although iirc there aren't any instructions that require a operand prefix to access 8bit registers.