Machine Cycle

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

❑ Jump only if a certain condition is met

JZ label ;jump if A=0

MOV A,R0 ;A=R0


JZ OVER ;jump if A = 0
MOV A,R1 ;A=R1
JZ OVER ;jump if A = 0
OVER:
END

❖ JZ can be used only for register A, not for any other


registers.
Dr. K. Ghosh 31
Examples:

Write a program to determine if R5 contains the value 0. If so,


put 55H in it.

Solution:

MOV A,R5 ;copy R5 to A


JNZ NEXT ;jump if A is not zero
MOV R5,#55H
NEXT:
END

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

❑ The stack is a section of RAM used by the CPU to store


information temporarily
➢ This information could be data or an address

❑ The register used to access the stack is called the SP


(stack pointer) register
➢ The stack pointer in the 8051 is only 8 bit wide, which means
that it can take value of 00 to FFH
➢ When the 8051 is powered up, the SP register contains value
07

▪ RAM location 08 is the first location begin used for the stack
by the 8051
Dr. K. Ghosh 34
Stack contd.. (PUSH and POP)

❑ The storing of a CPU register in the stack is called a


PUSH
➢ SP is pointing to the last used location of the stack
➢ As we push data onto the stack, the SP is incremented by one
▪ This is different from many microprocessors

❑ Loading the contents of the stack back into a CPU


register is called a POP
➢ With every pop, the top byte of the stack is copied to the
register specified by the instruction and the stack pointer is
decremented once
Dr. K. Ghosh 35
Example

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

MOV R2, #25H


MOV R1, #12H
MOV R4, #0F3H
PUSH 2 Solution:
PUSH 1
PUSH 4

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

1 0000 ORG 0H ;start (origin) at 0


2 0000 7D25 MOV R5,#25H ;load 25H into R5
Address Code
3 0002 7F34 MOV R7,#34H ;load 34H into R7
4 0004 7400 MOV A,#0 ;load 0 into A 0000 7D
5 0006 2D ADD A,R5 ;add contents of R5 to A 0001 25
;now A = A + R5
6 0007 2F ADD A,R7 ;add contents of R7 to A 0002 7F
;now A = A + R7 0003 34
7 0008 2412 ADD A,#12H ;add to A value 12H
0004 74
;now A = A + 12H
8 000A 80EF HERE: SJMP HERE ;stay in this loop 0005 00
9 000C END ;end of asm source file 0006 2D
0007 2F
ROM Address Machine Language Assembly Language
0008 24
0000 7D25 MOV R5, #25H
0002 7F34 MOV R7, #34H 0009 12
0004 7400 MOV A, #0 000A 80
0006 2D ADD A, R5 000B FE
0007 2F ADD A, R7
0008 2412 ADD A, #12H
000A 80EF HERE: SJMP HERE 41
Dr. K. Ghosh
42
Dr. K. Ghosh
43
Dr. K. Ghosh
44
Dr. K. Ghosh
LINE PC OPCODE Mnemonic Operand
01 0000 ORG 0000
02 0000 7800 MOV R0,#0
03 0002 7455 MOV A,#55H
04 0004 6003 JZ NEXT
05 0006 08 INC R0
06 0007 04 AGAIN: INC A
07 0008 04 + INC A
08 0009 2477 NEXT: ADD A,#77H
09 000B 5005 JNC OVER
10 000D E4 CLR A
11 000E F8 MOV R0,A
12 000F F9 + MOV R1,A
13 0010 FA MOV R2,A
14 0011 FB MOV R3,A
15 0012 2B OVER: ADD A,R3
16 0013 50F2 JNC AGAIN
17 0015 80FE + HERE: SJMP HERE
18 0017 END
45
Dr. K. Ghosh
Forward Jump

❑ 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

❑ CPU executing an instruction takes a certain number of clock cycles


➢ These are referred to as machine cycles

❑ The length of machine cycle depends on the frequency of the crystal


oscillator connected to 8051
❑ In original 8051, one machine cycle lasts 12 oscillator periods

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

The following shows crystal frequency for three different 8051-based


systems. Find the period of the machine cycle in each case.
(a) 11.0592 MHz (b) 16 MHz (c) 20 MHz

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

(c) 20 MHz/12 = 1.66 MHz; MC = 1/1.66 MHz = 0.60 µ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

Find the size of the delay in following program, if the


frequency is 11.0592MHz.

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

Find the size of the delay in following program, if the crystal


frequency is 11.0592MHz.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy