assemblyx86flagscarryflageflags

Intel x86 Assembly - Reading and copying the carry flag


I am currently creating a program to convert an decimal into binary. I am currently using the shifting method in order to get the 0's and 1's.

I have looked at other threads which leads to using LAHF and PUSHF to access the flags and set it; however, all I want to do is get the current value of the carry flag and store it into a register (i.e: 1's or 0's at that current position) which will later be copied into the array. Since I am not too familiar with LAHF or PUSHF, I was wondering if you can complete the same task I asked above using LAHF or PUSHF.

For example, I want to move the current CF into eax: mov edx,cf (I know this doesn't work, but I am looking for something like this). After I have copied either the 1 or 0, I will then move it into the array which was passed from a C++ program.


Solution

  • I find the most obvious way to store the carry in EDX is:

    setc  dl
    movzx edx, dl
    

    It is shorter (6 bytes) than the methods proposed in the other answers but the shortest (5 bytes) has to be:

    pushfd
    pop   edx
    and   edx, 1