Which instruction is used for setting the condition code i




















Those bits change from time to time based on the outcome of some instructions. In this case we have EQ ual condition. If the first number was bigger, we would have a G reater T han condition and in the opposite case — L ower T han. The following table lists the available condition codes, their meanings, and the status of the flags that are tested. We can use the following piece of code to look into a practical use case of conditions where we perform conditional addition.

The program exits with the result 3. In the Instruction Set chapter we talked about the fact that there are different Thumb versions.

Specifically, the Thumb version which allows conditional execution Thumb Each instruction inside the IT block must specify a condition suffix that is either the same or logical inverse. This means that if you use ITE, the first and second instruction If-Then must have the same condition suffix and the third Else must have the logical inverse of the first two.

Here are some examples from the ARM reference manual which illustrates this logic:. This example code starts in ARM state. Thus the C flag will be set to 0 in subtraction this means the actual result does not equal the true result. Thus the C flag will be set to 1 and the Z flag will be set to 0 result not zero.

Thus the C flag will be set to 0 or the Z flag will be set to 1 result zero. Similar observations can be made when the operands are integers, the difference being that the C flag has no meaning, instead the V and N flags have meaning.

Condition VS is true if there was overflow, so the V flag will be set to 1. Condition VC is true if there was no overflow, so the V flag will be set to 0. Condition MI is true if the actual result is negative, so the N flag will be set to 1.

Condition PL is true if the actual result is non-negative, so the N flag will be set to 0. Thus the V flag will be set to 0 no overflow and the N flag will be set to 0 result non-negative. Thus the V flag will be set to 1 overflow or the N flag will be set to 1 result negative , but not both. Thus the V flag will be set to 0, the N flag will be set to 0, and the Z flag will be set to 0 result not zero. Thus the V flag will be set to 1 or the N flag will be set to 1, but not both, or the Z flag will be set to 1 result zero.

A common use of the TST instruction is to determine if the value of a field within a byte or word is zero. For example. The corresponding AL instructions are shown on the right. The notation! We will assume that the values to be compared are in registers R can also be immediate data , so L stands for the register containing the left operand and R stands for the register containing the right operand or the immediate data.

If L is not in a register or R is neither in a register nor immediate data, instructions to load each into a register will have to be included before the CMP instruction. Since the body is executed only if the relation is true, the branch following CMP should branch on the complementary relation, which would mean that rel is false. If body can be coded so that three or fewer instructions are skipped, the AL code pattern should be.

Here, the condition code suffix used is the one for rel not! In the following examples we assume all values are naturals. If the values were integer, we would use the condition codes for integers instead of those for naturals. For an if statement with an else clause, assuming X is the register R0, the AL code is. An else if type structure is similar to the initial if part. Assume X is the register R0. This code illustrates a mixture of the two techniques discussed above. In this section, we continue to assume all values are naturals.

The basic loop structure contains an explicit test for the exit condition somewhere within the body of the loop.

B rel NXT. Execution of the loop is not terminated until the relation in the if is true. In an ordinary while loop.

If the relation is initially false, the body is not executed even once. In a do-while loop. B rel AGN. Even if the relation is initially false, the body is executed at least once. The AL code for a for loop is a bit more complex. In the following examples we assume all values relating to the loop variable are naturals. A for loop is basically a counting loop.

The following is a typical loop counting up. B AGN. Here the value of the loop variable i is kept in some register Ri. The body of the loop body is executed once for each value in the loop variable's range, which in this example is N inclusive. The case where both the upper and lower limits of the loop variable's range are variables is. The simplest way to set the condition flags is to use a comparison operation, such as cmp. This mechanism is common to many processor architectures, and the semantics if not the details of cmp will likely be familiar.

In addition, we have already seen that many instructions such as sub in the example can be modified to update the condition flags based on the result by adding an s suffix. That's all well and good, but what information is stored, and how can we access it? The flags indicate simple properties such as whether or not the result was negative, and are used in various combinations to detect higher-level relationships such as "greater than" and suchlike.

Once I have described the flags, I will explain how they map onto condition codes such as ne in the previous example. The N flag is set by an instruction if the result is negative.

In practice, N is set to the two's complement sign bit of the result bit The C flag is set if the result of an unsigned operation overflows the bit result register. This bit can be used to implement bit unsigned arithmetic, for example. The V flag works the same as the C flag, but for signed operations. The result of the operation would be 0x , but the top bit is lost because it does not fit into the bit destination register and so the real result is 0x In this case, the flags will be set as follows:.

If you fancy it, you can check this with the ccdemo application. The output looks like this:. We have worked out how to set the flags, but how does that result in the ability to conditionally execute some code?

Being able to set the flags is pointless if you cannot then react to them. The most common method of testing the flags is to use conditional execution codes. In effect, attaching one of the condition codes to an instruction causes it to execute if the condition is true. Otherwise, it does nothing, and is essentially a nop.

The following table lists the available condition codes, their meanings where the flags were set by a cmp or subs instruction , and the flags that are tested:. It is fairly obvious how the first few work because they test individual flags, but the others rely on specific combinations of flags. In practice, you very rarely need to know exactly what is happening; the mnemonics hide the complexity of the comparisons.

The cmp instruction that we saw in the first example can be thought of as a sub instruction that doesn't store its result: if the two operands are equal, the result of the subtraction will be zero, hence the mapping between eq and the Z flag. Of course, we could just use a sub instruction with a dummy register, but you can only do that if you have a register to spare. Dedicated comparison instructions are therefore quite commonly used.

There are actually four dedicated comparison instructions available, and they perform operations as described in the following table:. Note that the dedicated comparison operations do not require the s suffix; they only update the flags, so the suffix would be redundant.

Whilst the condition flag mechanism is fairly simple in principle, there are a lot of details to take in, and seeing some real examples will probably be useful! I will make a point of presenting some examples of realistic usage in a future blog post.



0コメント

  • 1000 / 1000