How is the comparison of values ​​in the program performed?

How does a computer perform equality comparisons? Does a bit match a bit, starting from the smallest bit, and stop when two different bits are encountered? Or does it start with the highest bit? Does it go through all bits regardless of where / when two dissimilar bits are found?

+5
source share
1 answer

When you write an equality comparison in a higher-level language (for example, c), it will be converted to an intermediate representation, and then to the instructions of the specific platform with which this code will be executed. The compiler is free to perform equality comparisons using any of the instructions available in the target architecture. The idea is usually made faster.

Different architectures have different sets of instructions. Different processors may have different implementation strategies (again, to speed things up) if they meet the specification.

Below are some examples.

x86

The CMP command is used to compare two values. Here is an excerpt from the instructions to set the link .

Compares the first operand of the source with the second source operand and sets the status flags in the EFLAGS register according to the results. Comparison is performed by subtracting the second operand from the first operand and then setting the status flags in the same way as the SUB instruction. When an immediate value is used as an operand, it is expanded to the length of the first operand.

This basically means that all bits are checked. I suppose this was implemented in such a way as to allow comparisons without equality (<,>).

So, all bits are checked. In the simplest cases, this can be done in series, but can be done faster. See wikibooks on add / subtract blocks .

Add a block diagram with hyphenation

ARM

Command

TEQ can be used to check two values ​​for equality. Here is an excerpt from infocenter.arm.com

The TEQ command performs the Exclusive OR bitwise operation for the value in Rn and the value of operand2. This is the same as the EORS statement, except that it discards the result. Use the TEQ statement to check if the two values ​​are equal without affecting the V or C flags.

Again, all bits are checked.

+5
source

Source: https://habr.com/ru/post/1260760/


All Articles