You are on page 1of 4

Mohamad Hamza HW#2 4/2/2023

ID# 22032027 CCE 320

Question #1:
After executing the following code:
MOVLW FFH
ADDLW 1
The status of the C (carry) and Z (zero) flags depend on the execution result of the
ADDLW instruction.
The ADDLW instruction adds the literal value 1 to the contents of the W register,
and updates the C and Z flags based on the result.
In this case, the W register contains FFH before the addition, and adding 1 to it
results in the value 00H, with a carry out.
Therefore, the C flag is set to 1 (indicating a carry-out), and the Z flag is cleared to
0 (since the result is not zero).
So, the status of the C and Z flags after the execution of the code are:
C = 1/Z = 0
Question #2:
a) MOVLW 45H ADDLW 0C4H:
• The MOVLW 45H instruction loads the value 45H into the WREG register.
• The ADDLW 0C4H instruction adds the literal value C4H to the value in the
WREG register.
• The result is 09H, which is stored back in the WREG register.
• There is no carry from the addition, so the C flag is cleared.
Therefore, the value of the C flag is 0 for this code.
b) MOVLW 00 ADDLW FFH:
• The MOVLW 00 instruction loads the value 00 into the WREG register.
• The ADDLW FFH instruction adds the literal value FFH to the value in the
WREG register.
• The result is FFH, which is stored back in the WREG register.
• There is a carry from the addition, so the C flag is set.
Therefore, the value of the C flag is 1 for this code.
c) MOVLW FFH ADDLW 05H:
• The MOVLW FFH instruction loads the value FFH into the WREG register.
• The ADDLW 05H instruction adds the literal value 05H to the value in the
WREG register.
• The result is 04H, which is stored back in the WREG register.
• There is a carry from the addition, so the C flag is set.
Therefore, the value of the C flag is 1 for this code.
Question #3:
ORG 0x0000 ; start at program memory address 0x0000

MOVLW 55H ; load the value 55H into W register


MOVWF temp ; store the value in a temporary register

MOVLW 5 ; set loop counter to 5


MOVWF counter ; store in a counter register

loop:
ADDWF temp, F ; add the value in temp to itself (in place)
DECFSZ counter, F ; decrement the counter and skip next instruction if zero
GOTO loop ; jump back to the loop label if counter is not zero

; insert other code here, if any

END ; end of program

temp RES 1 ; define a 1-byte temporary register in data memory


counter RES 1 ; define a 1-byte loop counter in data memory
Question #4:
The result of the code is 0xFF, which is stored in the WREG register.
The MOVLW 15H instruction loads the value 15H into the WREG register. The
ADDLW 0EAH instruction then adds the literal value 0EAH to the value in the
WREG register, resulting in the value FFH. The result is stored back in the WREG
register.
1. MOVLW 15H: This instruction loads the value 15H into the WREG register.
The WREG register is a general-purpose register used for holding data
values.
2. ADDLW 0EAH: This instruction adds the literal value 0EAH to the value in
the WREG register. The result is 0xFF, which is the maximum value that can
be stored in an 8-bit register. Since there is no carry from the addition, the
Carry flag (C) is cleared.
3. The result of the addition, 0xFF, is stored back in the WREG register.
So, at the end of the code execution, the WREG register contains the value 0xFF.

You might also like