130 likes | 245 Vues
This guide explores two essential methods for looping in PIC microcontrollers: DECFSZ and BNZ. It illustrates how to create loops to efficiently execute repetitive tasks using example code that adds 3 to a register ten times and stores the result in PORTB. Key concepts such as using COUNTER registers and handling potential maximum iterations are also discussed. By understanding these looping techniques, programmers can optimize their PIC assembly code and enhance execution efficiency.
E N D
PI18…Ch. 3.1 Md. Atiqur Rahman Ahad http://aa.binbd.com
3.1 Branch - Loop • 2 ways to loop in PIC. • DECFSZ decrement fileReg, skip next instruction if 0 DECFSZ fileReg, d • Place GOTO target right below it to create a loop
AGAINX ADDLW 3 | | DECFSZ COUNTX GOTOAGAINX
Do Exa. 3.1 Add 3 to WREG – ten times. Place the result in SFR of PORTB.
COUNTX EQU 0x25 ; Use location 25h for counter COUNTX MOVLW d’10’; WREG=10 [d=decimal] for counter MOVWF COUNTX; count, COUNTX = 10 MOVLW 0 ;clear WREG to 0. WREG = 0 now AGAINX ADDLW 3 ;sum, WREG = 3, then added up… DECFSZ COUNTX, F ;F, not W. If ‘W’, value to WREG GOTOAGAINX;repeat – until COUNTX becomes 0 MOVWF PORTB ;send sum/result to PORTB SFR
2 ways to loop in PIC. • DECFSZ • BNZ - branch if not zero - from PIC18… PIC16 has no BNZ
AGAINX ADDLW 3 ;sum, WREG = 3, then added up… DECFSZ COUNTX, F ;F, not W. If ‘W’, value to WREG GOTOAGAINX;repeat – until COUNTX becomes 0 AGAINX ADDLW 3;sum, WREG = 3, then added up… DECF COUNTX, F ;decrement counter ; decrement fileReg. Z=1 [zero flag] if fileReg = 0 BNZAGAINX;branch/jump to AGAINX if Z=0
Q. What is the max. number of times that the loop in Exa. 3.1/3.2 can be repeated? Location COUNTX in fileRegis an 8-bit register. • It can hold max. of FFh / 1111 1111b / 255 decimal • So, it can be repeated max. of 255 times.
Q. How to repeat 255++ times? Think about C prog. for (…){ … for (…){ … } } • Nestedloop of 2/3/… times. • ENDLESS? Time cost or complexity [of ur algorithm] . • O(n2), O(nlogn), O(n3), …
Read other conditional jumps • BC - branch if C=1 • BNC … if no carry, / C=0 • BZ …if Z= 1 • BNZ • Etc. …
All conditional jumps are short jumps • The address of the target must be within 256 bytes of the contents of the program counter (PC). • Read – unconditional long jumps • GOTO • Branch
Function / subroutine • CALL - long call • RECALL - relative call • What is User-definedfunction? [think C prog] • Why do we use User-defined function?
Stack – LIFO? FIFO? • Pop / Push Instructions … … CALL UserDefinedFunction;call of a subroutine Instructions … ------------------------------------------------------------- UserDefinedFunctionInstruction ;start of the subroutine Instruction … RETURN ;finishof the subroutine ;return to caller