简体   繁体   中英

cmp and subcc/others in assembly

SPARC assembly has a cmp instruction, which compares two numbers (using subtraction, if I understand correctly). It also has subcc , addcc , andcc , and others. What is the difference between comparing two numbers using cmp and setting condition codes after performing a calculation? I'm having trouble wrapping my mind around the concept.

What is the difference between comparing two numbers using cmp and setting condition codes after performing a calculation?

Generally (eg on x86 or 68000 type processors) , cmp only sets the status flags, without modifying the operands or storing the result anywhere.

sub etc. are also changing the destination operands (they need to store the result somewhere ), which is not necessary when doing comparison operations.

In essence, cmp is a sub operation where the result is simply discarded (probably saving instruction cycles). When doing comparisons, you do not need the result, you only need to know whether the result is zero or not, and whether it is negative or not.

On SPARC, in particular , cmp is a "synthetic instruction" provided for better readability which is eventually converted by the assembler into a subcc instruction. See "Synthetic instructions" at http://moss.csc.ncsu.edu/~mueller/codeopt/codeopt00/notes/sparc.html .

So, cmp %reg1,%reg2 is converted to subcc %reg1,%reg2,%g0 by the assembler. This subtracts the two registers, and effectively also discards the result by storing it into register %g0 . %g0 is a register which always returns 0 when read, and does not change when being written to. So, on an instruction level, there is no difference at all between cmp and subcc (in other words, SPARC does not have a separate cmp instruction, but uses subcc with a special destination register for comparison).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM