3
    byte x = 0x0c;
    byte y = 0x05;

    System.out.println((x&0xf) + (y&0xf)); // if > 15 then af on
    System.out.println((b&0xf) - (c&0xf));  
    System.out.println((int)(b&0xff) + (c&0xff)); // if > 255 then cf on    
    System.out.println(((int)(b&0xff) + (c&0xff)) & 0b10000000); // if 128 sign flag set
    System.out.println((int)(b&0xff) - (c&0xff)); // if less than 0 then cf set
    System.out.println(((int)(b&0xff) - (c&0xff)) & 0b10000000); //if 128 then sign flag set

I am building an 8085 simulator, and under the Arithmetic & Logical Instructions, I was trying to find a reliable method to check when to set or unset the 5 flags.

Out of the 5, sign flag, zero flag and parity flag are easy to check for, however carry and auxiliary carry flag gets complex. My method of approaching to check the carry flag & auxiliary carry flag are posted above which don't seem very reliable.

Any better/cleaner/more optimal solution is welcome. I have also been using www.sim8085.com to check my mini code snippets and check which flags are getting turned on, here is where I arrived at my next question :

    mvi a, ffh
    mvi b, cch
    sub b
    hlt

When I run this code, the auxiliary flag gets set. From what I know, the auxiliary flag only gets set if there is a carry from the lower nibble to the higher nibble. Hence I'm not being able to understand the logic behind the same.

Would also like to know if all flags are only affected by changes to accumulator or otherwise as well?

EDIT:

        byte b = 0x2f;
        byte c = 0x2c;

        byte d = (byte) ((byte) (b&0xf) + (c&0xf)); 
        byte e = (byte) ((byte) (b&0xf) + ((~c)&0xf) + 1); 

        int x = (int) ((b&0xff) + (c&0xff));
        int y = (int) ((int) (b&0xff) + ((~c)&0xff) + 1); 

        System.out.println(y);
        System.out.println((d>0xf) + "\t" + (e>0xf));
        System.out.println((x>0xff) + "\t" + (y>0xff));

I tried using this logic to see if it works for all mathematical operations, apparently this doesn't make it as well to reliably check if carry or auxiliary carry flag should be set. Kindly help me figure out how to reliably check if an operation results in carry flag and auxiliary carry flag being set or unset.

  • 2
    Set: OR with bit value `sr |= BIT;` Reset: AND with negation of bit value: `sr &= ~BIT;` – markspace Feb 19 '19 at 16:54
  • I meant how to perform the check to see if carry flag is to be set or unset, and the same with auxiliary carry flag – Total Anime Immersion Feb 19 '19 at 17:04
  • Well, what bit position is the carry flag at? – markspace Feb 19 '19 at 17:10
  • 0th bit position for carry and 4th for auxiliary carry – Total Anime Immersion Feb 19 '19 at 17:43
  • So then how does one test just the 0th bit position? (Hint: look at the two equations I posted above.) – markspace Feb 19 '19 at 21:22
  • Maybe I'm understanding your answer wrong, or you're understanding my question wrong. I can set the bit or unset it in my simulator. Same goes with checking 1 bit position, but what I'm really interested in knowing is if there is a reliable way to check if a carry occours after an arithmetic or logic instruction so I can simulate the same in my simulator. For example, when ADD B is executed, I wanna know if the resultant will trigger the carry flag and a general equation for checking if the carry flag and auxiliary carry flags are triggered. I don't have a system setting up the flags for me to – Total Anime Immersion Feb 19 '19 at 23:03
  • check and extract data from. I need to do that myself and that's where I'm seeking help. – Total Anime Immersion Feb 19 '19 at 23:03

1 Answers1

0
        byte b = (byte) 0x1c;
        byte c = (byte) 0xff;

        char a = (char) (Byte.toUnsignedInt(b) + Byte.toUnsignedInt(c));
        char d = (char) (Byte.toUnsignedInt(b) - Byte.toUnsignedInt(c));

        System.out.println(Integer.toHexString(a));
        System.out.println(Integer.toHexString(d));

        if(a > 0xff) {
            System.out.println("Add Carry");
        }

        if(d > 0xff) {
            System.out.println("Sub Carry");
        }

        if((Byte.toUnsignedInt(b) & 0xf) + (Byte.toUnsignedInt(c) & 0xf) > 0xf) {
            System.out.println("Add Aux Carry");
        }

        if((Byte.toUnsignedInt(b) & 0xf) + (Byte.toUnsignedInt((byte) ~c) & 0xf) + 1 > 0xf) {
            System.out.println("Sub Aux Carry");
        }

I did some further checking and figured char data type is unsigned in java and hence it can be used with the logic above for the required results.