Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week4. Program Branching  From what we have covered so far, our assembly programs execute one instruction after another in sequence  Programs that solve.

Similar presentations


Presentation on theme: "Week4. Program Branching  From what we have covered so far, our assembly programs execute one instruction after another in sequence  Programs that solve."— Presentation transcript:

1 Week4

2 Program Branching  From what we have covered so far, our assembly programs execute one instruction after another in sequence  Programs that solve real world problems must be able to alter the flow of control in a program  Example If the value of the accumulator is 0 then Launch rocket Else Delay launch  This can be achieved using branch and conditional branch instructions (also known as jump and conditional jump )

3 Program branching  Consider the requirement to perform the following task. Compute R1 + R0 and store in A Store 0 in R2 If A is equal to zero then Store 13 in R2 Compute R2 + R3 and store in R5  To perform this task requires using a jump instruction

4 Program branching: JZ rel  The instruction  JZ rel  Changes the program counter (i.e. address of next instruction) by the value specified by rel if A is zero  rel is an 8 bit signed value  Can move forward or backwards!!!  This instruction is 2 bytes in size  To understand this instruction and other branch instructions, it is necessary to consider addresses for instructions

5 Program operation (Revisited)  Consider the following example instructions MOV A,R0 ADD A,#55 MOV R5,A MOV R1,#26H  Some instructions are two bytes in size, some are 1.  Note the storage of the instructions in program memory below

6 Program operation (Revisited)  Remember what happens when the program is running  PC contains address of instruction to fetch  Instruction fetch & decode  Op code  Operands (if there are any)  PC incremented by instruction size  Determined by opcode  Instruction executed  What is interesting with branch instructions is that their execution can modify the PC

7 Program operation (Revisited) Memory 8051 Address bus Data bus  PC contains address of instruction to fetch  Instruction fetch & decode  Op code  Operands (if there are any)  PC incremented by instruction size  Instruction executed

8 Program branching: JZ rel  Consider including the JZ rel instruction with the example instructions MOV A,R0 ADD A,#55 JZ 1 MOV R5,A MOV R1,#26H After the JZ instruction execution, the PC will contain the address 0006H if A is zero, otherwise the PC will contain 0005H

9 Program branching: JZ rel  In general, the operation of this instruction is such that the program counter (PC) will be modified as follows  If A is zero then PC = PC + rel  Note that prior to this, the PC counter has been incremented by 2  This is the size of the JZ rel instruction  If A is not zero, then rel is not added to the PC and the PC will contain the address of the next instruction after the JZ instruction  Note that rel can be between –128 and +127  Therefore can jump backwards as well as forwards

10 Program branching: JZ rel  Note that in the example code using JZ, the instruction appeared as JZ 1  And the resulting encoding is 60 01  In general the resulting encoding is 60  Where is the 8 bit value for the relative jump  This instruction requires the use of a relative address  I.e. if we are at address 002AH and we want to conditional jump to address 0008H (jump backwards), then we need to work out the relative address as  0008 – (002A + 2) =DCH  This is equivalent to –36 in decimal

11 Program branching: JZ rel  The good news is that we don’t have to calculate the relative address  The assembler will do it for us!!  Instead of the following MOV A,R0 ADD A,#55 JZ 1 MOV R5,A MOV R1,#26H  We use labels  Symbolic names for addresses  The assembler then does the calculation of the relative address for use MOV A,R0 ADD A,#55 JZ fin MOV R5,A fin:MOV R1,#26H Note: Most assemblers will intrepret the value after JZ as an address and not a relative address!!!

12 Labels in assembly programs  To create a label in an assembly program, the programmer uses an identifier followed by a colon.  Example Loop:  The label can be followed on the same line by instructions and/or comments or can be left blank  Where a label is used, it must appear at the start of the line  The assembler keeps track of program addresses as a program is being assembled and so is able to associate an address with a label when found  Any reference to that label refers to the associated address

13 Program branching: JNZ rel  The instruction  JNZ rel  Is similar to JZ rel, but differs in that the jump is conditional on the value in A not being zero

14 Example MOV A,R0 ADD A,R1 MOV R2,#0 JNZ next MOV R2,#13 next:MOV A,R2 ADD A,R3 Assuming a start address of 0000H, write out the resulting object code for the above code.

15 Lecture 2

16 Example  Write a program to sum the numbers from 1 to 10  The solution illustrates what can be achieved with the few instructions we have encountered so far  As we cover more instructions, the solution given here can be simplified!  This solution shows how a conditional jump can be used to create a loop  Note the last example illustrated a conditional selection

17 Example: Solution  Solution  Flowchart shows use of loop  With 8051 assembly  Use R2 to compute sum  Use R1 for next value to count

18 Example: Code from flowchart

19 Example (Simplifying code with new instruction) MOV R2,#0 MOV R1,#10 Loop:MOV A,R2 ADD A,R1 MOV R2,A MOV A,R1 CLR C SUBB A,#1 MOV R1,A JNZ Loop MOV R2,#0 MOV R1,#10 Loop:MOV A,R2 ADD A,R1 MOV R2,A DEC R1 MOV A,R1 JNZ Loop What are the benefits of using DEC R1 in this example?

20 Full details for the decrement instruction  Two forms for the decrement instruction DEC A DEC source  Where source can be any of Rn, direct or @Ri  The instruction subtracts 1 from its operand, storing the result in same location  Useful instruction since decrementing by 1 is such a common operation

21 Full details for the increment instruction  You guessed that adding 1 is also important  Two forms for the increment instruction INC A INC source  Where source can be any of Rn, direct or @Ri  The instruction adds 1 to its operand, storing the result in same location  Useful instruction since incrementing by 1 is such a common operation

22 Program branching: CJNE A,#data,rel  This is another conditional branch instruction  C ompare & J ump if N ot E qual (CJNE)  More flexible than JZ and JNZ, b ut it is 3 bytes in size  The instruction operates by  Comparing the contents of A with the constant value #data and if the 2 values are not equal, the program counter (PC) is modified by adding rel to it. If the 2 values are equal, then the PC is not modified  Essentially a subtract occurs  Modifies the C flag

23  Can be used as follows CJNE A,#data,rel CJNE A,direct,rel CJNE Rn,#data,rel CJNE @Ri,#data,rel  All of the above instructions will compare two values and execute a relative jump if the 2 values are not equal >The first of the above is used when to compare the contents of A with a constant >The second to compare the contents of A with the contents of a memory location in the first 128 locations > the third to compare the contents of a register and a constant >the fourth to compare a value using indirect addressing with a constant Program branching: Full details for CJNE instruction Which should you use? Well depends where the values are that you are comparing!

24 Example: Using CJNE instruction  Example: Test if R4 is less than, greater than or equal to zero. Test:CJNE R4,#0,Less … ; Equal to zero JMP Endtest Less:MOV A,R4 ANL A,#0x80 CJNE R4,#0x80,Greater … ; Less than zero JMP Endtest Greater:… ; Greater than zero Endtest:

25 Lecture 3

26 Program branching: DJNZ Rn,rel  This is another conditional branch instruction  D ecrement & J ump if N ot Z ero (DJNZ)  Used for counting loops  The instruction operates by comparing decrementing the value in a register (Rn) and then if the value is not zero then the program counter (PC) is modified by adding rel to it. If the value is zero, then the PC is not modified

27  Can be used as follows DJNZ Rn,rel DJNZ direct,rel  The above instructions will decrement a value and compare the value to zero. If the value is not zero a relative jump will occur. >The first of the above is used with a register Rn »2 bytes in size >The second will work with any direct address »3 bytes in size Program branching: Full details for DJNZ instruction

28 Example: Using DJNZ instruction

29 Jump instructions  As well as having conditional jump instructions, there are jump instructions  I.e. when executed, these instructions change the PC  Like a goto statement in a HLL  Have no restriction on where to jump to  If not careful, can end up with spaghetti code!!  Need these instructions for some very basic constructs  Pre-test loops  If-Else construct  Multi-way selection  Early loop exits

30 Jump instructions  There are 3 jump instructions SJMP rel AJMP addr11 LJMP addr16  The first of these is a short relative jump ( SJMP )  Here the PC is modified by adding a signed 8 bit value (rel) to the PC  Can jump backwards 128 bytes or forward 127 bytes  2 bytes in size  Example: JZ elseif ADD A,R1 MOV R2,#0 SJMP endif elseif:MOV R2,#13 endif:MOV A,R2 ADD A,R3 Draw the flowchart for above code.

31 Jump instructions  The second jump instruction is an absolute jump ( AJMP ) using an 11 bit address  Here the PC’s lower 11 bits are directly replaced with the 11 address bits specified in the instruction  Also 2 bytes in size  The advantage of this instruction is that a potentially bigger jump than that of SJMP can be made.  The max possible is 2047 bytes, but the jump size is normally less than this  The exact view is that a jump is possible to any address within the 2K block within which the PC is currently addressing

32 AJMP instruction  There are thirty two 2K blocks in the 64K address space.  Using an AJMP instruction means that a jump can occur to any location within the 2K block in which the instruction resides

33 Jump instructions  The third jump instruction is an absolute long jump ( LJMP ) using a 16 bit address  Here the PC’s 16 bits are directly replaced with the 16 address bits specified in the instruction  Can jump to any location in the 64K program address space  3 bytes in size

34 Jump instructions  OK, 3 jump instructions. Which do you use??  Ans: The assembler will do the work for you!!  In the code use the instruction JMP label  The assembler works out which of the 3 instructions is needed  Replaces the JMP instruction with one of the 3.  Example: JZ elseif ADD A,R1 MOV R2,#0 JMP endif elseif:MOV R2,#13 endif:MOV A,R2 ADD A,R3 Given the starting address of 0000H, write out the object code in hex for each of the 3 possible JMP instructions.

35 Lecture 4

36 Example:  Write a program that counts the number of lowercase e’s and o’s in a string in data memory  Assume the string starts at address 20H and the end of the string is marked with zero value  Use R1 and R2 to store count values

37 Example: Solution

38 Example: Solution Code MOV R1,#0 MOV R2,#0 MOV R0,#20H Loop:MOV A,@R0 JZ Loopend CJNE @R0,#’e’,Testo INC R1 JMP Fintest Testo:CJNE @R0,#’o’,Fintest INC R2 Fintest:INC R0 JMP Loop Loopend: Mapping to assembly code is not trivial!

39 Example: Test & Debug

40  Need to use test data  Seen use of “helloo” string  Pick others  Think of general cases  Think of extreme cases, e.g. Null string  Can run code and seen results at end  Can step through code  Instruction by instruction  Can use breakpoints

41 Progress on instructions so far! ACALL addr11 DIV AB LJMP addr16RETI ADD A, DJNZ, MOV, RL A ADDC A, INC MOV DPTR,#data16RLC A AJMP addr11 INC DPTR MOV bit,bitRR A ANL JB bit,rel8 MOVC A,@A+ RRC A ANL JBC bit,rel8 MOVX, SETB bit CJNE,,rel8 JC rel8 MUL ABSJMP rel8 CLR A JMP @A+DPTR NOPSUBB A, CLR bit JNB bit,rel8 ORL, SWAP A CPL A JNC rel8 ORL C,bitXCH A, CPL bit JNZ rel8 POP directXCHD A,@Ri DA A JZ rel8 PUSH direct XRL, DEC LCALL addr16 RET

42 Exercises 1.Why are there branch instructions in an instruction set? 2.What is another term for branch in this context? 3.Taking the instruction JZ rel, explain the operation of this instruction. 1.How many bytes are needed to represent it? 2.What does rel represent? 1.How is its value determined? 3.What role does the PC regsiter have during the execution of this instruction?

43 Exercises 4 Taking the following code, explain what happens when the program runs. 1 Write out the resulting machine code MOV A,R0 ADD A,#36 JZ fin MOV R5,A fin:MOV R1,#26H

44 Exercises 5 Write out the code to sum the numbers from 3 to 13.  Detail a flowchart to describe the algorithm.  Detail what each registers use is.  Give values for register contents as code is hand executed  Explain how the loop ends  Detail machine code for program also.

45 Exercises 6 Explain using code how to test if an 8-bit value is less than equal or greater than zero; 7 What use is the JMP instruction? 1 Give an example 8 Write a program that counts the number of lowercase e ’s, a ’s and o ’s in a string in data memory  Assume the string starts at address 20H and the end of the string is marked with zero value  Use R1, R2 and R3 to store count values


Download ppt "Week4. Program Branching  From what we have covered so far, our assembly programs execute one instruction after another in sequence  Programs that solve."

Similar presentations


Ads by Google