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.

What means the instruction "goto $-1 " ?

Status
Not open for further replies.

Vitico

New Member
Somebody could explain to me what means this instruction?


init
bsf status,rp0 ;Change to Bank1
bcf status,rp1 ;rp0 = 1 , rp1 = 0
clrf trisa ;Set PORTA all OUT
movlw b'00100111' ;RB 0,1,2,5=IN RB 3,4,6,7,=OUT
movwf trisb ;Set PORTB
movlw b'10000000' ;RBPU=1 Pull up not use
movwf option_reg ;Set OPTION_REG
bcf status,rp0 ;Change to Bank0
movlw 0x07
movwf cmcon ;disable comparators
clrf mode ;Set mode = stop
clrf count1 ;Clear counter
clrf count2 ;Clear counter
movlw b'00000101' ;Set PORTA initial value
movwf porta ;Write PORTA
bsf portb,rb7 ;Set RB7 = 1
btfsc portb,rb5 ;RB5 = 0 ?
goto $-1 ;No. Wait
 
Code:
btfsc portb,rb5 ;RB5 = 0 ?
goto $-1 ;No. Wait
If RB5 is 0, skip the next instruction.
If it is 1, go back to btfsc portb,rb5
It means go to the previous instruction.
 
$ is the mneumonic for the program cursor. So, as was just pointed out, $-1 would be the previous instruction. And $+1 would go to the next instruction...which, by the way can be useful since gotos take up 2 instruction cycles.

Mike
 
Other advantage is using this “ GOTO $ -X " is you don’t need to add an extra label.

It means for ex:-

Code:
LEDON	bsf	PORTB,0
	call	DELAY
	bcf	PORTB,0
	call	DELAY	
	goto	LEDON

New code comes like this with no labels.

Code:
	bsf	PORTB,0
	call	DELAY
	bcf	PORTB,0
	call	DELAY	
	goto	$-4
 
Disadvantages

The disadvantages are:

Anybody reading that piece of code could have a hard time in understanding what you try to get.

A label is helpful, "goto $" and all variants, not at all.

If for any reason that part of the code get one or more lines added, the - XX / + XX applied to $, will be not correct anymore and the software will not work as expected.

I personally have confined its use to macros to be maintained unchanged along the time, avoiding above risks.

BTW, saving labels is a doutbful benefit. Labels do not increase program length.

Even more, while not common, informative labels could help to clarify the code. I use them and when revisiting my code at a later moment I am grateful to myself for using them.
 
Last edited:
atferrari said:
The disadvantages are:

Anybody reading that piece of code could have a hard time in understanding what you try to get.

A label is helpful, "goto $" and all variants, not at all.

If for any reason that part of the code get one or more lines added, the - XX / + XX applied to $, will be not correct anymore and the software will not work as expected.

I personally have confined its use to macros to be maintained unchanged along the time, avoiding above risks.

BTW, saving labels is a doutbful benefit. Labels do not increase program length.

No they don't, but simple little loops like the one in question don't really require a label, it's really far too simple, and the meaning is obvious (once you know about it, or have read the MPASM help file which explains it).
 
Flexibility is the key:

atferrari said:
The disadvantages are:
Anybody reading that piece of code could have a hard time in understanding what you try to get.
First, I have a hard time understanding your post!

A label is helpful, "goto $" and all variants, not at all.
Yes, labels are helpful, you will get no negation from any programmer on earth. These post are not about the benefits of labels.

If for any reason that part of the code get one or more lines added, the - XX / + XX applied to $, will be not correct anymore and the software will not work as expected.
If lines are added or removed, then change the number of (+) or (-) of course. Person writting the code is fully aware of this. Just like you must recompile code in order for the change to exist, its fully understood. What if you mistype the wrong label name? Are you going to correct this error like any other, or are you going to get rid of labels or not use them just to cover this possibility?

I personally have confined its use to macros to be maintained unchanged along the time, avoiding above risks.
??

BTW, saving labels is a doutbful benefit. Labels do not increase program length.


The GOTO $ is best used for tight loops, creating short delay routines, or small jumps withing +-1 to +-5. Code is easier to read when not burdened with labels that have no functional value. Too many labels in close proximity is a nightmare and can do more harm than good. Its also ugly.


Code:
;-----GOOD-----
USEC_10:
	GOTO	$ + 1
	GOTO	$ + 1
	GOTO	$ + 1
	RETURN

;-----WASTEFUL------
USEC_10:
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	RETURN

;-----STUPID-----
USEC_10:
	 GOTO 	USEC_10A
USEC_10A GOTO 	USEC_10B
USEC_10B GOTO 	USEC_10C
USEC_10C RETURN




;-----GOOD-----
REALFUNCTION:
	CLRF 	PORTB
	CLRF 	PORTC
	MOVLW	0X01
	
	BTFSS	PORTA, .0
	GOTO	$ - 1

	BTFSS	PORTA, .1
	GOTO	$ - 1

	BTFSS	PORTA, .2
	GOTO	$ - 1

	BTFSS	PORTA, .3
	GOTO	$ - 1
	
	GOTO	REALFUNCTION


;-----STUPID-----
REALFUNCTION:
	CLRF 	PORTB
	CLRF 	PORTC
	MOVLW	0X01
	
WASTE0	BTFSS	PORTA, .0
	GOTO	WASTE0

WASTE1	BTFSS	PORTA, .1
	GOTO	WASTE1

WASTE2	BTFSS	PORTA, .2
	GOTO	WASTE2

WASTE4	BTFSS	PORTA, .3
	GOTO	WASTE4

	GOTO	REALFUNCTION



;-----GOOD-----
EEWRITE2:
	BCF	PIR1, EEIF
	BANK1				;REQUIRED WRITE CONTROL.
	BSF 	EECON1, WREN		;ENABLE EEPROM WRITE.
	BCF	INTCON, GIE
	MOVLW 	0X55 
	MOVWF 	EECON2
	MOVLW 	0XAA 
	MOVWF 	EECON2
	BSF 	EECON1, WR		;WRITE DATA
	BANK0
	;-------------------------------------------------------------
	BTFSS	PIR1, EEIF		;EXIT AFTER WRITE COMPLETION
	GOTO 	$ - 1
	BCF	PIR1, EEIF
	;-------------------------------------------------------------

	BANK1				;DISABLE EEPROM WRITE.
	BCF 	EECON1, WREN
	BANK0
	RETURN

;-----STUPID SEE"OBVIOUS"-----
EEWRITE2:
	BCF	PIR1, EEIF
	BANK1				;REQUIRED WRITE CONTROL.
	BSF 	EECON1, WREN		;ENABLE EEPROM WRITE.
	BCF	INTCON, GIE
	MOVLW 	0X55 
	MOVWF 	EECON2
	MOVLW 	0XAA 
	MOVWF 	EECON2
	BSF 	EECON1, WR		;WRITE DATA
	BANK0
	;-------------------------------------------------------------
OBVIOUS	BTFSS	PIR1, EEIF		;EXIT AFTER WRITE COMPLETION
	GOTO 	OBVIOUS
	BCF	PIR1, EEIF
	;-------------------------------------------------------------

	BANK1				;DISABLE EEPROM WRITE.
	BCF 	EECON1, WREN
	BANK0
	RETURN
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top