ADC Instruction Machine Code

Programming, for all ages and all languages.
Post Reply
tolgacakiroglu
Posts: 2
Joined: Tue Mar 24, 2009 12:10 am

ADC Instruction Machine Code

Post 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.
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: ADC Instruction Machine Code

Post 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.
madeofstaples
Member
Member
Posts: 204
Joined: Thu Apr 12, 2007 8:15 am
Location: Michigan

Re: ADC Instruction Machine Code

Post by madeofstaples »

It's covered in the 80386's programmers reference manual, in section 16.2 which starts on the end of page 232.
Some people are offended by the verifiable truth; such people tend to remain blissfully unencumbered by fact.
If you are one of these people, my posts may cause considerable discomfort. Read at your own risk.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: ADC Instruction Machine Code

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: ADC Instruction Machine Code

Post 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!
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Re: ADC Instruction Machine Code

Post 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.
Post Reply