PIC 18 Ch 3 1 Md Atiqur Rahman

  • Slides: 14
Download presentation
PIC 18… Ch. 3. 1 Md. Atiqur Rahman Ahad http: //aa. binbd. com

PIC 18… Ch. 3. 1 Md. Atiqur Rahman Ahad http: //aa. binbd. com

3. 1 Branch - Loop • 2 ways to loop in PIC. 1. DECFSZ

3. 1 Branch - Loop • 2 ways to loop in PIC. 1. DECFSZ decrement file. Reg, skip next instruction if 0 DECFSZ file. Reg, d - Place GOTO target right below it to create a loop

AGAINX ADDLW 3 COUNTX GOTO AGAINX | | DECFSZ

AGAINX ADDLW 3 COUNTX GOTO AGAINX | | DECFSZ

Do Exa. 3. 1 Add 3 to WREG – ten times. Place the result

Do Exa. 3. 1 Add 3 to WREG – ten times. Place the result in SFR of PORTB.

COUNTX AGAINX EQU MOVLW MOVWF MOVLW ADDLW DECFSZ GOTO MOVWF 0 x 25 ;

COUNTX AGAINX EQU MOVLW MOVWF MOVLW ADDLW DECFSZ GOTO MOVWF 0 x 25 ; Use location 25 h for counter COUNTX d’ 10’ ; WREG=10 [d=decimal] for counter COUNTX ; count, COUNTX = 10 0 ; clear WREG to 0. WREG = 0 now 3 ; sum, WREG = 3, then added up… COUNTX, F ; F, not W. If ‘W’, value to WREG AGAINX ; repeat – until COUNTX becomes 0 PORTB ; send sum/result to PORTB SFR

 • 2 ways to loop in PIC. 1. DECFSZ 2. BNZ - branch

• 2 ways to loop in PIC. 1. DECFSZ 2. BNZ - branch if not zero - from PIC 18… PIC 16 has no BNZ

COUNTX EQU 0 x 25 MOVLW MOVWF MOVLW ; Use location 25 h for

COUNTX EQU 0 x 25 MOVLW MOVWF MOVLW ; Use location 25 h for counter COUNTX d’ 10’ ; WREG=10 [d=decimal] for counter COUNTX ; count, COUNTX = 10 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 DECF 3 ; sum, WREG = 3, then added up… COUNTX, F ; decrement counter ; decrement file. Reg. Z=1 [zero flag] if file. Reg = 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. 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.

Q. What is the max. number of times that the loop in Exa. 3. 1/3. 2 can be repeated? Location COUNTX in file. Reg is an 8 -bit register. It can hold max. of FFh / 1111 b / 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 (…){ } …

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(n 2), O(nlogn), O(n 3), …

Read other conditional jumps – BC – BNC – BZ – BNZ – Etc.

Read other conditional jumps – BC – BNC – BZ – BNZ – Etc. - branch if C=1 … if no carry, / C=0 … if Z= 1 …

 • All conditional jumps are short jumps • The address of the target

• 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 –

Function / subroutine • CALL - long call • RECALL - relative call – What is User-defined function? [think C prog] – Why do we use User-defined function?

 • Stack – LIFO? FIFO? • Pop / Push Instructions … … CALL

• Stack – LIFO? FIFO? • Pop / Push Instructions … … CALL User. Defined. Function ; call of a subroutine Instructions … ------------------------------User. Defined. Function Instruction ; start of the subroutine Instruction … RETURN ; finish of the subroutine ; return to caller