-1

I want to check the Status Flag after a command but it gives wrong values! For example: After adding 126 with 127 Status Flag would be FFBA(initial SF value is FFFF), BUT... when i run this code, it gives 7112:

mov ax, 126
mov bx, 127
PUSHF
MOV dx, 0FFFFh
PUSH dx
POPF
add ax, bx
PUSHF
POP ax
POPF
4

2 回答 2

3

You can't pop to flags (flag register) any value, because some of them are system or reserved!

Flags efected after ADD instructions are OF, SF, ZF, AF, PS and CF

FLAG REGISTER BITs:

    BIT  Flag   NAME
    0    CF     Carry flag  S
    1    1      Reserved     
    2    PF     Parity flag S
    3    0      Reserved     
    4    AF     Adjust flag S
    5    0      Reserved     
    6    ZF     Zero flag   S
    7    SF     Sign flag   S
    8    TF     Trap flag (single step) X
    9    IF     Interrupt enable flag   C
    10   DF     Direction flag  C
    11   OF     Overflow flag   S
    12,13 1,1   I/O privilege level (286+ only) always 1 on 8086 and 186
    14  1       Nested task flag (286+ only) always 1 on 8086 and 186
    15  1       on 8086 and 186, should be 0 above  Reserved
于 2011-12-12T16:44:34.933 回答
2

Elimantas,

As GJ answered you, you cannot directly pop back to the register flag since some of these flags are READ-ONLY flags, but instead use instructions targeting some of these flags individually.

CLC - Clear Carry Flag

STC - Set Carry Flag

CLD - Clear Direction Flag

STD - Set Direction Flag

CLI - Clear Interrupt Flag

STI - Set Interrupt Flag

CMC - Complement Carry flag. Inverts value of CF.

LAHF - Load AH with low 8bits of Register Flags:

AH bit: 7 6 5 4 3 2 1 0

    [SF] [ZF] [0] [AF] [0] [PF] [1] [CF]

SAHF - Store AH into low 8bits of Register Flags:

AH bit: 7 6 5 4 3 2 1 0

    [SF] [ZF] [0] [AF] [0] [PF] [1] [CF]

Now if you want to check the flags and take appropriate action in response to status of these flags, it is better to use "Conditional jump", like:

JNZ, JZ, : Jump if Zero Flags is Clear or Set respectively.

JNC, JC : Jump if Carry Flags is Clear or Set respectively.

JNO, JO : Jump if Overflow Flags is Clear or Set respectively.

JPE, JPO: Jump if Parity Flags is Even or Odd respectively.

And other conditional jumps exist, just check the 8086 instruction reference.

Another thing you should look for is the set of instructions that affect the Flags Register, not only the ADD instruction, many others affect this register, you will find it on the instruction reference manual.

Lastely, if you want to check the flags directly from the Flags Register, just:

PUSHF

POP AX; AX will contain the status of Flags register

Hope this was helpful to you.

Khilo - ALGERIA

于 2011-12-12T21:31:41.607 回答