Reputation: 72
I'm studying assembly language. I understand that CMP R1 R2
operation sets flag bits(Carry, Overflow, Zero, Sign etc..)according to the result of R1-R2.
And I understand that conditional jump instructions JXs such as JA, JBE follows after CMP. If flag bit condition is matched, JX instruction make IP jump to specified address.
What I never undetstand is "Tested Conditions" in the picture I attached.
CMP R1 R2
JAE somewhere
Above code obviously jump to somewhere if R1 is bigger or equal to R2. If R1=0111 and R2=0110, a JAE jump to somewhere. In this case,
R1-R2 = 0111-0110 = 0111+1010 = 10001 = 0001 with carry bit set
note that I added 2's complement of 0110 instead of subtracting 0110 because microcontrollers calculate in this way as I know
But textbook says that the JAE will jump if Carry flag is 0. My calculation show that C=1 if R1 is bigger than R2. Another examples show that C=1 if R1 is bigger than R2. There's no issue with sign.
So what's wrong with "tested conditions"?
Upvotes: 1
Views: 6451
Reputation: 71586
This subtraction
R1-R2 = 0111-0110
gets implemented like this in logic
1
0111
+ 1001
========
finish the math
11111
0111
+ 1001
========
0001
so carry out (unsigned overflow) is a 1, signed overflow is a 0 as the carry in and carry out to the msbit match can also determine this from the msbits of the operands and the result. if the msbits of the operands match each other but the msbit of the result doesnt match the msbits then signed overflow.
not zero so a z flag would be 0, and the msbit is not set so an n flag would not be set.
the next question is does this architecture invert carry out into the carry flag on a subtract making it a borrow or do they take it straight across?
In any case you have your four basic flags, carry, signed overflow, negative and zero. With good documentation you get a list of flags for the condition. You kind of know in your head if you want a greater than or less than or whatever, this little pencil and paper test, along with doing it on the processor and dumping the flags to see if this architecture inverts the carry out, also reading the docs to see if all the flags are touched by the test instruction in question, then looking at the various tested conditions to see which ones match your result.
Upvotes: 1