Part 2. In testing the relationship M < N, a CPU would compute M – N (using two’
ID: 3844104 • Letter: P
Question
Part 2. In testing the relationship M < N, a CPU would compute M – N (using two’s complement arithmetic) and then test the resulting condition flags. Given 8-bit numbers: N = 11001100 M = 10001000 1. Determine the values of the four condition codes (N,C,V,Z) for M – N. 2. For each of the following four cases, from the above condition codes, determine which block of code would be executed. Explain your result. Case 1: Assuming N and M are signed 8-bit numbers. if (M < N) { Block1 } else { Block2 } Case 2: Same as Case 1, but assuming that N and M are unsigned 8-bit numbers. Case 3: Assuming N and M are signed 8-bit numbers. if (M >=N) { Block1 } else { Block2 } Case 4: Same as Case 2, but assuming that N and M are unsigned 8-bit numbers
Part 2 In testing the relationship MExplanation / Answer
1)
Given:
M = 11001100 = (204) in decimal
N = 10001000 = (136) in decimal
2's complement of N = 1's complement of N + 1
1's complement of N = 01110111
+ 1 = 1
--------
01111000
--------
M = 11001100
2's complement of N = 01111000
--------
101000100
--------
If the register size is only 8 bits, then the answer gets truncated to 01000100 = 68 in decimal.
a) N Flag = Set to 0. Since the answer is positive.
b) C Flag = Some data is lost - (i.e. 9th bit from LSB). This flag is set to 1.
c) V Flag = From the arithmetic view point, 204 - 136 = 68 does not result in overflow.
This flag is set to 0.
d) Z Flag = Since the answer is non-zero value, this flag is set to 0.
2)
Comparison is similar to Subtract operation except that the results are not stored anywhere.
However the flags will be set as in the Subtract operation.
Case 1:
Here M-N operation happens and C flag will be set.
and Block 2 will get executed.
Case 2:
Same as Case 1
Case 3:
Block 1 will get executed.
M - N will result in C flag being set and Z flag not being set.
Z flag is important as the "if" condition compares both "greater than" and "equal".
Case 4:
Same as Case 3.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.