Machine Cycle
Machine Cycle
Machine Cycle
Solution:
Dr. K. Ghosh 32
JNC label ;jump if no carry, CY=0
➢ If CY = 0, the CPU starts to fetch and execute instruction from the address of
the label
➢ If CY = 1, it will not jump but will execute the next instruction below JNC
Find the sum of the values 79H, F5H, E2H. Put the sum in registers R0 (low
byte) and R5 (high byte).
MOV A,#0 ;A=0
MOV R5,A ;clear R5
ADD A,#79H ;A=0+79H=79H
JNC L1 ;if CY=0, add next number
INC R5 ;if CY=1, increment R5
L1: ADD A,#0F5H ;A=79+F5=6E and CY=1
JNC L2 ;jump if CY=0
INC R5 ;if CY=1,increment R5 (R5=1)
L2: ADD A,#0E2H ;A=6E+E2=50 and CY=1
JNC OVER ;jump if CY=0
INC R5 ;if CY=1, increment R5
OVER: MOV R0,A ;now R0=50H, and R5=02
Dr. K. Ghosh 33
Stack
▪ RAM location 08 is the first location begin used for the stack
by the 8051
Dr. K. Ghosh 34
Stack contd.. (PUSH and POP)
Show the stack and stack pointer from the following. Assume the
default stack area.
MOV R6, #25H
MOV R1, #12H
MOV R4, #0F3H
Solution:
PUSH6
PUSH1
PUSH4
Dr. K. Ghosh 36
Example
Examining the stack, show the contents of the register and SP after execution of
the following instructions. All value are in hex.
POP 3 ; POP stack into R3
POP 5 ; POP stack into R5
POP 2 ; POP stack into R2
Dr. K. Ghosh 37
Example
Examining the stack, show the contents of the register and SP after execution of
the following instructions. All value are in hex.
MOV SP, #5FH ;make RAM location 60H
;first stack location
Dr. K. Ghosh 38
Example
Write PUSH instruction to push the content of the register on stack after the
execution of the following set of instructions.
MOV SP, #4FH
SETB PSW .3
MOV R0, #25H
MOV R1, #0CH
MOV R2, #05H
MOV A, #0CEH
Dr. K. Ghosh 39
❑ The unconditional jump is a jump in which control
is transferred unconditionally to the target location
LJMP(long jump)
➢ 3-byte instruction
▪ First byte is the opcode
▪ Second and third bytes represent the 16-bit target address
– Any memory location from 0000 to FFFFH
SJMP(short jump)
➢ 2-byte instruction
▪ First byte is the opcode
▪ Second byte is the relative target address
– 00 to FFH
Dr. K. Ghosh 40
PC
❑ First notice that the JZ and JNC instructions both jump forward. The target
address for a forward jump is calculated by adding the PC of the following
instruction to the second byte of the short jump instruction, which is called the
relative address. In line 4 the instruction “JZ NEXT” has opcode of 60 and
operand of 03 at the addresses of 0004 and 0005. The 03 is the relative
address, relative to the address of the next instruction INC R0, which is 0006.
By adding 0006 to 3, the target address of the label NEXT, which is 0009, is
generated. In the same way for line 9, the “JNC OVER” instruction has
opcode and operand of 50 and 05 where 50 is the opcode and 05 the relative
address. Therefore, 05 is added to 000D, the address of instruction “CLR A”,
giving 12H, the address of label OVER.
Dr. K. Ghosh 46
Backward Jump
❑ In that program list, “JNC AGAIN” has opcode 50 and relative address F2H.
When the relative address of F2H is added to 15H, the address of the
instruction below the jump, we have 15H + F2H = 07 (the carry is dropped).
Notice that 07 is the address of label AGAIN. Look also at “SJMP HERE”,
which has 80 and FE for the opcode and relative address, respectively. The PC
of the following instruction, 0017H, is added to FEH, the relative address, to
get 0015H, address of the HERE label (17H + FEH = 15H). Notice that FEH
is -2 and 17H + (-2) = 15H. For further discussion of the addition of negative
numbers, see Chapter 6.
Dr. K. Ghosh 47
Machine Cycle
Find the period of the machine cycle for 11.0592 MHz crystal
frequency
Solution:
11.0592/12 = 921.6 kHz;
machine cycle is 1/921.6 kHz = 1.085μs
Dr. K. Ghosh 48
Example
Solution:
(a) 11.0592 MHz/12 = 921.6 kHz; machine cycle is 1/921.6 kHz = 1.085 µs
(microsecond)
(b) 16 MHz/12 = 1.333 MHz; machine cycle (MC) = 1/1.333 MHz = 0.75 µs
Dr. K. Ghosh 49
Example
For 8051 system of 11.0592 MHz, find how long it takes to execute
each instruction.
(a) MOV R3,#55 (b) DJNZ R2 target
(c) LJMP (d) SJMP (e) NOP (f) MUL AB
Solution:
Machine cycles Time to execute
(a) 1 1x1.085μs = 1.085μs
(b) 2 2x1.085μs = 2.17μs
(c) 2 2x1.085μs = 2.17μs
(d) 2 2x1.085μs = 2.17μs
(e) 1 1x1.085μs = 1.085μs
(f) 4 4x1.085μs = 4.34μs
Dr. K. Ghosh 50
Example
MOV A,#55H
AGAIN: MOV P1,A
ACALL DELAY
CPL A
SJMP AGAIN
;---time delay-------
DELAY: MOV R3,#200
HERE: DJNZ R3,HERE
RET
Solution:
Machine cycle
DELAY: MOV R3,#200 1
HERE: DJNZ R3,HERE 2
RET 2
Therefore, [(200x2)+1+2]x1.085μs = 436.255μs.
Dr. K. Ghosh 51
Example
Machine Cycle
DELAY: MOV R3,#250 1
HERE: NOP 1
NOP 1
NOP 1
NOP 1
DJNZ R3,HERE 2
RET 2
Solution:
The time delay inside HERE loop is
[250(1+1+1+1+2)]x1.085μs = 1627.5μs.
Adding the two instructions outside loop we
have 1627.5μs + 3 x 1.085μs = 1630.755μs
52
53
54