问题
So i was asked an assembly question from a friend who is also trying to learn and understand the language. He asked:
Given the following register values:
D0: 364B 421E
D1: F3FC 9066
after execution of the instruction:
cmp.w D0,D1
what would be the resulting values in the CCR:
N: Z: V: C:
and whether or not each of the following branches to would occur:
BLO <Label> Yes or No?
BVC <Label> Yes or No?
BGE <Label> Yes or No?
BMI <Label> Yes or No?
BLT <Label> Yes or No?
BCS <Label> Yes or No?
Can someone help me out in understanding it as well that way when i explain it to him, i can deepen my understand of the concept and answer. Thank you.
回答1:
It may help to map the concepts to modern-day terms. The C flag corresponds to unsigned arithmetic and the N and V flags correspond to signed arithmetic. The Z flag is valid for both.
The branch instructions are fully spelled out in table 3-19 of the M68000 Programmer's Reference Manual. Note that there are a couple of typos in the table.
For unsigned arithmetic use the following branches:
> BHI
<= BLS
>= BCC/BHS
< BCS/BLO
For signed arithmetic use the following branches:
>= BGE
< BLT
> BGT
<= BLE
And these work for both:
== BEQ
!= BNE
Finally there's the BVS/BVC pair. The V flag is set when there's overflow, meaning the sign bit of the result doesn't follow from the sign bits of the inputs. Subtracting a negative number from a positive number should always result in a positive number for example.
回答2:
with the ccr flags most are self explanitory except overflow and carry but, according to this http://teaching.idallen.com/dat2343/10f/notes/040_overflow.txt you should be able to figure them out
Carry Flag
The rules for turning on the carry flag in binary/integer math are two:
The carry flag is set if the addition of two numbers causes a carry out of the most significant (leftmost) bits added.
1111 + 0001 = 0000 (carry flag is turned on)
The carry (borrow) flag is also set if the subtraction of two numbers requires a borrow into the most significant (leftmost) bits subtracted.
0000 - 0001 = 1111 (carry flag is turned on)
Otherwise, the carry flag is turned off (zero). * 0111 + 0001 = 1000 (carry flag is turned off [zero]) * 1000 - 0001 = 0111 (carry flag is turned off [zero])
In unsigned arithmetic, watch the carry flag to detect errors. In signed arithmetic, the carry flag tells you nothing interesting.
Overflow Flag
The rules for turning on the overflow flag in binary/integer math are two:
If the sum of two numbers with the sign bits off yields a result number with the sign bit on, the "overflow" flag is turned on.
0100 + 0100 = 1000 (overflow flag is turned on)
If the sum of two numbers with the sign bits on yields a result number with the sign bit off, the "overflow" flag is turned on.
1000 + 1000 = 0000 (overflow flag is turned on)
Otherwise, the overflow flag is turned off. * 0100 + 0001 = 0101 (overflow flag is turned off) * 0110 + 1001 = 1111 (overflow flag is turned off) * 1000 + 0001 = 1001 (overflow flag is turned off) * 1100 + 1100 = 1000 (overflow flag is turned off)
Note that you only need to look at the sign bits (leftmost) of the three numbers to decide if the overflow flag is turned on or off.
If you are doing two's complement (signed) arithmetic, overflow flag on means the answer is wrong - you added two positive numbers and got a negative, or you added two negative numbers and got a positive.
If you are doing unsigned arithmetic, the overflow flag means nothing and should be ignored.
来源:https://stackoverflow.com/questions/13615568/ccr-on-the-68000