week4. program branching from what we have covered so far, our assembly programs execute one...

45
Week4

Upload: nathan-haskett

Post on 01-Apr-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

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

Week4

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

Program Branching 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

ExampleIf the value of the accumulator is 0 then Launch rocketElse Delay launch

This can be achieved using branch and conditional branch instructions (also known as jump and conditional jump)

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

Program branchingProgram 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 usinga jump instruction

A = R1+R0

R2 = 0

A == 0

R2 = 13

R5 = R2 + R3

TRUE

FALSE

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

Program branching: JZ rel

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 valueCan 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

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

Program operation (Revisited)

Program operation (Revisited)

Consider the following example instructions

MOV A,R0ADD A,#55MOV R5,AMOV R1,#26H

Some instructions are two bytes in size, some are 1.

0000H

0005H0004H0003H0002H0001H

E8

2679FD3724

MOV A,R0

ADD A,#55

MOV R1,#26H

MOV R5,A

}

}

}

}

Note the storage of the instructions in program memory below

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

Program operation (Revisited)

Program operation (Revisited)

Remember what happens when the program is running PC contains address of instruction to fetchInstruction fetch & decode

Op codeOperands (if there are any)

PC incremented by instruction sizeDetermined by opcode

Instruction executedWhat is interesting with branch instructions is that their

execution can modify the PC

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

Program operation (Revisited)

Program operation (Revisited)

Register File

ALU

To Data bus

To Address bus

From Data bus

InstructionRegister (IR)

Program CounterRegister (PC)

Memory 8051

Address bus

Data busE8

2679FD3724

0000H

0005H0004H0003H0002H0001H

PC contains address of instruction to fetch

Instruction fetch & decode

Op code

Operands (if there are any)

PC incremented by instruction size

Instruction executed

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

Program branching: JZ rel

Program branching: JZ rel

Consider including the JZ rel instruction with the example instructions

MOV A,R0ADD A,#55JZ 1MOV R5,AMOV R1,#26H

0000H

0005H0004H0003H0002H0001H

E8

2679FD

3724

MOV A,R0

ADD A,#55

MOV R1,#26H

MOV R5,A

}

}

}

}0160 JZ 01}

0007H0006H

After the JZ instruction execution, the PC will contain the address 0006H if A is zero, otherwise the PC will contain 0005H

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

Program branching: JZ rel

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 2This 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 +127Therefore can jump backwards as well as forwards

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

Program branching: JZ rel

Program branching: JZ rel

Note that in the example code using JZ, the instruction appeared as

JZ 1And the resulting encoding is

60 01In general the resulting encoding is

60Where 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) =DCHThis is equivalent to –36 in

decimal

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

Program branching: JZ rel

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,R0ADD A,#55JZ 1MOV R5,AMOV R1,#26H

We use labelsSymbolic names for addresses

The assembler then does the calculation of the relative address for use

MOV A,R0

ADD A,#55JZ finMOV R5,A

fin: MOV R1,#26HNote: Most assemblers will intrepret the value after JZ as an address and not a relative address!!!

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

Labels in assembly programs

Labels in assembly programs

To create a label in an assembly program, the programmer uses an identifier followed by a colon.

ExampleLoop:

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

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

Program branching: JNZ rel

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

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

ExampleExample

MOV A,R0ADD A,R1MOV R2,#0JNZ nextMOV R2,#13

next: MOV A,R2ADD A,R3

A = R1+R0

R2 = 0

A == 0

R2 = 13

R5 = R2 + R3

TRUE

FALSE

Assuming a start address of 0000H, write out the resulting object code for the above code.

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

Lecture 2

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

ExampleExample

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

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

Example: Solution

Example: Solution

SolutionFlowchart shows

use of loopWith 8051

assemblyUse R2 to

compute sumUse R1 for next

value to count

Sum = Sum +Count

Reduce Count by1

Is Count equal to 0

FALSE

Sum = 0Count = 10

TRUE

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

Example: Code from flowchart

Example: Code from flowchart

loop :MOV A,R2 ADD A,R1 MOV R2,A

MOV A,R1 CLR C SUBB A,#1 MOV R1,A

JNZ loopFALSE

MOV R2,#0MOV R1,#10

TRUE

R2 = R2 + R1

Reduce R1 by 1

R1 == 0FALSE

R2=0R1 = 10

TRUE

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

Example (Simplifying code with new instruction)

Example (Simplifying code with new instruction)

MOV R2,#0MOV R1,#10

Loop: MOV A,R2ADD A,R1MOV R2,AMOV A,R1CLR CSUBB A,#1MOV R1,AJNZ Loop

MOV R2,#0MOV R1,#10

Loop: MOV A,R2ADD A,R1MOV R2,ADEC R1MOV A,R1JNZ Loop

What are the benefits of using DEC R1 in this example?

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

Full details for the decrement instruction

Full details for the decrement instruction

Two forms for the decrement instructionDEC A

DEC source

Where source can be any of Rn, direct or @RiThe instruction subtracts 1 from its operand, storing the

result in same locationUseful instruction since decrementing by 1 is such a

common operation

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

Full details for the increment instruction

Full details for the increment instruction

You guessed that adding 1 is also importantTwo forms for the increment instruction

INC A

INC sourceWhere source can be any of Rn, direct or @RiThe instruction adds 1 to its operand, storing the result in

same locationUseful instruction since incrementing by 1 is such a

common operation

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

Program branching: CJNE A,#data,rel

Program branching: CJNE A,#data,rel

This is another conditional branch instructionCompare & Jump if Not Equal (CJNE)

More flexible than JZ and JNZ, but it is 3 bytes in sizeThe 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 occursModifies the C flag

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

Can be used as followsCJNE A,#data,rel

CJNE A,direct,relCJNE Rn,#data,relCJNE @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

Program branching: Full details for CJNE

instructionWhich should you use? Well depends where the values are that you are comparing!

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

Example: Using CJNE instruction

Example: Using CJNE instruction

Example: Test if R4 is less than, greater than or equal to zero.Test: CJNE R4,#0,Less

… ; Equal to zeroJMP Endtest

Less: MOV A,R4ANL A,#0x80CJNE R4,#0x80,Greater… ; Less than zeroJMP Endtest

Greater: … ; Greater than zero

Endtest:

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

Lecture 3

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

Program branching: DJNZ Rn,rel

Program branching: DJNZ Rn,rel

This is another conditional branch instructionDecrement & Jump if Not Zero (DJNZ)

Used for counting loopsThe 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

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

Can be used as followsDJNZ Rn,relDJNZ 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

Program branching: Full details for DJNZ

instruction

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

Example: Using DJNZ instruction

Example: Using DJNZ instruction

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

Jump instructionsJump instructionsAs well as having conditional jump instructions, there are jump

instructionsI.e. when executed, these instructions change the PCLike a goto statement in a HLL

Have no restriction on where to jump toIf not careful, can end up with spaghetti code!!

Need these instructions for some very basic constructsPre-test loopsIf-Else constructMulti-way selectionEarly loop exits

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

Jump instructionsJump instructionsThere are 3 jump instructions

SJMP relAJMP addr11LJMP 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 elseifADD A,R1MOV R2,#0SJMP endif

elseif: MOV R2,#13endif: MOV A,R2

ADD A,R3

Draw the flowchart for above code.

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

Jump instructionsJump instructionsThe 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

PC

PC bits modified byinstruction

PC bits unchanged byinstruction

aaa a aaa a aaa

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

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

AJMP instructionAJMP instruction

2K

2K2K2K2K

2K2K

0000H0800H1000H1800H

E800HF000HF800H

64K Programaddressspace

2K Block

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

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

Jump instructionsJump instructionsThe third jump instruction is an absolute long

jump (LJMP) using a 16 bit addressHere the PC’s 16 bits are directly replaced with the 16

address bits specified in the instructionCan jump to any location in the 64K program address space

3 bytes in size

aPC

All PC bits modified by instruction

aaa a aaa a aaa a aaa

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

Jump instructionsJump 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 elseifADD A,R1MOV R2,#0JMP endif

elseif: MOV R2,#13endif: 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.

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

Lecture 4

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

Example: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

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

s = start addressof string

is value at snot equal to 0

FALSE

e_count = 0o_count = 0

TRUE

is value at sequal to 'e'

FALSETRUE

Incremente_count

is value at sequal to 'o'

FALSETRUE

Incremento_count

Increment s

Example: Solution

Example: Solution

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

Example: Solution Code

Example: Solution Code

MOV R1,#0MOV R2,#0MOV R0,#20H

Loop: MOV A,@R0JZ LoopendCJNE @R0,#’e’,TestoINC R1JMP Fintest

Testo: CJNE @R0,#’o’,FintestINC R2

Fintest: INC R0JMP Loop

Loopend:

Mapping to assembly code is not trivial!

s = start addressof string

is value at snot equal to 0

FALSE

e_count = 0o_count = 0

TRUE

is value at sequal to 'e'

FALSETRUE

Incremente_count

is value at sequal to 'o'

FALSETRUE

Incremento_count

Increment s

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

Example: Test & DebugExample: Test & Debug

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

Example: Test & DebugExample: Test & Debug

Need to use test dataSeen use of “helloo” stringPick others

Think of general casesThink of extreme cases, e.g. Null string

Can run code and seen results at endCan step through code

Instruction by instructionCan use breakpoints

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

Progress on instructions so far!

Progress on instructions so far!

ACALL addr11 DIV AB LJMP addr16 RETIADD A,<src> DJNZ <byte>,<rel8> MOV <dest>,<src> RL AADDC A,<src> INC <byte> MOV DPTR,#data16 RLC AAJMP addr11 INC DPTR MOV bit,bit RR AANL <dest,<src> JB bit,rel8 MOVC A,@A+<base> RRC AANL <bit> JBC bit,rel8 MOVX <dest>,<src> SETB bitCJNE <dest>,<src>,rel8 JC rel8 MUL AB SJMP rel8CLR A JMP @A+DPTR NOP SUBB A,<src>CLR bit JNB bit,rel8 ORL <dest>,<src> SWAP ACPL A JNC rel8 ORL C,bit XCH A,<byte>CPL bit JNZ rel8 POP direct XCHD A,@RiDA A JZ rel8 PUSH direct XRL <dest>,<src>DEC <byte> LCALL addr16 RET

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

ExercisesExercises

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?

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

ExercisesExercises

4 Taking the following code, explain what happens when the program runs.

1 Write out the resulting machine code

MOV A,R0ADD A,#36JZ finMOV R5,A

fin: MOV R1,#26H

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

ExercisesExercises

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.

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

ExercisesExercises

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