Is there any way to change every flag manually? Or do you have to use a command with a result that you know will change them?
Basically I'm working with the command RCL
, and I don't want to get 1 at the begining, so I want to change the CF to 0, and I know that I can use commands like:
mov al, 0
shl al, 1
But I want to know if there is any other way to do that, without the use of another commands result.
I would also want to know whether the way you may show me, can also be used to change all of the flags, not only CF, but OF, ZF, and so on.
There isn't any instruction that treat eflags
as read-write GP-register.
Quoting Intel1:
Some of the flags in the EFLAGS register can be modified directly, using special-purpose instructions (described in the following sections). There are no instructions that allow the whole register to be examined or modified directly.
The following instructions can be used to move groups of flags to and from the procedure stack or the EAX register:
LAHF, SAHF, PUSHF, PUSHFD, POPF, and POPFD. After the contents of the EFLAGS register have been transferred to the procedure stack or EAX register, the flags can be examined and modified using the processor’s bit manipulation instructions (BT, BTS, BTR, and BTC).
The eflags
register is divided into three groups: Status flags, Control flags and System flags.
Of the Status flags only the CF can be manipulated directly with clc
, stc
, cmc
.
There is no instruction to read the CF but you can read it indirectly with instructions like cmovcc
, adc
, setcc
.
All other flags need to be modified with specially tailored arithmetic instructions or by coping the Status group content of eflags
into ah
(with lahf
) or the stack (with pushfd
) and than back into eflags
(with sahf
or popfd
).
In the Control flags group there is only DF that can be manipulated with cld
and std
.
To read the current value of DF
you need to use pushfd
.
The System flags are usually manipulated indirectly by performing some privileged operation like switching a task, entering v86 mode and similar.
The IF can be manipulated directly with cli
and sti
.
All other flags can only be manipulated with pushfd
/popfd
.
For reference:
- In 64-bit mode the flag register is
rflags
but the higher 32 bits are reserved so far, therebyrflags
is handled aseflags
. pushfd
pusheseflags
on the stack. There is also a 16-bit versionpushf
that pushes only the lower 16 bits ofeflags
. Same forpopfd
/popf
.lahf
/sahf
only copies the status flags.
1 Intel Manuals, Volume 1, Section 3.4.3.
来源:https://stackoverflow.com/questions/39816491/how-to-change-flags-manually-in-assembly-code-for-8086