Jump Call Chapter 3 Sepehr Naimi www Nicer

  • Slides: 38
Download presentation
Jump & Call Chapter 3 Sepehr Naimi www. Nicer. Land. com www. Micro. Digital.

Jump & Call Chapter 3 Sepehr Naimi www. Nicer. Land. com www. Micro. Digital. Ed. com

Topics n n n Introduction to jump and call Jump Call n n n

Topics n n n Introduction to jump and call Jump Call n n n Stack Calling a function Time Delay 2

Jump and Call n CPU executes instructions one after another. n For example in

Jump and Call n CPU executes instructions one after another. n For example in the following C program, CPU first executes the instruction of line 3 (adds b and c), then executes the instruction of line 4. 1 void main () 2 { 3 a = b + c; 4 c -= 2; 5 d = a + c; 6 } 3

Jump and Call (Continued) n But sometimes we need the CPU to execute, an

Jump and Call (Continued) n But sometimes we need the CPU to execute, an instruction other than the next instruction. For example: n n n When we use a conditional instruction (if) When we make a loop When we call a function 4

Jump and Call (Continued) n Example 1: Not executing the next instruction, because of

Jump and Call (Continued) n Example 1: Not executing the next instruction, because of condition. n In the following example, the instruction of line 6 is not executed. 1 void main () 2 { 3 int a = 2; 4 int c = 3; 5 if (a == 8) 6 c = 6; 7 else 8 c = 7; 9 c = a + 3; } 5

Jump and Call (Continued) n Example 2: In this example the next instruction will

Jump and Call (Continued) n Example 2: In this example the next instruction will not be executed because of loop. n In the following example, the order of execution is as follows: n n n Line 4 Line 5 Again, line 4 Again line 5 Line 6 1 void main () 2 { 3 int a, c = 0; 4 for(a = 2; a < 4; a++) 5 c += a; 6 7 a = c + 2; } 8 9 6

Jump and Call (Continued) n Example 3: Not executing the next instruction, because of

Jump and Call (Continued) n Example 3: Not executing the next instruction, because of calling a function. n In the following example, the instruction of line 6 is not executed after line 5. Code 1 void func 1 (); 2 void main () 3 { 4 int a = 2, c = 3; 5 func 1 (); 6 c = a + 3; 7 } 8 void func 1 (){ 9 10 int d = 5 / 2; } 11 7

Jump and Call (Continued) n In the assembly language, there are 2 groups of

Jump and Call (Continued) n In the assembly language, there are 2 groups of instructions that make the CPU execute an instruction other than the next instruction. The instructions are: n n Jump: used for making loop and condition Call: used for making function calls 8

Jump n Jump changes the Program Counter (PC) and causes the CPU to execute

Jump n Jump changes the Program Counter (PC) and causes the CPU to execute an instruction other than the next instruction. 9

Jump There are 2 kinds of Jump n Unconditional Jump: When CPU executes an

Jump There are 2 kinds of Jump n Unconditional Jump: When CPU executes an unconditional jump, it jumps unconditionally (without checking any condition) to the target location. n n Example: RJMP and JMP instructions Conditional Jump: When CPU executes a conditional jump, it checks a condition, if the condition is true then it jumps to the target location; otherwise, it executes the next instruction. 10

Unconditional Jump in AVR n n There are 3 unconditional jump instructions in AVR:

Unconditional Jump in AVR n n There are 3 unconditional jump instructions in AVR: RJMP, and IJMP We label the location where we want to jump, using a unique name, followed by ‘: ’ Then, in front of the jump instruction we mention the name of the label. This causes the CPU to jump to the location we have labeled, instead of executing the next instruction. Code 1 LDI R 16, 0 2 LDI R 17, 2 3 L 1: ADD R 16, R 17 4 RJMP L 1 5 SUB R 10, R 15 11

Ways of specifying the jump target n There are 3 ways to provide the

Ways of specifying the jump target n There are 3 ways to provide the jump address: n n n PC = operand PC = PC + operand PC = Z register 12

JMP n JMP PC = operand 1001 010 X XXXX 110 X XXXX XXXX

JMP n JMP PC = operand 1001 010 X XXXX 110 X XXXX XXXX n Example: 1001 0100 0000 1100 0000 0110 n Operand = 0000000000110 13

JMP n n In JMP, the operand, contains the address of the destination When

JMP n n In JMP, the operand, contains the address of the destination When an JMP is executed: n PC is loaded with the operand value PC: 0002 0001 0000 0007 Address Code 0000 . ORG 0 0000 LDI R 16, 15 Machine code: 0001 LDI R 17, 5 940 C 0006 0002 JMP LBL_NAME 0004 LDI R 18, 4 0005 ADD R 18, R 17 op. Code operand 0006 LBL_NAME: Machine code: 0006 ADD R 16, R 17 940 C 0006 0007 JMP LBL_NAME op. Code operand 0009 14

RJMP (Relative jump) n RJMP PC = PC + operand 1100 XXXX n Example:

RJMP (Relative jump) n RJMP PC = PC + operand 1100 XXXX n Example: 1100 0000 0110 n Operand = n PC = PC + 000000000110 15

RJMP n When RJMP is executed: n The operand will be added to the

RJMP n When RJMP is executed: n The operand will be added to the current value of PC PC: 0007 0003 0006 0002 0001 0000 +0 +F 0005 Machine code: 002 C 002 op. Code operand Address 0000 . ORG 0 0000 LDI R 16, 15 0001 LDI R 17, 5 0002 RJMP LBL_NAME 0003 LDI R 18, 4 0004 ADD R 18, R 17 0005 Machine code: Code LBL_NAME: 0005 ADD CFFE 0006 RJMP LBL_NAME op. Code operand 0007 R 16, R 17 16

IJMP (Indirect jump) n IJMP PC = Z register 1001 0100 0000 1001 The

IJMP (Indirect jump) n IJMP PC = Z register 1001 0100 0000 1001 The instruction has no operand. n the Program counter is loaded with the contents of Z register. n For example, if Z points to location 100, by executing IJMP, the CPU jumps to location 100. n 17

Conditional Jump in AVR SREG: n I T H S V N Z C

Conditional Jump in AVR SREG: n I T H S V N Z C The conditional jump instructions in AVR are as follows: Instruction Abbreviation of Comment BREQ lbl Branch if Equal Jump to location lbl if Z = 1, BRNE lbl Branch if Not Equal Jump if Z = 0, to location lbl BRCS lbl BRLO lbl Branch if Carry Set Branch if Lower Jump to location lbl, if C = 1 BRCC lbl BRSH lbl Branch if Carry Cleared Branch if Same or Higher Jump to location lbl, if C = 0 BRMI lbl Branch if Minus Jump to location lbl, if N = 1 BRPL lbl Branch if Plus Jump if N = 0 BRGE lbl Branch if Greater or Equal Jump if S = 0 BRLTlbl Branch if Less Than Jump if S = 1 BRHS lbl Branch if Half Carry Set If H = 1 then jump to lbl BRHC lbl Branch if Half Carry Cleared if H = 0 then jump to lbl BRTS Branch if T flag Set If T = 1 then jump to lbl BRTC Branch if T flag Cleared If T = 0 then jump to lbl BRIS Branch if I flag set If I = 1 then jump to lbl BRIC Branch if I flag cleared If I = 0 then jump to lbl 18

Usages of Conditional jump n n Conditions Loop 19

Usages of Conditional jump n n Conditions Loop 19

Conditions n When b is subtracted from a: n n The result is zero,

Conditions n When b is subtracted from a: n n The result is zero, when a is equal to b Carry will be set when a < b SREG: I T H S V N Z a -b C 20

Example 1 n n Write a program that if R 20 is equal to

Example 1 n n Write a program that if R 20 is equal to R 21 then R 22 increases. Solution: SUB R 20, R 21 BRNE NEXT INC R 22 NEXT: ; Z will be set if R 20 == R 21 ; if Not Equal jump to next 21

Example 2 n n Write a program that if R 26 < R 24

Example 2 n n Write a program that if R 26 < R 24 then R 22 increases. Solution: SUB R 26, R 24 BRCC L 1 INC R 22 ; C will be set when R 26 < R 24 ; if Carry cleared jump to L 1: 22

Example 3 n n Write a program that if R 26 >= R 24

Example 3 n n Write a program that if R 26 >= R 24 then R 22 increases. Solution: SUB R 26, R 24 BRCS L 1 INC R 22 ; C will be cleared when R 26 >= R 24 ; if Carry set jump to L 1: 23

Example 4: IF and ELSE int main ( ) { R 17 = 5;

Example 4: IF and ELSE int main ( ) { R 17 = 5; if (R 20 > R 21) R 22++; else R 22 --; R 17++; } LDI R 17, 5 SUB R 21, R 20 BRCC ELSE_LABEL INC R 22 JMP NEXT ELSE_LABEL: DEC R 22 NEXT: INC R 17 ; C is set when R 20>R 21 ; jump to else if cleared 24

Loop n n Write a program that executes the instruction “ADD R 30, R

Loop n n Write a program that executes the instruction “ADD R 30, R 31” 9 times. Solution: . ORG LDI L 1: ADD DEC BRNE L 2: RJMP 00 R 16, 9 R 30, R 31 R 16 L 1 L 2 ; R 16 = 9 ; R 16 = R 16 - 1 ; if Z = 0 ; Wait here forever 25

Loop n n Write a program that calculates the result of 9+8+7+…+1 Solution: .

Loop n n Write a program that calculates the result of 9+8+7+…+1 Solution: . ORG LDI L 1: ADD DEC BRNE L 2: RJMP 00 R 16, 9 R 17, 0 R 17, R 16 L 1 L 2 ; R 16 = 9 ; R 17 = 0 ; R 17 = R 17 + R 16 ; R 16 = R 16 - 1 ; if Z = 0 ; Wait here forever 26

Loop n n Write a program that calculates the result of 20+19+18+17+…+1 Solution: .

Loop n n Write a program that calculates the result of 20+19+18+17+…+1 Solution: . ORG LDI L 1: ADD DEC BRNE L 2: RJMP 00 R 16, 20 R 17, R 16 L 1 L 2 ; R 16 = 20 ; R 17 = R 17 + R 16 ; R 16 = R 16 - 1 ; if Z = 0 ; Wait here forever 27

Loop for (init; condition; calculation) { do something } init Do something calculation Yes

Loop for (init; condition; calculation) { do something } init Do something calculation Yes Condition No END 28

Loop n n Write a program that calculates 1+3+5+…+27 Solution: LDI R 20, 0

Loop n n Write a program that calculates 1+3+5+…+27 Solution: LDI R 20, 0 LDI R 16, 1 L 1: ADD R 20, R 16 LDI R 17, 2 ADD R 16, R 17 ; R 16 = R 16 + 2 LDI R 17, 27 ; R 17 = 27 SUB R 17, R 16 BRCC L 1 ; if R 16 <= 27 jump L 1 29

Call Topics n n Stack, Push and Pop Calling a function 30

Call Topics n n Stack, Push and Pop Calling a function 30

Stack n PUSH Rr [SP] = Rr SP = SP - 1 n POP

Stack n PUSH Rr [SP] = Rr SP = SP - 1 n POP Rd SP = SP + 1 Rd = [SP] SP Stack 31

Stack Address Code ORG 0 $00 R 20: $10 R 21: SP 0000 LDI

Stack Address Code ORG 0 $00 R 20: $10 R 21: SP 0000 LDI R 16, HIGH(RAMEND) $00 R 22: $30 0001 OUT SPH, R 16 0002 LDI R 16, LOW(RAMEND) R 0: $00 0003 OUT SPL, R 16 0004 LDI R 20, 0 x 10 0005 LDI R 21, 0 x 20 0006 LDI R 22, 0 x 30 0007 PUSH $10 R 20 0008 PUSH $20 R 21 0009 PUSH $30 R 22 000 A POP R 21 000 B POP R 0 000 C POP R 20 000 D L 1: RJMP L 1 $00 $20 0000 Memory 32

Calling a Function n To execute a call: n n Address of the next

Calling a Function n To execute a call: n n Address of the next instruction is saved PC is loaded with the appropriate value Machine code: 940 E 000 A op. Code operand SP Stack PC: 000 C 000 B 0006 0005 0004 0009 0008 Code 0000 LDI R 16, HIGH(RAMEND) 0001 OUT SPH, R 16 0002 LDI R 16, LOW(RAMEND) 0003 OUT SPL, R 16 0004 LDI R 20, 15 0005 LDI R 21, 5 0006 CALL FUNC_NAME 0008 00 08 INC 0009 L 1: 000 A FUNC_NAME: RJMP 000 A ADD 000 B SUBI 000 C RET R 20 L 1 R 20, R 21 R 20, 3 000 D 33

Time delay TMachine cycle = 1 FXTAL 1 16 MHz = 62. 5 ns

Time delay TMachine cycle = 1 FXTAL 1 16 MHz = 62. 5 ns 34

Time delay machine cycle LDI R 16, 19 LDI R 20, 95 LDI R

Time delay machine cycle LDI R 16, 19 LDI R 20, 95 LDI R 21, 5 ADD R 16, R 20 ADD R 16, R 21 Delay = 5 x T machine cycle 1 1 1 5 = 5 x 62. 5 ns = 312. 5 ns 35

Time delay LDI AGAIN: ADD R 16, 100 R 17, R 16 DEC R

Time delay LDI AGAIN: ADD R 16, 100 R 17, R 16 DEC R 16 BRNE AGAIN machine cycle 1 1/2 *100 Branch penalty 36

Time delay LDI R 16, 50 AGAIN: NOP DEC R 16 BRNE AGAIN machine

Time delay LDI R 16, 50 AGAIN: NOP DEC R 16 BRNE AGAIN machine cycle 1 1 1/2 *50 *50 37

Time delay LDI R 17, 20 L 1: LDI R 16, 50 L 2:

Time delay LDI R 17, 20 L 1: LDI R 16, 50 L 2: NOP DEC R 16 BRNE L 2 DEC R 17 BRNE L 1 machine cycle 1 1 1/2 *20 * 50 *20 *20 38