Nick Pandolfi
Nick Pandolfi

Reputation: 993

Difference between assembly zero and equal

I am a complete beginner in the vast world of assembly, and while learning I have come across a weird occurrence.

Conditional jumps are done on the base of flag checking, to see how certain operators compare. However, there seems to be two different ways to do the checking. With almost every conditional jump instruction, there seems to be a counterpart that does the exact same thing, just with different notation. For example, je appears to be the same as jz. As far as I know, both of these instructions jump if the comparison of two operands before the instruction sets the zero flag. What is the difference between the instructions here. Is one more efficient than the other? Does one incur more overhead? Is the differentiation just for readability?

There are a few other instructions that also seem to be the same:

Upvotes: 2

Views: 887

Answers (2)

Deleted User
Deleted User

Reputation: 2541

Two equal numbers give zero when subtracted from each other. A comparison is essentially nothing else than subtracting one operand from the other, without keeping the difference as result. Therefore, testing if the result of an operation is zero, or whether two operands have the same value, using a "compare", is in fact the same operation.

Upvotes: 1

user555045
user555045

Reputation: 64903

There is no difference. They're just two names for exactly the same thing. Look at how they're encoded - it's exactly the same byte sequence, so the CPU (or disassembler) can't know which one you wrote, so it can't act differently based on which one you used.

It's just so can you can make the intention a little more obvious to a fellow programmer, for example by using the z variant if you actually checked for zero (with test perhaps) but the e variant if you just compared two things.

Upvotes: 6

Related Questions