C ++ view each flag in binary details

Source: Internet
Author: User
Tags comparison table

For example, when we use assembly commands such as ADC and SBB, We Have To unbind the content in the FLAG register (EFL) for the sake of attention. View the value of each flag in binary details. Then these commands that need to use the mark register are clear at a glance. --

I can't wait to write a test code to see the secret:
# Include <iostream>
Int main (void)
{
_ Asm
{
Mov al, 0xff
Mov dl, 0x01

Add al, dl
Adc al, dl
}
Return 0;

}
It should be explained in the form of restrained assembly. Here we want to give a 0xff to AL. Why is it easy for AL to be carried? Of course, you can make other numbers, as long as the sum of the two numbers is carried (Here we want to test the CF bit of carry ). Dl I gave the sum of 0x01 to carry exactly and can test the parity and zero value.

We lay down the breakpoint in the red code above, then run the program to break here, and then open the VC register window, we can see the values of each register. EFL =? The value of the Flag register.

There are two ways to view the value of each flag in the flag register:

First, convert the EFL value to binary, and view the value of a flag bit through binary bit.

2. If no flag is displayed in the register window, right-click the register window and select "flag" to display the flag value, as shown in the following figure:
OV =? UP =? EI =? PL =? ZR =? AC =? PE =? CY =?

Here is a comparison table:

You can compare the bit values of each flag Based on the binary data of EFL.

We can see each value of the flag at the starting breakpoint:
OV = 0 UP = 0 EI = 1 PL = 0 ZR = 0 AC = 0 PE = 0 CY = 0
Here, EI = 1 indicates that the instance is in the interrupted state --..
Let's look at EFL = 0x00000202 (1000000010:
1 0 0 0 0 0 0 1 0
IF TF SF ZF AF PF CF
Only IF (EI | DI) is 1, and only one of the brackets is used in the VC register window...

Good! After reading the flags, run the add al and DL statement following F10.

Look at the value of each flag: OV = 0 UP = 0 EI = 1 PL = 0 ZR = 1 AC = 1 PE = 1 CY = 1

Let's take a look at the value of the Flag register: EFL = 0x00000257 (1001010111)
1 0 0 1 0 1 0 1 1 1
IF TF SF ZF AF PF CF

Here, IF indicates that the operation has been interrupted, ZF indicates that the destination operand result is zero, and AF indicates that (AL is a byte) is added to half (4 bits low) to the other half (4 bits high) carry, which can also represent whether to borrow (subtraction ). PF indicates an even number. CF indicates that carry has occurred. It can also represent a bits.

Then press F10 to execute the adc al and DL. The ADC is the addition of the incoming bits. The result is AL = al dl cf.

At this time, AL: 0 0x1 1 = 0x02.
Flag value: OV = 0 UP = 0 EI = 1 PL = 0 ZR = 0 AC = 0 PE = 0 CY = 0 I will not explain it here --

In particular:

DF: indicates the direction of the control flag. It is used to control the direction of processing information in the string processing command. When DF is set to 1, the address change register SI and DI are reduced after each operation, so that the string processing starts from the high address to the low address. When DF is 0, the opposite is true ..

TF: When TF is set to 1, the CPU enters the single-step execution mode, that is, each execution of a command generates a single-step interruption request. This method is mainly used for program debugging.

Here we want to obtain the value of the Mark register with the following commands:

Transmit the LAHF flag register and load the flag into AH.
Send the SAHF flag register and load the AH content into the flag register.
PUSHF marks the inbound stack.
POPF marks the exit of the stack.
PUSHD 32-bit flag into the stack.
POPD 32-bit marks the stack.

The above AF bit reminds me of a common method to carry or borrow digits using ADC or SBB, we can add or subtract two 4-byte data, which can be 2 bytes in height and 2 bytes in height. The ADC/SBB can be used to obtain the CF value when calculating the 2-byte height to add/subtract it for carry or borrow.

For example:
Mov ax, low1
Add ax, low2
Mov sumLow, ax
Mov ax, high1
Adc ax, high2
Mov sumHigh, ax

In this way, carry is implemented. low1 and low2 indicate the lower 2 bytes of the first number and the second number respectively. high1 and high2 indicate the higher 2 bytes of the first number and the second number respectively. When add ax and low2 generate carry, CF = 1. The ADC will be used to obtain the CF value when adding two bytes in height. SumHigh = high1 high2 CF. The final number is the combination of the sumHigh and sumLow. Shape:

DWORD var = 0;
WORD sumHigh = 0;
WORD sumLow = 0;

C:
Var | = sumHigh;
Var <= 16;
Var | = sumLow;
ASM:

Movzx eax, word ptr [sumHigh]
Shl eax, 10 h
Or eax, dword ptr [sumLow]
Mov var, eax

This enables carry. The principle of borrow space is the same, so I will not discuss it here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.