table of Contents
- The ZF (Zero Status Register)
- PF (parity flag)
- SF (sign flag)
- CF (carry flag)
- OF (overflow flag)
- ADC (bring carry value addition operation)
- SBB (borrow bring worth subtraction)
- cmp
- Comparing the detection result of the conditional jump instructions
- DF flag transfer instruction string and
- pushf and popf
- DEBUG flag register indicates the number of
- After-school checkpoint
Flag register with the normal registers as 16 bits, its main role is to provide some procedural status field, referred to as the program status word
The ZF (Zero Status Register)
When using the operation instruction (such as add, sub, mul, etc.), ZF bit may be modified; the transfer instruction (such as mov, pop, push, etc.) does not modify the ZF bit. When the operation instruction is executed the result is 0 then the the ZF =. 1 , if the result is not 0 then the ZF = 0
sub al, al ;执行完后ZF=1
add al, 1 ;执行完后ZF=0
PF (parity flag)
This indicates that all data bits in a number of 1 is an even number, if the number 1 is even, PF = 1; 0 otherwise
sub al, al ; PF = 1
add al, 3 ; PF = 0
SF (sign flag)
This bit indicates whether the data with the symbol, i.e., whether it is negative. And this register only when the developers when doing signed operation into effect , how to understand this sentence? As we all know, the computer is actually a bunch of NOR gates, which is essentially a bunch of 01 unsigned consisting of (positive), and people in order to express a negative number, the highest bit bit bit as a sign bit, that is negative is artificially added to the list, it can be seen as a bunch of 01 unsigned, but also as a signed number. The program is not known how humans think, so simply use SF register, if a maximum of 1, I give you the SF is set to 1, do you need is your thing.
Note that, here, mul, div instruction will not affect the SF.
mov al 0000 0001B ;可以看作无符号数 1,也能看作有符号数 1
mov al 1000 0001B ;可以看作是无符号数 129,也能看做是有符号数 -127
CF (carry flag)
Carry flag during unsigned arithmetic the most significant bit to a higher bit of the value (or borrow value), the recording of the operation results. In other words when the two numbers together, not saved when (the current exceeds the maximum number of bits) CF2 =. 1 , when the subtraction of two numbers,
; 加法进位
mov al, 98H
add al, al ; 98H + 98H = 130H,其位数已经超过8位,产生了进位,故CF=1
; 减法借位
mov al, 97H
sub al, 98H ; 97H - 98H = 0FFH,由于97H不够减98H,故需要向更高一位借位(借位后相当于197H - 98H = 0FFH),此时CF=1
OF (overflow flag)
Overflow flag only signed operation when only functional operations performed when the machine than the number of bits can be represented by (signed), the flag will be set to 1.
;执行后,al=12F,两个负数相加本来应该等于负数,但由于寄存器只有8位,所以最终al=2F,变为正数,属于溢出
mov al, 97H
add al, 98H
;执行后,al=69,此次运算,一个稍大一点的正数加一个负数,等于一个正数,没有产生溢出,故OF!=1
mov al, 0F1H
add al, 78H
We should note one thing, when discussing OF or CF, please consider each scenario, not to be confused! For example, in determining OF, FF + FF
this operation in the unsigned operation appears to have exceeded the maximum number of bits (assuming adding at 8), so CF2 =. 1 ; in signed arithmetic opinion -1 + -1 = -2
no overflow occurs, so OF = 0
Look example, 50H + 60H
in unsigned arithmetic opinion does not exceed the maximum number of bits, so CF2 = 0 ; while signed arithmetic opinion two positive numbers together into a negative overflow has occurred, so OF = 1
In short, the overflow flag need only consider the two numbers together, whether in line with normal thinking logically (positive number is a positive number + = positive number; normal logic negative + negative = negative), if violated (such as + = positive positive negative, negative, negative + = positive), then it will overflow, then some students might ask a negative number + positive of it? ? Will, a fixed number of registers inside, the biggest negative have? Let alone add a positive number it! So negative + positive, this life can not overflow! .
ADC (bring carry value addition operation)
When we perform the addition of two numbers, it may produce a carry value, if you want to carry this value with the next calculation, how? Assembly language provides a adc instruction (where c may be understood to carry, i.e., carry) , use the instruction and the add instruction is the same, are adc / add operation target 1, the operation target , while inside there are some different: ADC command when performing extra adder adds a value of CF , corresponding to a + B + CF .
This instruction is mainly used in the calculation of larger data , such as computing 10F000H + 202000H , we do:
- Adding 16 to the low - F000H + 2000H = 11000H, since only 16-bit storage register 16, so AX (AX, as default stored in 16-bit adder ADD instruction execution time) stored in the 1000H, CF is set to 1
- Followed by adding high - 10H + 20H = 30H
- Finally, since the lower 16 bits of a carry value, also need to add a high result, the final result is a high 31H
- So the final result is 311000H
Execution steps 2 and 3 are joined together here is instruction adc
SBB (borrow bring worth subtraction)
adc sbb instructions and commands have similar effect, it is always larger for the operation data. sbb instructions use form and sub command the same, but also more than a minus in the internal implementation of CF .
Here put an example, the calculation 012333H - 013333H , we do:
- To subtract the lower 16 bits - 2333H - 3333H = F000H, since 2333H 3333H Save enough, it will find 2333H higher by one bit (in this case 2333H -> 12333H), so at this time CF = 1
- Conduct high subtraction - 01H - 01H = 0H? Not the lower 16 bits borrowed one, that is not the minuend minuend, so at this time of the operation should be 01H - 01H - 01H = -1, which is FFFF
- So the final result is a high- FFFFH (where F is the number of bits based on the current register if the register is 16 bits, then it is FFFF, if the FF is 8) , together known as FFFF F000H
cmp
cmp nature is to do a subtraction (this subtraction no side effects), and then change the flag. As a result of the subtraction and non-signed values to generate the number of symbols vary (because of the signed overflow occurs), it can be divided into instruction cmp unsigned and signed numbers
Analyzing the unsigned cmp
Above is a summary of the law on the books, but I think the ax> = bx something wrong, since AX <= BX , to launch CF = 1 or ZF = 1, then the ax> = bx should launch CF = 0 or ZF = 1
cmp signed number determination
Cmp was no sign, there is a need to discuss the symbol cmp greater than / less than the case, since these two cases may cause an overflow, such as (al) <(bl) when, 080h - 070h (negative - positive => negative + negative) and 080H - = negative> positive + negative) - 0FEH (negative analyzed one by one, in both cases, the result of their different flag register, following on them
- If (ax) <(bx)
- If (ax) <0, (bx) <0, (ax) - time (BX) is not possible inevitable overflow, the result is negative, then OF = 0, SF = 1
- Examples: 080H - 0FFH = 81H
- If (ax) <0, (bx)> 0, (ax) - time (BX) an overflow, the result is positive, then OF = 1, SF = 0
- Examples: 080H - 01H = 7FH
- If (ax) <0, (bx)> 0, (ax) - time (BX) does not overflow, the result is negative, then OF = 0, SF = 1
- Examples: 0FFH - 0FH = 0F0H,
- If (ax) <0, (bx) <0, (ax) - time (BX) is not possible inevitable overflow, the result is negative, then OF = 0, SF = 1
- If (ax)> (bx)
- If (ax)> 0, (bx)> 0, (ax) - (bx) must not overflow, the result is positive, then OF = 0, SF = 0
- Examples: 7FH - 1H = 7EH
- If (ax)> 0, (bx) <0, (ax) - time (BX) an overflow occurs, the result is negative, then OF = 1, SF = 1
- Examples: 7FH - FFH = 80H
- If (ax)> 0, (bx) <0, (ax) - time (BX) does not overflow, the result is positive, then OF = 0, SF = 0
- Examples: 0AH - FFH = 0BH
- If (ax)> 0, (bx)> 0, (ax) - (bx) must not overflow, the result is positive, then OF = 0, SF = 0
In short, under normal circumstances, simply by is equal to SF. 1 determines a (ax) <(bx) or (ax)> (bx), but encountered OF = 1, have negated the SF, the SF is judged finally to such determination (ax) <(bx) is based on (oF == 0 && SF == 1 ) || (oF == 1 && SF == 0); (ax)> (bx) is based on (oF == 0 && SF == 0) || (OF == 1 && SF == 1)
Comparing the detection result of the conditional jump instructions
The above section describes some features cmp instruction, this section is to make the flag register utilized inside of you.
This section describes some of the instructions of jump according to the flag bits of the flag register, only the instructions for unsigned:
- je >>> The jump instruction indicates equal (ax = bx, ZF = 1)
- jne >>> jump is not equal to (ax! = bx, ZF = 0)
- jb >>> below jump (ax <bx, CF = 1)
- jnb >>> jump is not less than (ax> = bx, CF = 0)
- ja >>> than jump (ax> bx, CF = 0 && ZF = 0)
- jna >>> not higher jump (ax <= bx, CF = 1 || ZF = 0)
Here only the instruction fetch register inside the specified bit, and then determines whether the jump here is not presented.
DF flag transfer instruction string and
Before introducing the DF flag, first talk burst transfer instructions according to the operand size divided into two types:
- movsb, the first transmission of a command byte
- movsw, the instruction transfer one word
The operation thereof will be described as assembly language as follows:
- mov byte ptr es: [di], ds: [you] 或者 mov word ptr es: [di], ds: [you]
- add si, X | sub si, X (depending on the size of the transmission, X will be: lower byte, X = 1; the word, X = 2)
- add di, X | sub di, X (depending on the size of the transmission, X will be: lower byte, X = 1; the word, X = 2)
Determined si, di is increasing or decreasing, by the 10th (DF) flag register to determine if DF = 1, then the increased execution, such as add si, 1
or add si, 2
; if DF = 0, then the reduction is carried out
pushf and popf
pushf
For the flag registers onto the stack; popf
for the flag registers pop the stack, and usage push
, pop
almost
DEBUG flag register indicates the number of
A value of 1 | A value of 0 | |
OF | OV | NV |
SF | OF | PL |
ZF | ZR | NZ |
PF | ON | PO |
CF | CY | NC |
DF | DN | UP |