How do I interpret this instruction?

itsols

Trying to understand how cmp works with NASM, On this page I found the following:

CMP r/m32,reg32               ; o32 39 /r            [386]

There are more like this. However, I cannot understand what this line means.

I presume [386] refers to the processor/architecture. Can someone please explain the meaning of the ASM statement?

paxdiablo

r/m32 and reg32 are operand specifications. The first means a 32-bit register or memory operand, the second is a 32-bit register. Section B.1 Key to operand specifications of that page you link to explains this:

Registers: reg8 denotes an 8-bit general purpose register, reg16 denotes a 16-bit general purpose register, and reg32 a 32-bit one. fpureg denotes one of the eight FPU stack registers, mmxreg denotes one of the eight 64-bit MMX registers, and segreg denotes a segment register. In addition, some registers (such as AL, DX or ECX) may be specified explicitly.

Memory references: mem denotes a generic memory reference; mem8, mem16, mem32, mem64 and mem80 are used when the operand needs to be a specific size. Again, a specifier is needed in some cases: DEC [address] is ambiguous and will be rejected by NASM. You must specify DEC BYTE [address], DEC WORD [address] or DEC DWORD [address] instead.

Register or memory choices: many instructions can accept either a register or a memory reference as an operand. r/m8 is a shorthand for reg8/mem8; similarly r/m16 and r/m32. r/m64 is MMX-related, and is a shorthand for mmxreg/mem64.

Similarly, section B.2 Key to opcode descriptions shows how the opcode/operands are encoded:

The codes o16 and o32 indicate that the given form of the instruction should be assembled with operand size 16 or 32 bits. In other words, o16 indicates a 66 prefix in BITS 32 state, but generates no code in BITS 16 state; and o32 indicates a 66 prefix in BITS 16 state but generates nothing in BITS 32.

That explains the o32.

A hex number, such as 3F, indicates a fixed byte containing that number.

That covers the 39, a fixed opcode.

The code /r ... indicates that one of the operands is a memory address or r/m, and another is a register, and that an effective address should be generated with the spare (register) field in the ModR/M byte being equal to the `register value' of the register operand.

And that details how the other operands are stored, though it's not a simple process since there's usually some bit fiddling required. I'd suggest following the links given in that section to the sections detailing how effective addresses and registers are encoded.

The [386] is the level at which the opcode/operand set was introduced.


If you really want to understand the encoding, assemble a few different variations of the cmp statement, and have a look at the machine code they generate. Then try to disassemble them back into source code using sections B.1, B.2, B.2.1 and B.2.5.

That will hopefully greatly speed your understanding of how it works.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I interpret the output of corrplot?

From Dev

How do I interpret an AudioBuffer and get the power?

From Dev

How do I interpret the output of the "time" command?

From Dev

How do I interpret this Namespaced Schema Def?

From Dev

How do I interpret the output of corrplot?

From Dev

How do I interpret dates like 1394862706?

From Dev

How do I interpret an AudioBuffer and get the power?

From Dev

How do I interpret the statistics of a memtest run?

From Dev

How do I interpret the results of gpscat?

From Dev

How do I interpret this header in this curl request?

From Dev

What is this expression in Haskell, and how do I interpret it?

From Dev

How do I interpret a Swift function declaration?

From Dev

How do I interpret the output of php_uname

From Dev

How do I interpret pycaffe classify.py output?

From Java

How do I interpret the memory usage information from htop

From Dev

Camera pose estimation: How do I interpret rotation and translation matrices?

From Dev

How do I tell Python not to interpret backslashes in strings?

From Dev

How do I interpret PostgreSQL error messages from within Go?

From Dev

How do I interpret the response from GET PROCESSING OPTIONS?

From Dev

How do I interpret the orientation of the gradient when using imgradient in MATLAB?

From Dev

How do I interpret the Asus motherboard LED and beep patterns?

From Dev

How do I use / interpret vi shortcuts on linux?

From Dev

How do I interpret the results from dieharder for great justice

From Dev

How do I interpret audit message logged during shutdown?

From Dev

How do I interpret GetExceptionCode results when using SEH?

From Dev

How do I interpret this template signature of priority queue?

From Dev

How do I interpret output from "top" command? How do I see actual CPU load?

From Dev

How can I interpret this macro?

From Dev

JTAG: How do I know the width of the Instruction Register?

Related Related

  1. 1

    How do I interpret the output of corrplot?

  2. 2

    How do I interpret an AudioBuffer and get the power?

  3. 3

    How do I interpret the output of the "time" command?

  4. 4

    How do I interpret this Namespaced Schema Def?

  5. 5

    How do I interpret the output of corrplot?

  6. 6

    How do I interpret dates like 1394862706?

  7. 7

    How do I interpret an AudioBuffer and get the power?

  8. 8

    How do I interpret the statistics of a memtest run?

  9. 9

    How do I interpret the results of gpscat?

  10. 10

    How do I interpret this header in this curl request?

  11. 11

    What is this expression in Haskell, and how do I interpret it?

  12. 12

    How do I interpret a Swift function declaration?

  13. 13

    How do I interpret the output of php_uname

  14. 14

    How do I interpret pycaffe classify.py output?

  15. 15

    How do I interpret the memory usage information from htop

  16. 16

    Camera pose estimation: How do I interpret rotation and translation matrices?

  17. 17

    How do I tell Python not to interpret backslashes in strings?

  18. 18

    How do I interpret PostgreSQL error messages from within Go?

  19. 19

    How do I interpret the response from GET PROCESSING OPTIONS?

  20. 20

    How do I interpret the orientation of the gradient when using imgradient in MATLAB?

  21. 21

    How do I interpret the Asus motherboard LED and beep patterns?

  22. 22

    How do I use / interpret vi shortcuts on linux?

  23. 23

    How do I interpret the results from dieharder for great justice

  24. 24

    How do I interpret audit message logged during shutdown?

  25. 25

    How do I interpret GetExceptionCode results when using SEH?

  26. 26

    How do I interpret this template signature of priority queue?

  27. 27

    How do I interpret output from "top" command? How do I see actual CPU load?

  28. 28

    How can I interpret this macro?

  29. 29

    JTAG: How do I know the width of the Instruction Register?

HotTag

Archive