Search code examples
assemblyflagsstatusmicrochip

When is the zero flag set?


I'm doing some studying for my exam in computer engineering in mid august. Some of the exercises are about counting the working register and status register for certain instructions in assembler. I'm struggling to understand how the status register and in particular the zero flag works in this exercise. The exercise is about calculating what the value of WREG is after these 11 instructions. I have written my answers on every line but somehow I don't get the status register and zero flag right:

  1. CLRF WREG; WREG = 0x00
  2. BSF STATUS,C; Carry flag = 1
  3. BSF WREG,7; WREG = 0x80 (1000 0000) 7th bit set to 1.
  4. RLCF WREG; W = 0x01 and status register 0x03?
  5. BC MCARRY; We will branch because C is set.
  6. ADDLW 0x01; Therefore this line is skipped.
  7. MCARRY DECF WREG; W = 0x00
  8. MOVLW OxFF; W = 0xFF, status register is somehow 7 here
  9. BZ MHALT; Z bit is set since line 4.
  10. INCF WREG; Therefore this line i skipped

11.MHALT GOTO MHALT;

WREG = 0xFF which is the right answer, but I don't understand what happens with the status register at line 4. Somehow it rotates together with WREG and the value gets put in the status register. And then later the value of the status register is 7 (line 8), which I am so confused about. Do any of u have a good explanation that can make me understand what happens with the status register and in particular the zero flag when executing instructions in assembler?

END


Solution

  • It's unclear what you have problem with. First you mention line 4. As you have correctly commented there, the MSB of WREG is 1, and that gets rotated into C. The original value of C which is also 1 gets rotated into the LSB of WREG. Thus WREG=0x01, C=1 and Z=0. Line 7 will set WREG=0x00, C=1 and Z=1. The MOVW does not affect flags so line 9 will use the value from line 7 not line 4.

    Note that the status register is 0 0 0 N OV Z DC C.