In-depth understanding of computer systems - Chapter III -3.6 control

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_40199047/article/details/102533909

3.6 control (control)

jumpThe instructions may change the order of execution of a set of machine code instructions.

3.6.1 Condition Code (condition code)

Condition code register.
CF2 (In Flag with Carry) : Carry flag . Recent operating the highest position a carry. It is used to check unsigned overflow operation.
ZF (ZERO Flag) : Zero flag . Recently the results of the operation is zero.
SF (Signed In Flag) : a sign flag . Recent results obtained in the negative.
OF (overflow Flag) : overflow flag . Recent operating results in a complement overflow - positive or negative overflow overflow.

Sign flag( ) carried out effect
CF (unsigned) t < (unsigned) a Unsigned overflow
ZF t == 0 zero
SF t < 0 negative number
OF a < 0 == b < 0 && (t < 0 != a<0 Signed overflow

leaq Instruction code does not change any condition, because it is used for address calculations.
Write pictures described here

3.6.2 Access condition code

Condition code is not normally read directly, using a method commonly used in three ways:
1) some combination according to the condition code, one byte is set to 0 or 1;
2) is a conditional jump to another program portion;
3) can be conditionally transmitted data.
Write pictures described here

3.6.3 jump instruction

Write pictures described here

3.6.4 jump instruction encoding

Write pictures described here
Write pictures described here

3.6.5 controlled conditions to achieve with Conditions

The conditional expressions and statements translated from the C language into machine code, the most common way to combine conditional and unconditional jumps.
Write pictures described here
The general form of C language template if-else statement is as follows:

if (test-expr)
    then-statement
else
    else-statement

The corresponding assembly is typically used to achieve this the following form. In C language syntax to describe the flow of control:

t = test-expr;
if (!t)
    goto false;
then-statement;
goto done;
false:
    else-statement;
done:

3.6.6 implemented by a conditional branch conditional transfer

The conventional method is achieved by using the operation condition of the control to be redirected (condition is detected, A; otherwise, execution B).
An alternative strategy is to use the data in the transfer conditions (under certain conditions, the conditions previously listed results, according to the data results directly select the next path condition is performed).
Example ( retreturned parameters stored in %raxthe):
Write pictures described here
penalties branch misprediction of
Write pictures described here
conditional transfer instructions
Write pictures described here
Write pictures described here
if a conditional branch any side effects or possible error condition will result in an illegal act.
Write pictures described here

3.6.7 cycle

1. do-while loop

Write pictures described here
Write pictures described here
Write pictures described here

2. while loop

Here Insert Picture Description
The first translation method jump to middle( )
Here Insert Picture Description
Example:
Here Insert Picture Description
The second translation method guarded-do( )
Write pictures described here
Example:
Write pictures described here
Write pictures described here
interesting features. Cycle test (the first assembly code 9lines) from the original Ccode n>1becomes n != 1. When the compiler knows only n > 1when entering the loop, it will nreduce 1mean n > 1or n = 1. Therefore, the test n != 1is equivalent to testing n >= 1. (I have not verified)

3. for loop

The general format of the for loop:

for (init-expr; test-expr; update-expr)
    body-statement

Equivalent to:

init-expr:
while(test-expr){
    body-statement
    update-expr;
}

Jump to the middle of the strategy goto Code:

    init-expr;
    goto test;
loop:
    body-statement;
    update-expr;
test:
    t = test-expt;
    if(t)
        goto loop;

The guarded-do policies get:

    init-expr;
    t = test-expr;
    if(!t)
        goto done;
loop:
    body-statement;
    t = test-expt;
    if(t)
        goto loop;
done:

Write pictures described here
In summary, all three forms of C language cycle of - , do-while, whileand forcan be used to translate a simple strategy, generates the code comprising one or more conditional branch. Controlled conditions to provide a basic transfer mechanism for circulating translated into machine code.

3.6.8 switch statement

switch(Switch) statements can be multiple branches (in accordance with an integer index multiway branching).
The jump table is an array entry iis the address of a code segment, the code segments is equal to the index value when the switching ioperation of the program should be taken.
Example:
Write pictures described here
Write pictures described here
Jump table
Write pictures described here

Guess you like

Origin blog.csdn.net/weixin_40199047/article/details/102533909