Coen317 Assignment2 Sol
Coen317 Assignment2 Sol
Assignment 2
Microprocessor-based Systems – Assignment 2
Answer 1:
Carry flag will be set when there is a carry after an arithmetic instruction such as
addition. Assume R1 = AB00 0000 and R2 = A200 0000. Then ADDS R1, R2
results in a carry, therefore C flag will be set to 1.
This carry is also an unsigned overflow as the result exceeds the range for 32-bit
unsigned values (see next page about unsigned overflow).
If these numbers are signed numbers, the result indicates a signed overflow as it
also exceeds the range for signed values (see next page about signed overflow).
Therefore, V flag will be set to 1 too.
Microprocessor-based Systems – Assignment 2
Solution 1 (continue):
There are two types of overflow: unsigned overflow and signed overflow.
Unsigned overflow:
Let’s say R0 = 0xFFFFFFFF and R1 = 0x00000001. If we perform an addition
instruction, the 32-bit answer comes out to be 0x00000000 with carry, i.e., 1
comes out of the most significant bit (MSB). If these numbers are assumed to be
unsigned, then our expected answer is:
4,294,967,295 + 1 = 4,294,967,296.
Signed overflow:
Let’s say R0 = 0x7FFFFFFF and R1 = 0x7FFFFFFF and both are signed. By
performing an addition instruction, the answer will be 0xFFFF FFFE. However, the
real answer is out of the range of signed numbers and we say that a signed
overflow has occurred. It will be indicated by V = 1.
Microprocessor-based Systems – Assignment 2
2. Write the equivalent assembly instructions for each of the following instructions
written in C programming language, without using explicit multiplication and division.
Assume 𝑥, 𝑦, and 𝑧 are R1, R2, and R3 registers, respectively. (15 points)
a) y = z + x/64;
b) z = 8 * z + 𝑦 ∗ 𝑥;
z
c) z = 2 − y + 128x;
Answer 2:
a) ADD R2, R3, R1, ASR #6
R1 = A000 D4BA H
R2 = B001 B1C2 H
R3 = 7FFF FFFE H
R4 = 8000 0002 H
N = 0, Z = 0, C = 1, V = 0, Q = 0
Indicate the content of registers and flags after executing each one of the following
instructions: (9 x 3pts = 27 points)
a) ADD R1, R2
b) ADD R3, R1, R2
c) ADDS R1, R2
d) ADDS R3, R4, R3
e) ADCS R3, R1, R2
f) ADDEQ R3, R4
g) SUBS R2, R4, R3
h) BIC R1, R2, R3
i) ORN R2, R1, R3
Note: Examine each instruction independent of the previous instructions.
Microprocessor-based Systems – Assignment 2
Answer 3:
a) ADD R1, R2: As the instruction does not have the optional S, the flags are not
updated if there is any.
R1 R2 R3 R4 N Z C V Q
5002 867C B001 B1C2 7FFF FFFE 8000 0002 0 0 1 0 0
b) ADD R3, R1, R2: As the instruction does not have the optional S, the flags are
not updated if there is any.
R1 R2 R3 R4 N Z C V Q
A000 D4BA B001 B1C2 5002 867C 8000 0002 0 0 1 0 0
c) ADDS R1, R2: The result is placed in R1, and flags are updated. R1 and R2
are negative, the result is positive, which is wrong -> a signed overflow occurs
R1 R2 R3 R4 N Z C V Q
5002 867C B001 B1C2 7FFF FFFE 8000 0002 0 0 1 1 0
Microprocessor-based Systems – Assignment 2
Solution 3 (continue):
d) ADDS R3, R4, R3: The flags are updated.
R1 R2 R3 R4 N Z C V Q
A000 D4BA B001 B1C2 0000 0000 8000 0002 0 1 1 0 0
e) ADCS R3, R1, R2: This one is an addition with carry. Since carry bit is 1, the
result of R1, and R2 addition will be incremented by 1.
R1 R2 R3 R4 N Z C V Q
A000 D4BA B001 B1C2 5002 867D 8000 0002 0 0 1 1 0
f) ADDEQ R3, R4: This one is a conditional instruction. EQ means if zero flag is
equal to 1 (i.e. Z = 1), then execute the instruction. Since before this
instruction execution Z = 0, therefore the instruction will not be executed, and
everything will remain unchanged.
R1 R2 R3 R4 N Z C V Q
A000 D4BA B001 B1C2 7FFF FFFE 8000 0002 0 0 1 0 0
Microprocessor-based Systems – Assignment 2
g) SUBS R2, R4, R3: This is a subtraction with S condition. The result of R4
and R3 subtraction (R4 – R3) equals R4 + (2’s complement of R3). Therefore,
C flag will be 1, and V will be 1 because there will be a signed overflow.
R1 R2 R3 R4 N Z C V Q
A000 D4BA 0000 0004 7FFF FFFE 8000 0002 0 0 1 1 0
h) BIC R1, R2, R3: This is AND logic of R2 with 1’s complement of R3 with no S.
Therefore, no flags will be updated. C was originally 1 and stays 1.
1’s complement of R3 = 8000 0001 H.
R1 R2 R3 R4 N Z C V Q
8000 0000 B001 B1C2 7FFF FFFE 8000 0002 0 0 1 0 0
i) ORN R2, R1, R3: This is OR logic of R1 with 1’s complement of R3 with no S.
Therefore, no flags will be updated. C was originally 1 and stays 1.
1’s complement of R3 = 8000 0001 H.
R1 R2 R3 R4 N Z C V Q
A000 D4BA A000 D4BB 7FFF FFFE 8000 0002 0 0 1 0 0
Microprocessor-based Systems – Assignment 2
4. Assume that a byte-addressable memory has the content 0x98 in each of its
cells. If R0 = 0x00000000, R1 = 0x1F348796, R2 = 0x00000012, indicate the
memory address to be accessed and the contents of R0, R1, and R2 after the
execution of the following instructions independently: (3 x 6pts = 18 points)
Answer 4:
Solution 4 (continue):
R1 + shifted R2 =
1F34 8796
0000 0480 +
--------------
1F34 8C16
Memory addresses accessed: 0x1F34 8C16
0x1F34 8C17
0x1F34 8C18
0x1F34 8C19
R0 = 0x9898 9898
R1 = 0x1F34 8796
R2 = 0x0000 0012
Solution 4 (continue) :
Memory addresses accessed: 0x1F34 879D
0x1F34 879E
0x1F34 879F
0x1F34 87A0
R0 = 0x9898 9898
R1 = 0x1F34 879D
R2 = 0x0000 0012
R0 = 0x9898 9898
R1 = 0x1F34 879E
R2 = 0x0000 0012
5. Repeat Q4 if the three instructions are executed consecutively (not
independently) in the same order they appear. (21 points)
Answer 5:
Answer 6:
The program is known as the delay loop:
a) 110016 = 212 + 28 = 4352 times
b) 99 = 6316 → 0x1100 must be replaced with 0x63
c) DLY: SUBS R2, R2, #0x2C
and replace NOP with ADDSMI R2, R2, #0x4