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.

strange variable problem, need help

Status
Not open for further replies.

skyrock

New Member
strange variable problem, *solved*

Hello guys,

I encountered a strange problem with variable today. It seems the variable i've declared are not changing even with simplest instruction like decfsz.

The code works and compiled. So, it also loaded some value using movwf too. But when it comes to decfsz, it does change. I wonder if the way i am declaring the variables makes it like this or some setting is wrong?

I'll insert my code so that you guys can help me out here.

regards.
Code:
	list      p=16f628A           ; list directive to define processor
	#include <p16F628A.inc>       ; processor specific variable definitions
	errorlevel  -302              ; suppress message 302 from list file

	__CONFIG   _CP_OFF & _DATA_CP_OFF & _LVP_OFF & _BOREN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _HS_OSC 


cblock 0x20
     ACCaLO, ACCaHI, ACCbLO, ACCbHI
     ACCcLO, ACCcHI, ACCdLO, ACCdHI, Dcount
	
endc

#include <muldiv.h>

;***** VARIABLE DEFINITIONS
w_temp        EQU     0x7E        ; variable used for context saving 
status_temp   EQU     0x7F        ; variable used for context saving




;**********************************************************************
	ORG     0x000             ; processor reset vector
	goto    main              ; go to beginning of program
	

	ORG     0x004             ; interrupt vector location
	movwf   w_temp            ; save off current W register contents
	movf	STATUS,w          ; move status register into W register
	movwf	status_temp       ; save off contents of STATUS register

; isr code can go here or be located as a call subroutine elsewhere

	movf    status_temp,w     ; retrieve copy of STATUS register
	movwf	STATUS            ; restore pre-isr STATUS register contents
	swapf   w_temp,f
	swapf   w_temp,w          ; restore pre-isr W register contents
	retfie                    ; return from interrupt

	#include <muldiv.inc>

main
	movlw	0x02
	movwf	Dividend
	movlw	0x02
	movwf	Divider
	call DIV8

	goto	main		  ;loop forever, remove this instruction, for test only


;**************************************************************
;Dividend 16/16  (this has nothing to do with at the moment)
;**************************************************************
D_divS
	clrf     count
	bsf      count,4         ; set count = 16
	clrf     ACCcHI
	clrf     ACCcLO
	clrf     ACCdLO
	clrf     ACCdHI
;
;  Looped code
;
divLoop
	bcf      STATUS,C
	rlf     ACCbLO
	rlf     ACCbHI
	rlf     ACCcLO
	rlf     ACCcHI
	movfw    ACCaHI
	subwf	ACCcHI,W          ;check if a>c
	btfss	STATUS,Z
	goto	notz
	movfw    ACCaLO
	subwf     ACCcLO,W        ; if msb equal then check lsb
notz
	btfss	STATUS,C    ; carry set if c>a
	goto    nosub           ; if c < a
subca
	movfw    ACCaLO    ; c-a into c
	subwf     ACCcLO, F
	movfw    ACCaHI
	subwf    ACCcHI, F
	bsf      STATUS,C              ;shift a 1 into d (result)
nosub
	rlf     ACCdLO, F
	rlf     ACCdHI, F
	decfsz     Dcount, F
	goto    divLoop


; initialize eeprom locations

	ORG	0x2100
	DE	0x00, 0x01, 0x02, 0x03


	END                       ; directive 'end of program'

muldiv.inc
Code:
;*****************************************************
;multiply 8bit
;*****************************************************
mpy_S 
	clrf H_byte
	clrf L_byte
	movlw 8
	movwf count
	movf mulcnd,W
	bcf STATUS,C ; Clear the carry bit in the status Reg.
loop 
	rrf mulplr, F
	btfsc STATUS,C
	addwf H_byte,Same
	rrf H_byte,Same
	rrf L_byte,Same
	decfsz count, F
	goto loop
	retlw 0


;*****************************************************
;dividend 8bit
;*****************************************************
DIV8	;	movlw	b'00001010'		; load the divider parameter
		;	movwf	Divider			; store it
		;	movlw	b'01100100'		; load the dividend parameter
		;	movwf	Dividend		; store it
                       movlw       0x01
                       movwf       Same
			movlw	8			; load counter parameter
			movwf	Counter			; store it
			clrf	Result			; clear the result
			clrf	Remainder		; clear the remainder
			bcf	StatusReg,CarryFlag	; clear the carry flag
process		rlf	Dividend,Same		; rotate bit into carry
			rlf 	Remainder,Same		; rotate carry into remainder file
			movf	Divider,w		; load the divider
			subwf	Remainder,w		; subtract it from remainder file store in w
			rlf	Result,Same		; rotate carry into result (1) pos (0) neg
			btfsc	Result,0		; check if last calculation was positive
			goto 	calc_remainder		; 
countdown	decfsz	Counter,Same		; coundown 8 bits
			goto 	process			; carry on
			return					; return to call stack
calc_remainder	movwf	Remainder		; store the remainder in Remainder file
			goto	countdown		; carry on

muldiv.h
Code:
cblock
count, prodH, prodL, mulplr,mulcnd,answer,Counter
H_byte
L_byte
Same, Dividend, Divider, StatusReg, CarryFlag, Remainder, Result
endc

i removed the variable "Same" and it worked. strange...
 
Last edited:
Assemblers Assemble ...

Assemblers and compilers are both translators.

Compilers translate high level languages into something else.

Assemblers translate asm into machine code, most often in some hex format.

Assemblers assemble... Compilers compile... Assemblers do not compile !

Thanks for you time. :)

3v0
 
This is VERY suspicious:
countdown decfsz Counter,Same ; coundown 8 bits
How is SAME defined? I see you also use it as a variable:
movlw 0x01
movwf Same
These two uses are NOT compatable. Use F or W in the decfz statements and NOT "Same". F and W are one bit definitions. "Same" is probably defined as the location of an 8 bit variable address.
 
Last edited:
I confirmed the problem came from "Same"

it is originally defined as "Same equ 1"

so, in my code i made it wrongly and defined as variable block, just like kchriste said, it wouldn't work with a memory block for an instruction option.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top