0

The ARM documentation says the condition code GE is true when "N equals V", ie, Sign bit is same as oVerflow bit. Why is this not just N and V are zero? Can both N and V get set to 1 as a result of CMP instruction? That is, can a subtraction result in setting both sign and overflow to 1?

This question is almost a duplicate of Assembly comparison flags understanding

But can someone explain this with ARM assembly code? Something that I can check in ( https://cpulator.01xz.net/?sys=arm ) ?

1 Answers1

0

You could have written this in a few minutes:

#include <stdio.h>
int main ( void )
{
    unsigned int a,b,c,d;

    for(a=0;a<=7;a++)
    {
        for(b=0;b<=7;b++)
        {
            c = a+b;
            d = (a&3)+(b&3);
            d = (d&4)|(c&8);
            if(c&4)
            {
                if(d==0x4)
                {
                    printf("%u %u\n",a,b);
                }
            }
        }
    }
    return(0);
}


/*

1 3
2 2
2 3
3 1
3 2
3 3


   001
+  011
=======   

  0110
   001
+  011
=======   
   100

*/

so if for example you add 0x20000000 and 0x60000000 you should see N = V = 1

EDIT

based on the above

1+3

  001
+ 011
=======

1-(-3) move the lsbit up to the carry in to make the same math but a subtract.

     1
   001
+  010
========

001 - 101 = 1 - (-3) = 1 + 3

  0111
   001
+  010
========
   100

0x20000000 - 0xA0000000

EDIT 2

mov r0,#0x20000000
mov r1,#0xA0000000
cmp r0,r1
b .

in the emulator changes the cpsr to 0x900001d3 after the compare which is N and V set.

old_timer
  • 69,149
  • 8
  • 89
  • 168
  • Tried it with your interesting simulator, it does generate N = V = 1 as expected. An exercise to the reader to write the assembly language, not going to do that part of your homework for you. – old_timer Nov 15 '19 at 12:32
  • Apologies. I actually wanted to know how both N and V can be set to 1 with a CMP instruction. I have edited the question as well. Could you please have a look? – Satheesh Paul Antonysamy Nov 15 '19 at 13:12
  • Also, what would the condition code GE (greater or equal) mean in the case of an ADD instruction? Will an instruction with suffix GE following this ADD be executed? – Satheesh Paul Antonysamy Nov 15 '19 at 13:53
  • _" I actually wanted to know how both N and V can be set to 1 with a CMP instruction."_ See the table I linked to in my comment to your question. A `CMP` sets the flags based on a subtraction. – Michael Nov 15 '19 at 14:21
  • doesnt matter how you get the flags the instruction will branch if the condition codes are met. so if N == V is the condition and the flags are met then it will branch, however you got those flags set. branch or whatever bge, movge, addge, etc. – old_timer Nov 15 '19 at 15:01
  • "0x20000000 - 0xA0000000"; this does set both N and V. But I am more puzzled now. I'm not really able to understand what your C code is doing and how did you arrive at "0x20000000 - 0xA0000000" from what the program produced? Could you kindly explain the logic? – Satheesh Paul Antonysamy Nov 15 '19 at 15:52
  • I did. 0b001 + 0b011 = 0b100 in a three bit world (it all scales easy to do these things with three or four bits, 30, 300, 3000 bits it all works the same) produces the N bit and V bit the N bit is the msbit of the result and the V bit is set if the carry in and carry out of the msbit are different. so 1 + 3 = 1 - (-3) we know this from grade school so twos complement invert and add one so 1 - (-3) = 1 - ~3 + 1 = 0b001 - 0b101 now that is the same as 0b001 + (-0b101) = 0b001 + 0b010 + 1 and that is how hardware can/does it – old_timer Nov 15 '19 at 21:06
  • carry in of a 1 add 001 plus 010 and I showed that also produces N = V = 1 – old_timer Nov 15 '19 at 21:07
  • but dont need to do all that work simply see that 001 + 011 produces N = V = 1 steal one of the 1 msbits and make it a carry in 001 + 010 + 1, and then ones complement the 010 to see it is a 101 so 001 - 101 is the same as 001 + 011 – old_timer Nov 15 '19 at 21:09
  • to scale these bit patterns to a 32 bit test you simply put them in the top three bits of each number so 0x20000000 and 0xA0000000 (start with 001xxxx and 101xxxx) – old_timer Nov 15 '19 at 21:12
  • the C code is simply doing the "long" addition if you will. for three bit math using variables larger than three bits the carry out will be in the fourth bit (c&8) to do the carry in you want to do 2 bit math the lower two bits of the operands so a&3 + b&3 and the third bit is the carry out for that operation d&4. now compare the carry in and the carry out. for V to be set it is either 10xx or 01xx I knew for some reason it cant be 10xx so only tested for 01xx. after confirming that the n bit was set c&4. – old_timer Nov 15 '19 at 21:15
  • this is all really simple if you do it with three or four digit numbers in binary with pencil and paper, then from that you can write a simple program to automate all the combinations of inputs if you are searching for something specific (N = V = 1 for example). I posted mine, you should go through the exercise to make your own. if you want to make it subtract base then think about how you want to do that and make it match the logic c = a + ((~b)&7) + 1; that kind of thing – old_timer Nov 15 '19 at 21:16