Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why multiple instructions with same opcode and working?

I was looking at instructions and their corresponding opcodes. Instructions such as "je" and "jz" have the same opcode:

je,jz - 0x74 (8 bit)
je,jz - 0x0f84 (16/32 bit).

Why do we have such redundant instructions?

Is it because it makes assembly coding easier? That is, it's easier to understand 'jump if equal' in some cases and 'jump on zero' in other cases. But we don't really code in assembly these days, so does it help?

like image 389
Hrishikesh Murali Avatar asked Dec 01 '22 07:12

Hrishikesh Murali


1 Answers

What's the old quote from that computer architecture book we all had in school? "There's nothing wrong with x86, it's just that a lot of it doesn't make sense."

To answer your question: Likely because "jump on zero" and "jump if equal" are both jumps to a destination address based on the outcome of a previous instruction. That said, the outcome of the previous instruction sets the zero flag (ZF) to 1. JZ may be for "math" and JE may be for "comparison". So from a programmer perspective, it somewhat makes sense to have to two mnemonics. Perhaps the assembler writers of the early days were trying to mimic another popular assembly language.

Looking at the Intel x86 manual for Jcc (set of conditional jump instructions), we can see that both JZ and JE essentially mean "Jump Near if equal (ZF=1)". And then the docs actually mention that this is common for certain sets of jump instructions.

Because a particular state of the status flags can sometimes be interpreted in two ways, two mnemonics are defined for some opcodes. For example, the JA (jump if above) instruction and the JNBE (jump if not below or equal) instruction are alternate mnemonics for the opcode 77H.

like image 77
selbie Avatar answered Dec 05 '22 06:12

selbie