Md. Atiqur Rahman Ahad http://aa.binbd.com PIC18… 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 GOTO AGAINX
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 GOTO AGAINX ;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… 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 GOTO AGAINX ;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 BNZ AGAINX ;branch/jump to AGAINX if Z=0
Q. Implement in Assembly lang. - for(i=0; i<=10; i++) - for(i=100; i>=0; i--)
Q. What is the max. number of times that the loop in Exa. 3. 1/3 Q. What is the max. number of times that the loop in Exa. 3.1/3.2 can be repeated? Location COUNTX in fileReg is an 8-bit register. It can hold max. of FFh / 1111 1111b / 255 decimal So, it can be repeated max. of 255 times. Why not 256??
Q. How to repeat 255++ times? Think about C prog. for (…){ … } Nested loop 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-defined function? [think C prog] Why do we use User-defined function?
CALL UserDefinedFunction ;call of a subroutine Stack – LIFO? FIFO? Pop / Push Instructions … … CALL UserDefinedFunction ;call of a subroutine ------------------------------------------------------------- UserDefinedFunction Instruction ;start of the subroutine Instruction RETURN ;finish of the subroutine ;return to caller