Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Status, Z - Assembly code

Status
Not open for further replies.

Spadez

New Member
Hi,

Ive been shown this example code (very basic and not necessarily well coded)

Code:
movlw    0x0A
movwf    i
movlw    b'00000110'
movwf   k

incf       i

movlw    2
addwf     i,w
movwf    k

movf      i,w
addlw     2
movwf    j

movf    i,w
subwf   k,w
btfsc   status,Z
incf      i
goto     $
end

I understand it all upto the last block. I dont fully understand the status register and Z. The teacher spoke about something like it was checking for a zero, or trailing zero. Could someone clarify this for me please.

James
 
Z = zero
btfsc is bit test and skip if clear (not zero)

Run the code through MPLABs SIM and you can watch it all happening. Very useful tool, learn how to use it.

Goto $ = loop forever. It won't translate to the 18F or higher PICs so better to use a label.
 
Last edited:
Im a bit confused about the "status" register bit. Why is checking "status", I thought that was only for selecting different banks on the PIC.

Secondly, is there a way I can step through this code as it is? I thought my teacher did it, but it looks like I will have to add all the additional code like headers etc to be able to quickbuild it and then step into it.

Finally, is this right (from what I get without compiling because im trying to add header etc)

movf i,w ; Move 11 (i) to working
subwf k,w ; W - K ; 11 - 13
btfsc status,Z ; Is it zero?; No ; Dont skip
incf i ; Increment i by 1
goto $ ; Go to this line ; loop

This also brings up another question. If you have a command to move file i to the working register, then in the next line you increment i, is it correct that the working register wouldnt have incremented. If so, I dont see how this code could ever work without running this again:

movf i,w ; Move i to working
 
Last edited:
Have a look at the PIC dtatsheet and look at the detailed description of the STATUS register.

Bit 2 is the Z or Zero bit, which is set to "1" when the result of an arithmetic or logical operation is zero

So, the BTFSC STATUS Z instruction checks the Z bit in the STATUS register, and skips over the next instruction if the Z bit is clear (ie the Z bit is a"0").

JimB
 
Truethy i havent been able to simulate it. Is it nessesary to add the headers etc to this code just to simulate it?

This is my attempt at making it into a complete program which I can quickbuild and set into:
LIST P=16F628A

INCLUDE P16F628A.INC

__CONFIG _CP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_LVP_OFF&_MCLRE_OFF&_INTRC_OSC_NOCLKOUT
; PWRTE_ON enables power up timer delay ~72ms which should help screen to self initialise





;*******************************************************************************************

ORG 0x00 ;Reset vector

goto START_OF_PROG

;*******************************************************************************************



START_OF_PROG

movlw 0x0A
movwf i
movlw b'00000110'
movwf k

incf i

movlw 2
addwf i,w
movwf k

movf i,w
addlw 2
movwf j

movf i,w
subwf k,w
btfsc status,Z
incf i
goto $

FINISH
goto FINISH

END

It doesnt compile but its getting late, im not really thinking straight. Im guessing because I need to equate my relevant code to relevant registers.

-------------------------

In regards to the Zero bit. So, if the code was this instead:

movf i,w ; Move 13 (i) to working
subwf k,w ; W - K ; 13 - 13
btfsc status,Z ; Is it zero?; Yes ; 1
incf i ; Increment i by 1
goto $ ; Go to this line ; loop

Then 13-13 is equal to 0. Therefore Z will be 1, and it wont skip, and will increment by 1 and then get stuck in the end loop. Is this right? Ill read the datasheet for a detailed status explanation first thing in the morning.
 
Last edited:
Yes but they didnt come up on here for some reason. My compiler says:

----------------------------------------------------------------------
Release build of project `C:\Users\James\Desktop\test.disposable_mcp' started.
Language tool versions: MPASMWIN.exe v5.37, mplink.exe v4.37, mplib.exe v4.37
Thu Nov 04 00:22:24 2010
----------------------------------------------------------------------
Clean: Deleting intermediary and output files.
Clean: Done.
Executing: "D:\Program Files\Microchip\MPASM Suite\MPASMWIN.exe" /q /p16F628A "test.asm" /l"test.lst" /e"test.err"
Warning[205] C:\USERS\JAMES\DESKTOP\TEST.ASM 2 : Found directive in column 1. (LIST)
Warning[205] C:\USERS\JAMES\DESKTOP\TEST.ASM 4 : Found directive in column 1. (INCLUDE)
Warning[205] C:\USERS\JAMES\DESKTOP\TEST.ASM 6 : Found directive in column 1. (__CONFIG)
Error[113] C:\USERS\JAMES\DESKTOP\TEST.ASM 6 : Symbol not previously defined (_MC)
Error[113] C:\USERS\JAMES\DESKTOP\TEST.ASM 6 : Symbol not previously defined (LRE_OFF)
Error[112] C:\USERS\JAMES\DESKTOP\TEST.ASM 6 : Missing operator
Error[113] C:\USERS\JAMES\DESKTOP\TEST.ASM 26 : Symbol not previously defined (i)
Error[113] C:\USERS\JAMES\DESKTOP\TEST.ASM 28 : Symbol not previously defined (k)
Message[305] C:\USERS\JAMES\DESKTOP\TEST.ASM 30 : Using default destination of 1 (file).
Error[113] C:\USERS\JAMES\DESKTOP\TEST.ASM 30 : Symbol not previously defined (i)
Error[113] C:\USERS\JAMES\DESKTOP\TEST.ASM 33 : Symbol not previously defined (i)
Error[113] C:\USERS\JAMES\DESKTOP\TEST.ASM 34 : Symbol not previously defined (k)
Error[113] C:\USERS\JAMES\DESKTOP\TEST.ASM 36 : Symbol not previously defined (i)
Error[113] C:\USERS\JAMES\DESKTOP\TEST.ASM 38 : Symbol not previously defined (j)
Error[113] C:\USERS\JAMES\DESKTOP\TEST.ASM 40 : Symbol not previously defined (i)
Error[113] C:\USERS\JAMES\DESKTOP\TEST.ASM 41 : Symbol not previously defined (k)
Error[113] C:\USERS\JAMES\DESKTOP\TEST.ASM 42 : Symbol not previously defined (status)
Message[305] C:\USERS\JAMES\DESKTOP\TEST.ASM 43 : Using default destination of 1 (file).
Error[113] C:\USERS\JAMES\DESKTOP\TEST.ASM 43 : Symbol not previously defined (i)
Halting build on first failure as requested.
----------------------------------------------------------------------
Release build of project `C:\Users\James\Desktop\test.disposable_mcp' failed.
Language tool versions: MPASMWIN.exe v5.37, mplink.exe v4.37, mplib.exe v4.37
Thu Nov 04 00:22:25 2010
----------------------------------------------------------------------
BUILD FAILED

I guess I need to define everything. Is there a way I can test this without making it into a full program?
 
Ive been trying to make this into a program so I can see it run. Ive done the equates in the header but im still becoming stuck. Here is my code:


LIST P=16F628A

INCLUDE P16F628A.INC

i EQU 0X20 ;DELAY LOOP
k EQU 0X21 ;DELAY LOOP
j EQU 0X22


;************************************************* ******************************************

ORG 0x00 ;Reset vector

goto START_OF_PROG

;************************************************* ******************************************



START_OF_PROG

movlw 0x0A
movwf i
movlw b'00000110'
movwf k

incf i

movlw 2
addwf i,w
movwf k

movf i,w
addlw 2
movwf j

movf i,w
subwf k,w
btfsc status,Z
incf i
goto $

FINISH
goto FINISH

END

The error is as follows:

Executing: "D:\Program Files\Microchip\MPASM Suite\MPASMWIN.exe" /q /p16F628A "test.asm" /l"test.lst" /e"test.err"
Message[305] C:\USERS\JAMES\DESKTOP\TEST.ASM 30 : Using default destination of 1 (file).
Error[113] C:\USERS\JAMES\DESKTOP\TEST.ASM 42 : Symbol not previously defined (status)
Message[305] C:\USERS\JAMES\DESKTOP\TEST.ASM 43 : Using default destination of 1 (file).
Halting build on first failure as requested.

Can someone help me figure out how to assemble this so I can run through and see the code working
 
Sorry ive been working on this but i still cant compile it, so I cant see how the code runs through. How do I get rid of the "status" error? Status doesnt seem to need to be defined.
 
Try,
Code:
		list	P=16F628A

		INCLUDE	P16F628A.INC 

i		equ	0X20		;DELAY LOOP
k		equ	0X21		;DELAY LOOP
j		equ	0X22


;************************************************* ******************************************  

		org	0x00		;Reset vector

		goto	START_OF_PROG

;************************************************* ******************************************  



START_OF_PROG

		movlw	0x0A
		movwf	i
		movlw	b'00000110'
		movwf	k

		incf	i[COLOR="red"],f[/COLOR]

		movlw	2
		addwf	i,w
		movwf	k

		movf	i,w
		addlw	2
		movwf	j

		movf	i,w
		subwf	k,w
		btfsc	[COLOR="red"]STATUS[/COLOR],Z
		incf	i[COLOR="red"],f[/COLOR]
		goto	$

FINISH
		goto	FINISH

		end

To keep the formatting correct when posting code, type [code] before it and [/code] after.

Mike.
 
Thank you Mike, that fixed allowed it to compile.

Am I right in this now:
Code:
movf	i,w                                    ;move 11 to working
subwf	k,w                                    ;13-11=2
btfsc	STATUS,Z                               ;Result = 1 (set) as 2 is not equal to 0
incf	i,f                                    ;Skip as per above
goto	$                                      ;Loop

If you could tell me if I have this right it would be helpful as if I have it wrong I need to go back to the drawing board on this.
 
Last edited:
Yes, that is correct. The easy way to test it is to run MPSIM and single step through it. If you add a watch window you can see the variables change as well.

Mike.
 
Actually I dont believe that is correct, I did a typo. If the result of btfsc status,z is not zero then the result will be 0 (clear) not set as above.
 
Ok, I see where I went wrong. I have said this:
Code:
btfsc	STATUS,Z                               ;Result =[B] 1[/B] (set) as 2 is not equal to 0

Because actually since it is not zero, the result of Status,Z will be "clear" not set. This means it will skip inc, and will not increment i.
Code:
btfsc	STATUS,Z                               ;Result =[B]0[/B] (set) as 2 is not equal to 0, INC will be skipped
 
Last edited:
Symbols/labels are case sensitive. In the includes file of the assembler, STATUS is defined but status is not. You have to type it out in all caps in order for the assembler to recognize it.

The zero bit in STATUS is a very useful tool for comparing a value to a known constant. Say if you're looking for a specific byte. XOR'ing it with a known constant will set the Z flag in the STATUS register if and only if the byte matches the constant. With btfss and btfsc, you create a branch in the code that allows the chip to perform a different task when the matching byte is XOR'ed with the known constant.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top