raphnguyen
raphnguyen

Reputation: 3605

Assembly Language Symbolic opcodes

I'm pretty confused with converting hex machine instructions to symbolic opcodes. I have a few review problems from this section:

What machine code is generated for the instruction: sbb al, 10
sbb reg/mem from reg/mem    0001 10dw | mod reg r/m | disp-lo | disp-hi
sbb immed from reg/mem      1000 00dw | mod 101 r/m | disp-lo | disp-hi | disp-lo | disp-hi
sbb immed from accumulator  0001 110w | data-lo | data-hi
Answer: 01C0A

What is the symbolic opcode for this hex machine instruction: F7 26 10 00
I converted F7 to 1111 0111 and this matches up with these instructions
mul    1111 011w | mod 100 r/m | disp-lo | disp-hi
imul   1111 011w | mod 101 r/m | disp-lo | disp-hi
div    1111 011w | mod 110 r/m | disp-lo | disp-hi
idiv   1111 011w | mod 111 r/m | disp-lo | disp-hi
Answer: mul

I have no idea how to handle these problems and I can't seem to wrap my head around the concepts. Can someone break it down to me in simpler terms?

Upvotes: 0

Views: 1930

Answers (2)

Alexey Frunze
Alexey Frunze

Reputation: 62106

It's rightfully noted that there're multiple possibilities for sbb al, 10, but either your resource is wrong or you picked the wrong data from it. Here's from the intel's manual:

SBB – Integer Subtraction with Borrow

immediate to register        1000 00sw : 11 011 reg : immediate data
immediate to AL, AX, or EAX  0001 110w : immediate data

If you use the second encoding, you get a 2-byte instruction: 1Ch, 0Ah. w distinguishes instructions operating with byte-sized operands from (d/q)word-sized operands. Here w=0 since the operands are bytes. w would be 1 for sbb (r/e)ax, imm.

Now, if you use the first encoding, you get a 3-byte instruction. w has the same meaning. s distinguishes instructions whose immediate is (d)word-sized from those whose immediate is byte-sized (if you look at the instruction description closely, you'll find two forms that differ only in the immediate operand size). Here s=0 because the immediate is byte-sized. reg is the register index. For al (and (r/e)ax) it's 0. With that you arrive at: 80h, 0D8h, 0Ah.

Both encodings are valid encodings of sbb al, 10, but usually the shorter is preferred.

You absolutely must read intel and/or AMD CPU manuals, where the instruction set and instruction encodings are covered, likely read several times in several takes and read both intel and AMD. All the information you need is there, it's just that there's a lot of it and it's not in the best form suitable for learning.

If you want to validate your understanding of instruction encodings, use tools such as: assembler, disassembler and debugger. You can, for example, write db 80h, 0D8h, 0Ah or db 1Ch, 0Ah instead of sbb al, 10 in the code, compile it, then look at its disassembly to see that it's indeed sbb al, 10. You can alter various bits here and there and see what changes they bring about in the disassembly.

Upvotes: 2

See wikipedia pages on x86 and x86 instruction listings

See also Intel's manual on x86 instruction set and many other sites like x86asm, this page.

Searching on the web gives many many references.

And you could use a debugger like gdb to help you.

Upvotes: 4

Related Questions