Search code examples
assemblyx86-16eflags

Assembler 8086 status FLAGS


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

Solution

  • 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