460 likes | 611 Views
Week4. 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
E N D
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)
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 Program branching
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 Program branching: JZ rel
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. Program operation (Revisited) • Note the storage of the instructions in program memory below
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 Program operation (Revisited)
Program operation (Revisited) 8051 Memory Data bus Address 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
Consider including the JZ rel instruction with the example instructions MOV A,R0 ADD A,#55 JZ 1 MOV R5,A MOV R1,#26H Program branching: JZ rel After the JZ instruction execution, the PC will contain the address 0006H if A is zero, otherwise the PC will contain 0005H
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
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
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!!!
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
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 Anot being zero
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.
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
Example: Solution • Solution • Flowchart shows use of loop • With 8051 assembly • Use R2 to compute sum • Use R1 for next value to count
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 Example (Simplifying code with new instruction) What are the benefits of using DEC R1 in this example?
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 Full details for the decrement instruction
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
Program branching: CJNE A,#data,rel • This is another conditional branch instruction • Compare & Jump if Not Equal (CJNE) • More flexible than JZ and JNZ, but 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
Program branching: Full details for CJNE instruction • 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 Which should you use? Well depends where the values are that you are comparing!
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:
Program branching: DJNZ Rn,rel • This is another conditional branch instruction • Decrement & Jump if Not Zero (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
Program branching: Full details for DJNZ instruction • 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
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
Jump instructions • Example: JZ elseif ADD A,R1 MOV R2,#0 SJMP endif elseif: MOV R2,#13 endif: MOV A,R2 ADD A,R3 • 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 Draw the flowchart for above code.
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
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
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
Jump instructions • Example: JZ elseif ADD A,R1 MOV R2,#0 JMP endif elseif: MOV R2,#13 endif: MOV A,R2 ADD A,R3 • 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. Given the starting address of 0000H, write out the object code in hex for each of the 3 possible JMP instructions.
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
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! Example: Solution Code
Example: Test & Debug • 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
Progress on instructions so far! ACALL addr11 DIV AB LJMP addr16 RETI ADD A,<src> DJNZ <byte>,<rel8> MOV <dest>,<src> RL A ADDC A,<src> INC <byte> MOV DPTR,#data16 RLC A AJMP addr11 INC DPTR MOV bit,bit RR A ANL <dest,<src> JB bit,rel8 MOVC A,@A+<base> RRC A ANL <bit> JBC bit,rel8 MOVX <dest>,<src> SETB bit CJNE <dest>,<src>,rel8 JC rel8 MUL AB SJMP rel8 CLR A JMP @A+DPTR NOP SUBB A,<src> CLR bit JNB bit,rel8 ORL <dest>,<src> SWAP A CPL A JNC rel8 ORL C,bit XCH A,<byte> CPL bit JNZ rel8 POP direct XCHD A,@Ri DA A JZ rel8 PUSH direct XRL <dest>,<src> DEC <byte> LCALL addr16 RET
Exercises • Why are there branch instructions in an instruction set? • What is another term for branch in this context? • Taking the instruction JZ rel, explain the operation of this instruction. • How many bytes are needed to represent it? • What does rel represent? • How is its value determined? • What role does the PC regsiter have during the execution of this instruction?
Exercises • Taking the following code, explain what happens when the program runs. • Write out the resulting machine code MOV A,R0 ADD A,#36 JZ fin MOV R5,A fin: MOV R1,#26H
Exercises • 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.
Exercises • Explain using code how to test if an 8-bit value is less than equal or greater than zero; • What use is the JMP instruction? • Give an example • 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