What instruction would I use to save the current value of the flags register?

If you do not need to store the entire flags register, you can use the LAHF instruction to manually load and store the status of the lower byte of the flag register in the AH register. SAHF restores the value.

The FLAGS register is the status register that contains the current state of a x86 CPU. The size and meanings of the flag bits are architecture dependent. It usually reflects the result of arithmetic operations as well as information about restrictions placed on the CPU operation at the current time. Some of those restrictions may include preventing some interrupts from triggering, prohibition of execution of a class of "privileged" instructions. Additional status flags may bypass memory mapping and define what action the CPU should take on arithmetic overflow.

The carry, parity, adjust, zero and sign flags are included in many architectures. The adjust flag used to be called auxiliary carry bit in 8080 and half-carry bit in the Zilog Z80 architecture.

In the i286 architecture, the register is 16 bits wide. Its successors, the EFLAGS and RFLAGS registers, are 32 bits and 64 bits wide, respectively. The wider registers retain compatibility with their smaller predecessors.

Intel x86 FLAGS registerBit #MaskAbbreviationDescriptionCategory=1=0FLAGS00x0001CFCarry flagStatusCY(Carry)NC(No Carry)10x0002—Reserved, always 1 in EFLAGS —20x0004PFParity flagStatusPE(Parity Even)PO(Parity Odd)30x0008—Reserved—40x0010AFAdjust flagStatusAC(Auxiliary Carry)NA(No Auxiliary Carry)50x0020—Reserved—60x0040ZFZero flagStatusZR(Zero)NZ(Not Zero)70x0080SFSign flagStatusNG(Negative)PL(Positive)80x0100TFTrap flag (single step)Control90x0200IFInterrupt enable flagControlEI(Enable Interrupt)DI(Disable Interrupt)100x0400DFDirection flagControlDN(Down)UP(Up)110x0800OFOverflow flagStatusOV(Overflow)NV(Not Overflow)12-130x3000IOPLI/O privilege level (286+ only),
always all-1s on 8086 and 186System140x4000NTNested task flag (286+ only),
always 1 on 8086 and 186System150x8000MDMode flag (NEC V-series only),
reserved on all Intel CPUs.
Always 1 on 8086/186, 0 on 286 and later.Control(NEC only)
Native Mode
(186 compatible)(NEC only)
Emulation Mode
(8080 compatible)EFLAGS160x0001 0000RFResume flag (386+ only)System170x0002 0000VMVirtual 8086 mode flag (386+ only)System180x0004 0000ACAlignment Check (486+, ring 3),
SMAP Access Check (Broadwell+, ring 0-2)System190x0008 0000VIF (Pentium+)System200x0010 0000VIP (Pentium+)System210x0020 0000IDAble to use CPUID instruction (Pentium+)System22-290x3FC0 0000—Reserved—300x4000 0000(none)AES key schedule loaded flag
(CPUs with VIA PadLock only)System310x8000 0000—Reserved—RFLAGS32‑630xFFFF FFFF…
…0000 0000—Reserved—

Note: The mask column in the table is the AND bitmask (as hexadecimal value) to query the flag(s) within FLAGS register value.

All FLAGS registers contain the condition codes, flag bits that let the results of one machine-language instruction affect another instruction. Arithmetic and logical instructions set some or all of the flags, and conditional jump instructions take variable action based on the value of certain flags. For example, jz (Jump if Zero), jc (Jump if Carry), and jo (Jump if Overflow) depend on specific flags. Other conditional jumps test combinations of several flags.

FLAGS registers can be moved from or to the stack. This is part of the job of saving and restoring CPU context, against a routine such as an interrupt service routine whose changes to registers should not be seen by the calling code. Here are the relevant instructions:

  • The PUSHF and POPF instructions transfer the 16-bit FLAGS register.
  • PUSHFD/POPFD (introduced with the i386 architecture) transfer the 32-bit double register EFLAGS.
  • PUSHFQ/POPFQ (introduced with the x64 architecture) transfer the 64-bit quadword register RFLAGS.

In 64-bit mode, PUSHF/POPF and PUSHFQ/POPFQ are available but PUSHFD/POPFD are not.: 4–349, 4–432 

The lower 8 bits of the FLAGS register is also open to direct load/store manipulation by SAHF and LAHF (load/store AH into flags).

Example[edit]

The ability to push and pop FLAGS registers lets a program manipulate information in the FLAGS in ways for which machine-language instructions do not exist. For example, the cld and std instructions clear and set the direction flag (DF), respectively; but there is no instruction to complement DF. This can be achieved with the following assembly code:

pushf ; Use the stack to transfer the FLAGS pop ax ; ...into the AX register push ax ; and copy them back onto the stack for storage xor ax, 400h ; Toggle (complement) DF only; other bits are unchanged push ax ; Use the stack again to move the modified value popf ; ...into the FLAGS register ; Insert here the code that required the DF flag to be complemented popf ; Restore the original value of the FLAGS

By manipulating the FLAGS register, a program can determine the model of the installed processor. For example, the alignment flag can only be changed on the 486 and above. If the program tries to modify this flag and senses that the modification did not persist, the processor is earlier than the 486.

Starting with the Intel Pentium, the CPUID instruction reports the processor model. However, the above method remains useful to distinguish between earlier models.

Which of the following is true about the pop instruction?

Which of the following is true about the POP instruction? It copies the data pointed to by the stack pointer into the operand, and then increments the stack pointer (by 2 or 4).

When passing procedure parameters on the stack why are the following lines of code often necessary in a procedure?

Terms in this set (39) When passing procedure parameters on the stack, why are the following lines of code often necessary in a procedure? To keep additional usage of the stack within the procedure from invalidating the stack offsets.

What type of tool can convert ARM assembly to x86 assembly?

What type of tool can convert ARM Assembly to x86 Assembly? Architecture's instructions are directly executed by the CPU. A program that combines object files into an executable program is called a linker .

Toplist

Neuester Beitrag

Stichworte