Maybe you could use instructions like CLC, STC, etc., to work directly with the real FLAGS in the ISR. Then when you get the state of flags you need, you can:
- Read the first byte of FLAGS from the stack into, say AL register.
- Apply AND mask to AL with the corresponding bits set to 0 to clear the old flags you need. For instance, if you need Carry flag the AND mask should be 11111110b.
- Now that you have "emptied" the old flags, you can use the LAHF instruction to load the real current first 8 bits of FLAGS into AH.
- Apply AND mask to AH with the correspondign bits set to 1 to save only the new flags you need. For instance, if you need Carry flag the AND mask should be 00000001b
- Now apply OR AL,AH
- Store the updated flags in the proper stack position
In a more natural way, I think you could do normal operations and based on them, again do something similar and just store the state of the current FLAGS. For example, maybe you could use something like XOR AX,AX to set the Zero Flag to 1 to indicate Zero, or use RCR/RCL to alter the Carry Flag in interesting ways.
Or you could make a function that does all of that automatically and all you have to do is pass it a bitmask with the bits set to 1 for the flags you want updated in the stack, and all you would need to concentrate in would be to keep the intended flags up to date and then call this function to update the flags in the stack. Something like:
Code: Select all
.....
do something
.....
update_stack_flags(00000001b, stack_flags_ptr_offset); //only update the CF
But you would also need perfectly at all times where in the stack is your EFLAGS copy before calling such function as specified in the stack_flags_ptr_offset address parameter or attempting to update it "by hand".
The LAHF instruction stores these flags in AH:
Code: Select all
Bits in AH:
0 -- (CF) Carry Flag
1 -- (1) Always set to 1
2 -- (PF) Parity Flag
3 -- (0) Always set to 0
4 -- (AF) Auxiliary Carry Flag
5 -- (0) Always set to 0
6 -- (ZF) Zero Flag
7 -- (SF) Sign Flag
The only warning is that the LAHF instruction (0x9F opcode) may or may not be supported in 64-bit mode.