1
$\begingroup$

My initial guess is that you will have more instruction space for the immediate in your branch instruction when you first use a CMP instruction. However, you have to use 2 instructions each time you want to branch: a CMP and then the branch instruction.

So are there any advantages to using CMP?

$\endgroup$
1
$\begingroup$

Every architecture may offer different flags and condition registers. It also may implement operations (e.g. addition) affecting these flags one way or another. Say an architecture A you mention does not need CMP; this means all flags and registers are set predictably in an orthogonal fashion for each operation (or else that particular jump/branch instructions must be enforced). An architecture B could not have this guarantee and use different flags for each operation and/or offer more generic jump/branch - then B offers a CMP instruction that makes sure flags are set as expected by the programmer/compiler.

There are several differences between A and B. For example, an architecture may offer more registers (X, Y not just ACC) which you could directly run operations on (therefore appropriate CMP instructions makes sense to select register etc). Architecture A may have to implement additional logic or even offer more flag registers to accomplish its aforementioned guarantee. Some processors want an instruction to do minimal register transfers/operations to maintain a given command execution cycle/clock.

These are a few things there are of course much more that turn this and other design choices to a successful architecture for compilers and intended users. Each choice reflects on the instruction set. What is important is to take advantage of each instruction the way it is intended in the best way and produce correct and fast code easily even in an automated way (from compiler).

All of the above possible reasons aside, note that you do not always have to use CMP (or have two instructions to branch) if your flags, condition registers are treated as you need them from the previous operation - you use CMP only if previous operation set flags are not set as expected for a subsequent branch.

Indeed sometimes in assembly written by humans (not compiler) we often just add a CMP in there, even if unnecessary, just to make sure because we may not remember the flags each operation affects (so that is a waste and not the way CMP is intended at least not for performance programming)!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.