PROGRAMMING OF 8085 PROCESSOR UNIT II Mr. S. VINOD ASSISTANT PROFESSOR EEE DEPARTMENT
description
Transcript of PROGRAMMING OF 8085 PROCESSOR UNIT II Mr. S. VINOD ASSISTANT PROFESSOR EEE DEPARTMENT
PROGRAMMING OF 8085 PROCESSOR
UNIT II
Mr. S. VINOD
ASSISTANT PROFESSOR
EEE DEPARTMENT
• Instruction format and addressing modes • Assembly language format
- Data transfer,
-data manipulation & control instructions • Programming:
-Loop structure with counting & Indexing
- Look up table
- Subroutine instructions stack.
ADDRESSING MODES
-Every instruction of a program has to operate on a data. -The method of specifying the data to be operated by the
instruction is called Addressing.
The 8085 has the following 5 different types of addressing. 1. Immediate Addressing2. Direct Addressing3. Register Addressing4. Register Indirect Addressing5. Implied Addressing
IMMEDIATE ADDRESSING
• In immediate addressing mode, the data is specified in the instruction itself. The data will be a part of the program instruction.
• EX.
-MVI B, 3EH - Move the data 3EH given in the instruction to B register;
-LXI SP, 2700H- Move the data 2700h to stack pointer
-ADI 45H- The 8-bit data (operand) is added to the contents of the accumulator and the result is stored in the accumulator
-ACI 45H -The 8-bit data (operand) and the Carry flag are added to the contents of the accumulator and the result is stored in the accumulator
SUI 45H , SBI 45H , ORI 86H
DIRECT ADDRESSING
• In direct addressing mode, the address of the data is specified in the instruction.
• The data will be in memory. In this addressing mode, the program instructions and data can be stored in different memory.
EX. LDA 1050H - Load the data available in memory location 1050H in to accumulator;
SHLD 3000H-The contents of register L are stored into the memory location specified by the 16-bit address in the operand and the contents of H register are stored into the next memory location by incrementing the operand
STA 4350H-The contents of the accumulator are copied into the memory location specified by the operand
REGISTER ADDRESSING
• In register addressing mode, the instruction specifies the name of the register in which the data is available.
EX. MOV A, B - Move the content of B register to A register;
SPHL- The instruction loads the contents of the H
and L registers into the stack pointer register ADD C-The contents of the operand (register or
memory) are added to the contents of the accumulator and the result is stored in the accumulator
XCHG-The contents of register H are exchanged with the contents of register D, and the contents of register L are exchanged with the contents of register E
REGISTER INDIRECT ADDRESSING
• In register indirect addressing mode, the instruction specifies the name of the register in which the address of the data is available.
• Here the data will be in memory and the address will be in the register pair.
EX. MOV A, M - The memory data addressed by H L pair is moved to A register.
LDAX B -The contents of the designated register pair point to a memory location. This instruction copies the contents of that memory location into the accumulator
•
IMPLIED ADDRESSING
• In implied addressing mode, the instruction itself specifies the data to be operated.
EX. CMA - Complement the content of accumulator;
RAL-Each binary bit of the accumulator is rotated left by one position through the Carry flag. Bit D7 is placed in the Carry flag, and the Carry flag is placed in the least significant position
1.Calculate the sum of series of numbers. The length of the series is in memory location 4200H and the series begins from memory location 4201H.
a. Consider the sum to be 8 bit number. So, ignore carries. Store the sum at memory location 4300H
LDA 4200H MOV C, A : Initialize counterSUB A : sum = 0LXI H, 420lH : Initialize pointerBACK: ADD M : SUM = SUM + dataINX H : increment pointerDCR C : Decrement counterJNZ BACK : if counter 0 repeatSTA 4300H : Store sumHLT : Terminate program execution
2.a. To write an assembly language program to multiply two 8-bit numbers.
3.b. To write an assembly language program to division
1.MULTIPLICATION PROGRAM
LDA 4200H MOV E, A
MVI D, 00HLDA 4201HMOV C, ALXI H,0000H
LOOP: DAD DDCR CJNZ LOOPSHLD 4300HHLT
program simulation
program simulation
LDA 2000H ;divisorMOV B,ALDA 2001H ;dividendMVI C, 00H
LOOP: CMP BJC BRANCHSUB BINR CJMP LOOP
BRANCH: STA 3000H ;reminderMOV A,CSTA 3001H ;quotientHLT
Division Of Two 8 Bit
program simulation
4.
START: LXI H, 2000HLXI D, 3000HMVI C, 0AH
LOOP: MOV A,MSTAX DINX HINX DDCR CJNZ LOOPHLT
Program
program simulation
5. PROGRAM FOR SMALLEST NUMBER IN GIVEN ARRAY
-Get number of element as first number
-compare two number if 1st number is smaller than 2nd then carry will be set so number in acc is small
-if reverse move the 2nd number to acc
-get the next number repeat the procedure
-finally the number in the acc is stored in memory location
lxi h,2000mov b,minx hmov a,mdcr b
loop: inx hcmp mjc aheadmov a,m
ahead: dcr bjnz loopsta 3000hlt
program simulation
6. PROGRAM FOR LARGEST NUMBER IN GIVEN ARRAY
-Get number of element as first number
-compare two number if 1st number is smaller than 2nd then carry will be set move the 2nd to acc, before jumping check for no carry
-or the 1st number will be in acc
-get the next number repeat the procedure
-finally the number in the acc is stored in memory location
lxi h,2000mov b,minx hmov a,mdcr b
loop: inx hcmp mjnc aheadmov a,m
ahead: dcr bjnz loopsta 3000hlt
7.SQUARE OF A NUMBER USING LOOKUP TABLE
lxi h,2000 //initialize look up table address
lda 3000 //get the datacpi 0ah //check the data is
> 9jc AFTER //if yes errormvi a,ffh //error indicationsta 3001jmp STOP
AFTER: mov c,amvi b,00dad bmov a,msta 3001 //store the result
STOP: hlt
8.ARRANGE DATA ARRAY IN ASCENDING ORDER
lxi h,2000mov c,mdcr c
REPEAT: mov d,clxi h,2001
LOOP: mov a,minx hcmp mjc SKIPmov b,mmov m,adcx hmov m,binx h
SKIP: dcr djnz loopdcr cjnz REPEAThlt
Program 9,10
• Pack the two unpacked BCD number stored in memory locations 2000h and 2001h assume the least significant digit is in stored at 2000h and store the result at 3000h, eg 06 and 07 as 67
• Two digit BCD number is stored in memory location 2000h. Unpacked the bcd number store in 3000h and 3001h such that 3000 will have lower BCD digit
LDA 2000RLCRLCRLCRLCANI F0HMOV C, ALDA 2001ADD CSTA 3000HLT
LDA 2000ANI F0HRRCRRCRRCRRCSTA 3000LDA 2000ANI 0FHSTA 3001HLT
11. FIND 2'S COMPLEMENT OF 16 BIT NUMBER
LDA 2000CMAADI 01HSTA 3000HLDA 2001CMAACI 00HSTA 3001HHLT
PROGRAM 12
• Write a program to find the number of negative elements and positive elements in a block of data. Length of the block is in 2000h and data start from 2001h, number of negative element in memory location 3000, and positive element in memory location 3001
LDA 2000HMOV C,AMVI B,00HMVI D,00HLXI H, 2001H
BACK: MOV A,MANI 80HJZ SKIPINR BJMP TOO
SKIP: INR DTOO: INX H
DCR CJNZ BACKMOV A,BSTA 3000HMOV A,DSTA 3001HHLT
Program 13.Write a program to count number of 1’s in given data (2000) and store the result in
memory location 3000hLDA 2000HMOV D,AMVI B,00MVI C,08
BACK: RARJNC SKIPINR B
SKIP: DCR CJNZ BACKMOV A,BSTA 3000HLT
Program 14, To find sum of series of even number from the list of numbers
LDA 2000HMOV C,AMVI B,00HLXI H,2001H
BACK: MOV A,MANI 01HJNZ SKIPMOV A,BADD MMOV B,A
SKIP: INX HDCR CJNZ BACKSTA 3000HLT
Program 15, program to separate even number from the given list of number and store them in the 3000h and sum in 300F
LXI H, 2001H
LXI D, 3000H
LDA 2000H
MOV C,A
BACK: MOV A,M
ANI 01H
JNZ SKIP
MOV A,M
STAX D
INX D
SKIP: INX H
DCR C
JNZ BACK
HLT
16. Write A Program To Search The Given Byte In The List Of Data And Store The Address of the in 2001 and 2002
LXI H, 3000HMOV B, MINX HLDA 2000HMOV C,A
BACK: MOV A,MCMP CJZ LASTINX HDCR BJNZ BACKLXI H, 0000HSHLD 2001HJMP END
LAST: SHLD 2001HEND: HLT
17.Write a program for matrix addition
LXI H, 2000H
LXI B, 2011H
LXI C, 3000H
BACK: LDAX B
ADD M
STAX D
INX H
INX B
INX D
MOV A, L
CPI 0AH
JNZ BACK
HLT
18.Write a program to generate Fibonacci number
LDA 2000H
LXI D, 2001
MOV H, A
MVI B, 00H
MOV A, B
STAX D
MVI C, 01H
MOV A, C
INX D
STAX D
BACK: MOV A,B
ADD C
INX D
STAX D
MOV B, C
MOV C, A
DCR H
JNZ BACK
HLT
37
Delays
• Each instruction passes through different combinations of Fetch, Memory Read, and Memory Write cycles.
• Knowing the combinations of cycles, one can calculate how long such an instruction would require to complete.– B for Number of Bytes– M for Number of Machine Cycles– T for Number of T-State.
38
Delays
• Knowing how many T-States an instruction requires, and keeping in mind that a T-State is one clock cycle long, we can calculate the time using the following formula:
Delay = No. of T-States / Frequency
• For example a “MVI” instruction uses 7 T-States. Therefore, if the Microprocessor is running at 2 MHz, the instruction would require 3.5 Seconds to complete.
39
Delay loops• We can use a loop to produce a certain amount of time
delay in a program.
• The following is an example of a delay loop:
MVI C, FFH 7 T-StatesLOOP DCR C 4 T-States
JNZ LOOP 10 T-States
• The first instruction initializes the loop counter and is executed only once requiring only 7 T-States.
• The following two instructions form a loop that requires 14 T-States to execute and is repeated 255 times until C becomes 0.
40
Delay Loops
• We need to keep in mind though that in the last iteration of the loop, the JNZ instruction will fail and require only 7 T-States rather than the 10.
• Therefore, we must deduct 3 T-States from the total delay to get an accurate delay calculation.
• To calculate the delay, we use the following formula:
Tdelay = TO + TL
– Tdelay = total delay
– TO = delay outside the loop
– TL = delay of the loop
• TO is the sum of all delays outside the loop.
• TL is calculated using the formula
TL = T X Loop T-States X N10
41
Delay Loops
• Using these formulas, we can calculate the time delay for the previous example:
• TO = 7 T-States
– Delay of the MVI instruction
• TL = (14 X 255) - 3 = 3567 T-States
– 14 T-States for the 2 instructions repeated 255 times (FF16 = 25510) reduced by the 3 T-States for the final JNZ.
• TDelay = (7 + 3567) X 0.5 Sec = 1.787 mSec
– Assuming f = 2 MHz
42
Using a Register Pair as a Loop Counter
• Using a single register, one can repeat a loop for a maximum count of 255 times.
• It is possible to increase this count by using a register pair for the loop counter instead of the single register.– A minor problem arises in how to test for the final
count since DCX and INX do not modify the flags.– However, if the loop is looking for when the count
becomes zero, we can use a small trick by ORing the two registers in the pair and then checking the zero flag.
43
Using a Register Pair as a Loop Counter
• The following is an example of a delay loop set up with a register pair as the loop counter.
LXI B, 1000H 10 T-StatesLOOP DCX B 6 T-States
MOV A, C 4 T-StatesORA B 4 T-StatesJNZ LOOP 10 T-States
44
Using a Register Pair as a Loop Counter
• Using the same formula from before, we can calculate:
• TO = 10 T-States
– The delay for the LXI instruction
• TL = (24 X 4096) - 3 = 98301 T- States
– 24 T-States for the 4 instructions in the loop repeated 4096 times (100016 = 409610) reduced by the 3 T-States for the JNZ in the last iteration.
• TDelay = (10 + 98301) X 0.5 mSec = 49.155 mSec
45
Nested Loops• Nested loops can be
easily setup in Assembly language by using two registers for the two loop counters and updating the right register in the right loop.– In the figure, the body
of loop2 can be before or after loop1.
Initialize loop 1
Update the count1
Body of loop 1
No
Body of loop 2
Update the count 2
No
Is thisFinal
Count?
Yes
Initialize loop 2
Is thisFinal
Count?
Yes
46
Nested Loops for Delay
• Instead Register Pairs, a nested loop structure can be used to increase the total delay produced.
MVI B, 10H 7 T-StatesLOOP2 MVI C, FFH 7 T-StatesLOOP1 DCR C 4 T-States
JNZ LOOP1 10 T-StatesDCR B 4 T-StatesJNZ LOOP2 10 T-States
47
Delay Calculation of Nested Loops
• The calculation remains the same except that it the formula must be applied recursively to each loop.– Start with the inner loop, then plug that delay in the calculation of
the outer loop.
• Delay of inner loop
– TO1 = 7 T-States
• MVI C, FFH instruction
– TL1 = (255 X 14) - 3 = 3567 T-States
• 14 T-States for the DCR C and JNZ instructions repeated 255 times (FF16 = 25510) minus 3 for the final JNZ.
– TLOOP1 = 7 + 3567 = 3574 T-States
48
Delay Calculation of Nested Loops
• Delay of outer loop– TO2 = 7 T-States
• MVI B, 10H instruction
– TL1 = (16 X (14 + 3574)) - 3 = 57405 T-States
• 14 T-States for the DCR B and JNZ instructions and 3574 T-States for loop1 repeated 16 times (1016 = 1610) minus 3 for the final JNZ.
– TDelay = 7 + 57405 = 57412 T-States
• Total Delay– TDelay = 57412 X 0.5 Sec = 28.706 mSec
49
Increasing the delay
• The delay can be further increased by using register pairs for each of the loop counters in the nested loops setup.
• It can also be increased by adding dummy instructions (like NOP) in the body of the loop.
Write a program to generate a delay of 04 sec if the crystal
frequency is 5MHz
Operating frequency is half of crystal frequency 2.5MHzTime for one T-state = .4 micro sec
Number of
T-state required = RT/Time for 1 T-state
=1000000
LXI B, count
BACK:DCX B
MOVA,C
ORGA B
JNZ BACK
52
The Stack
• The stack is an area of memory identified by the programmer for temporary storage of information.
• The stack is a LIFO structure.– Last In First Out.
• The stack normally grows backwards into memory.– In other words, the programmer
defines the bottom of the stack and the stack grows up into reducing address range.
Memory
Bottomof theStack
The Stackgrows backwardsinto memory
53
The Stack
• Given that the stack grows backwards into memory, it is customary to place the bottom of the stack at the end of memory to keep it as far away from user programs as possible.
• In the 8085, the stack is defined by setting the SP (Stack Pointer) register.
LXI SP, FFFFH
• This sets the Stack Pointer to location FFFFH (end of memory for the 8085).
54
Saving Information on the Stack
• Information is saved on the stack by PUSHing it on.– It is retrieved from the stack by POPing it off.
• The 8085 provides two instructions: PUSH and POP for storing information on the stack and retrieving it back.– Both PUSH and POP work with register pairs
ONLY.
55
The PUSH Instruction• PUSH B
– Decrement SP– Copy the contents of register B to the
memory location pointed to by SP– Decrement SP– Copy the contents of register C to the
memory location pointed to by SPB C
SPFFFFFFFEFFFDFFFCFFFB
F312
F312
56
The POP Instruction• POP D
– Copy the contents of the memory location pointed to by the SP to register E
– Increment SP– Copy the contents of the memory location
pointed to by the SP to register D– Increment SPD E
SP
FFFFFFFEFFFDFFFCFFFB
F312
F312
57
Operation of the Stack
• During pushing, the stack operates in a “decrement then store” style.– The stack pointer is decremented first, then the
information is placed on the stack.
• During poping, the stack operates in a “use then increment” style.– The information is retrieved from the top of the the
stack and then the pointer is incremented.
• The SP pointer always points to “the top of the stack”.
58
LIFO
• The order of PUSHs and POPs must be opposite of each other in order to retrieve information back into its original location.
PUSH BPUSH D...POP DPOP B
• Reversing the order of the POP instructions will result in the exchange of the contents of BC and DE.
59
The PSW Register Pair
• The 8085 recognizes one additional register pair called the PSW (Program Status Word).– This register pair is made up of the Accumulator and
the Flags registers.
• It is possible to push the PSW onto the stack, do whatever operations are needed, then POP it off of the stack.– The result is that the contents of the Accumulator and
the status of the Flags are returned to what they were before the operations were executed.
60
Subroutines
• A subroutine is a group of instructions that will be used repeatedly in different locations of the program.– Rather than repeat the same instructions several
times, they can be grouped into a subroutine that is called from the different locations.
• In Assembly language, a subroutine can exist anywhere in the code.– However, it is customary to place subroutines
separately from the main program.
61
Subroutines
• The 8085 has two instructions for dealing with subroutines.– The CALL instruction is used to redirect
program execution to the subroutine.– The RTE insutruction is used to return the
execution to the calling routine.
62
The CALL Instruction
• CALL 4000H– Push the address of the instruction immediately following the
CALL onto the stack
– Load the program counter with the 16-bit address supplied
with the CALL instruction.
PC
SPFFFFFFFEFFFDFFFCFFFB
2 0 0 3
0320
2000 CALL 40002003
63
The RTE Instruction
• RTE– Retrieve the return address from the top of
the stack– Load the program counter with the return
address.PC
FFFFFFFEFFFDFFFCFFFB
2 0 0 3
0320
4014 . . .4015 RTE SP
64
Cautions
• The CALL instruction places the return address at the two memory locations immediately before where the Stack Pointer is pointing.– You must set the SP correctly BEFORE using the
CALL instruction.
• The RTE instruction takes the contents of the two memory locations at the top of the stack and uses these as the return address.– Do not modify the stack pointer in a subroutine. You
will loose the return address.
65
Passing Data to a Subroutine
• In Assembly Language data is passed to a subroutine through registers.– The data is stored in one of the registers by the
calling program and the subroutine uses the value from the register.
• The other possibility is to use agreed upon memory locations.– The calling program stores the data in the memory
location and the subroutine retrieves the data from the location and uses it.
66
Call by Reference and Call by Value
• If the subroutine performs operations on the contents of the registers, then these modifications will be transferred back to the calling program upon returning from a subroutine.– Call by reference
67
Cautions with PUSH and POP
• PUSH and POP should be used in opposite order.
• There has to be as many POP’s as there are PUSH’s.– If not, the RET statement will pick up the wrong
information from the top of the stack and the program will fail.
• It is not advisable to place PUSH or POP inside a loop.
68
Conditional CALL and RTE Instructions
• The 8085 supports conditional CALL and conditional RTE instructions.– The same conditions used with conditional JUMP
instructions can be used.
– CC, call subroutine if Carry flag is set.– CNC, call subroutine if Carry flag is not set– RC, return from subroutine if Carry flag is set– RNC, return from subroutine if Carry flag is not set
Program to convert BCD to HEX
LXI H, 2000MOV A,MADD AMOV B,AADD AADD AADD BINX HADD MINX HMOV M,AHLT
Input: 2000 022001 092002 1D
Program to convert HEX to BCDLXI H,2000MVI D,00XRA AMOV C,M
LOOP2: ADI 01DAAJNC LOOP1INR D
LOOP1: DCR CJNZ LOOP2STA 2001MOV A,DSTA 2002HLT
INPUT:2000 0A2001 10
Program to convert HEX to ASCII LDA 2000MOV B,AANI 0FHCALL SUB1STA 2001MOV A,BANI F0HRLCRLCRLCRLCCALL SUB1STA 2002HLT
SUB1: CPI 0AHJC SKIPADI 07
SKIP: ADI 30RET
INPUT: 2000 E42001 342002 45
Program to convert ASCII to HEX
LDA 2000
SUI 30
CPI 0AHJC SKIP
SUI 07
SKIP: STA 2001
HLT
INPUT:
2000 45
2001 0E