Download Week4

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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
Else
Delay launch
 This
can be achieved using branch and conditional branch
instructions (also known as jump and conditional jump)
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
A = R1+R0
R2 = 0
A == 0
TRUE
R2 = 13
 To
perform this task requires using
a jump instruction
R5 = R2 + R3
FALSE
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
Program operation (Revisited)
 Consider
the following
example instructions
MOV
ADD
MOV
MOV
 Some
A,R0
A,#55
R5,A
R1,#26H
instructions are
two bytes in size, some
are 1.
 Note
the storage of the
instructions in program
memory below
0000H
0001H
0002H
0003H
0004H
0005H
E8
24
37
FD
79
26
} MOV A,R0
} ADD A,#55
} MOV R5,A
} MOV R1,#26H
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
 Instruction
by opcode
executed
 What is interesting with branch instructions is that their
execution can modify the PC
Program operation (Revisited)
8051
Memory
0000H
0001H
0002H
0003H
0004H
0005H
E8
24
37
FD
79
26
Data bus
Instruction
Register (IR)
From Data
bus
Register File
Address bus
To
Data
bus
Program Counter
Register (PC)
PC
contains address of instruction to fetch
Instruction
Op
fetch & decode
code
Operands
PC
(if there are any)
incremented by instruction size
Instruction
executed
To Address
bus
ALU
Program branching: JZ rel
 Consider
including the JZ rel instruction with the
example instructions
0000H
0001H
0002H
0003H
0004H
0005H
0006H
0007H
E8
24
37
60
01
FD
79
26
} MOV A,R0
} ADD A,#55
} JZ 01
MOV A,R0
ADD A,#55
} MOV R5,A
JZ 1
MOV R1,#26H
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
}
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 A not being zero
Example
A = R1+R0
R2 = 0
FALSE
A == 0
TRUE
R2 = 13
R5 = R2 + R3
next:
MOV
ADD
MOV
JNZ
MOV
MOV
ADD
A,R0
A,R1
R2,#0
next
R2,#13
A,R2
A,R3
Assuming a start address of 0000H,
write out the resulting object code
for the above code.
Lecture 2
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
shows
use of loop
 With 8051
assembly
Sum = 0
Count = 10
 Flowchart
 Use
R2 to
compute sum
 Use R1 for next
value to count
Sum = Sum +
Count
Reduce Count by
1
FALSE
Is Count equal to 0
TRUE
Example: Code from flowchart
MOV R2,#0
MOV R1,#10
R2=0
R1 = 10
loop :MOV A,R2
ADD A,R1
MOV R2,A
R2 = R2 + R1
MOV A,R1
CLR C
SUBB A,#1
MOV R1,A
Reduce R1 by 1
FALSE
FALSE
R1 == 0
TRUE
JNZ loop
TRUE
Example (Simplifying code with
new instruction)
Loop:
MOV R2,#0
MOV R1,#10
MOV A,R2
ADD A,R1
MOV R2,A
MOV A,R1
CLR C
SUBB A,#1
MOV R1,A
JNZ Loop
Loop:
MOV
MOV
MOV
ADD
MOV
DEC
MOV
JNZ
R2,#0
R1,#10
A,R2
A,R1
R2,A
R1
A,R1
Loop
What are the benefits of using
DEC R1 in this example?
Full details for the decrement
instruction
 Two
forms for the decrement instruction
DEC A
DEC source
 Where
 The
source can be any of Rn, direct or @Ri
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 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
CJNE
CJNE
CJNE
A,#data,rel
A,direct,rel
Rn,#data,rel
@Ri,#data,rel
Which should you use?
Well depends where the
values are that you are
comparing!
 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
Example: Using CJNE instruction
 Example:
zero.
Test if R4 is less than, greater than or equal to
Test:
Less:
Greater:
Endtest:
CJNE R4,#0,Less
… ; Equal to zero
JMP Endtest
MOV A,R4
ANL A,#0x80
CJNE R4,#0x80,Greater
… ; Less than zero
JMP Endtest
… ; Greater than zero
Lecture 3
Program branching:
DJNZ Rn,rel
 This
is another conditional branch instruction
 Decrement
 Used
& Jump if Not Zero (DJNZ)
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
Example: Using DJNZ instruction
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
 If
no restriction on where to jump to
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
 There are 3 jump
SJMP rel
AJMP addr11
LJMP addr16
instructions
 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.
Jump instructions
 The
second jump instruction is
an absolute jump (AJMP) using
an 11 bit address
 The
 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 unchanged by
instruction
a a a a a a a a a a
PC bits modified by
instruction
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
a currently addressing
AJMP instruction
 There
F800H
F000H
E800H
2K
2K
2K
2K
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
64K Program
address
space
1800H
1000H
0800H
0000H
2K
2K
2K
2K Block
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
3
jump to any location in the 64K program address space
bytes in size
PC
a a a a a a a a a a a a a a a a
All PC bits modified by instruction
Jump instructions
 OK,
3 jump instructions.
Which do you use??
 Example:
 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.
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.
Lecture 4
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
e_count = 0
o_count = 0
Example: Solution
s = start address
of string
is value at s
not equal to 0
FALSE
TRUE
TRUE
is value at s
equal to 'e'
Increment
e_count
FALSE
TRUE
Increment
o_count
Increment s
is value at s
equal to 'o'
FALSE
Example: Solution Code
Mapping to
assembly code
is not trivial!
e_count = 0
o_count = 0
s = start address
of string
is value at s
not equal to 0
FALSE
Loop:
TRUE
TRUE
is value at s
equal to 'e'
Increment
e_count
FALSE
TRUE
Increment
o_count
Increment s
is value at s
equal to 'o'
FALSE
Testo:
Fintest:
Loopend:
MOV R1,#0
MOV R2,#0
MOV R0,#20H
MOV A,@R0
JZ Loopend
CJNE @R0,#’e’,Testo
INC R1
JMP Fintest
CJNE @R0,#’o’,Fintest
INC R2
INC R0
JMP Loop
Example: Test & Debug
Example: Test & Debug
 Need
to use test data
 Seen
 Pick
use of “helloo” string
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
 Can
by instruction
use breakpoints
Progress on instructions so far!
ACALL addr11
ADD A,<src>
ADDC A,<src>
AJMP addr11
ANL <dest,<src>
ANL <bit>
CJNE <dest>,<src>,rel8
CLR A
CLR bit
CPL A
CPL bit
DA A
DEC <byte>
DIV AB
DJNZ <byte>,<rel8>
INC <byte>
INC DPTR
JB bit,rel8
JBC bit,rel8
JC rel8
JMP @A+DPTR
JNB bit,rel8
JNC rel8
JNZ rel8
JZ rel8
LCALL addr16
LJMP addr16
RETI
MOV <dest>,<src> RL A
MOV DPTR,#data16 RLC A
MOV bit,bit
RR A
MOVC A,@A+<base> RRC A
MOVX <dest>,<src> SETB bit
MUL AB
SJMP rel8
NOP
SUBB A,<src>
ORL <dest>,<src> SWAP A
ORL C,bit
XCH A,<byte>
POP direct
XCHD A,@Ri
PUSH direct
XRL <dest>,<src>
RET
Exercises
1.
2.
3.
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.
1.
2.
How many bytes are needed to represent it?
What does rel represent?
1.
3.
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.
4
1
Write out the resulting machine code
fin:
MOV A,R0
ADD A,#36
JZ fin
MOV R5,A
MOV R1,#26H
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.
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?
6
7
1
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
8


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