UNIT-II MPMC

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

Unit II

Flow chart
 The thinking process described here and the steps necessary to write the program can be
represented in a pictorial format, called a flowchart.
 Generally, a flowchart is used for two purposes: to assist and clarify the thinking process
and to communicate the programmer's thoughts or logic to others
 Symbols commonly used in flowcharting are shown in Figure

Circle with an arrow: Represents continuation


(an entry or exit) to a different page
DATA TRANSFER (COPY) OPERATIONS

 One of the primary functions of the microprocessor is copying data, from a


register(or I/O or memory) called the source, to another register (or I/O or
memory) called the destination
 The contents of the source are not transferred, but are copied into the destination
register without modifying the contents of the source.
​
Several instructions are used to copy data. This section is concerned with the following
operations.

MOV: Move -Copy a data byte.

MVI: Move Immediate -Load a data byte directly.

OUT: Output to Port -Send a data byte to an output device.

IN: Input from Port -Read a data byte from an input device.

 The term copy is equally valid for input/output functions because the contents of the
source are not altered.
 However, the term data transfer is used so commonly to indicate the data copy function
that, these terms are used interchangeably when the meaning is not ambiguous.
 In addition to data copy instructions, it is necessary to introduce two machinecontrol
operations to execute programs.

HLT: Halt Stop processing and wait.

NOP: No Operation Do not perform any operation.

DATA MANIPULATIONOPERATIONS:

1. ARITHMETIC OPERATIONS

ADD: Add Add the contents of a register.*

ADI: Add Immediate Add 8-bit data.

SUB: Subtract Subtract the contents of a register.

SUI: Subtract Immediate Subtract 8-bit data.


INR: Increment Increase the contents of a register by 1.

DCR: Decrement Decrease the contents of a register by 1.

Addition
 The 8085 performs addition with 8-bit binary numbers and stores the sum in the
accumulator.
 If the sum is larger than eight bits (FFH), it sets the Carry flag.
 Addition can be performed either by adding the contents of a source register (B, C, D, E,
H, L, or memory) to the contents of the accumulator (ADD) or by adding the second byte
directly to the contents of the accumulator (ADI).
Subtraction

 The 8085 performs subtraction by using the method of 2's complement.


 Subtraction can be performed by using either the instruction SUB to subtract the contents
of a source register or the instruction SUI to subtract an 8-bitnumber from the contents of
the accumulator. In either case, the accumulator contents are regarded as the minuend
(the number from which to subtract).
The8085performs the following steps internally to execute the instruction SUB (or SUI).
Step1: Convertssubtrahend (the number to be subtracted) into its l's complement.
Step2:Adds 1 to l's complement to obtain 2's complement of the subtrahend. Step3:
Add 2's complement to the minuend (the contents of the accumulator). Step4:
Complementsthe Carry flag.

2. LOGIC OPERATIONS
 A microprocessor is basically a programmable logic chip.
 It can perform all the logic functions of the hard-wired logic through its instruction set.
 The 8085 instruction set includes such logic functions as AND, OR, Ex OR, and NOT
(complement). The opcodes of these operations are as follows:*

ANA: AND Logically AND the contents of A- register

ANI: AND Immediate Logically AND 8-bit data.

ORA: OR Logically OR the contents of A- register.

ORI: OR Immediate Logically OR 8-bit data.

XRA: X-OR Exclusive-OR the contents of A- register.

XRI : X-OR Immediate Exclusive-OR 8-bit data.

All logic operations are performed in relation to the contents of the accumulator.
OR, Exclusive-OR, and NOT

The instruction ORA (and ORI) simulates logic ORing with eight 2-input OR gates; this process
is similar to that of AND ing. The instruction XRA (and XRI)performsExc1usive-ORingof eight
bits and the instruction CMA invert the bits of the accumulator.

BRANCH OPERATIONS

 The branch instructions are the most powerful instructions because they allow the
microprocessor to change the sequence of a program, either unconditionally or under
certain test conditions.
 These instructions are the key to the flexibility and versatility of a computer.
 The microprocessor is a sequential machine; it executes machine codes from one memory
location to the next.
 Branch instructions instruct the microprocessor to go to a different memory location, and
the microprocessor continues executing machine codes from that new location.
 The address of the new memory location is either specified explicitly or supplied by the
microprocessor or by extra hardware.
 The branch instructions are classified in three categories:
1. Jump instructions
2. Call and Return instructions
3. Restart instructions

 The Jump instructions specify the memory location explicitly.

 They are 3-byte instructions: one byte for the operation code, followed by a 16-bit
memory address.

 Jump instructions are classified into two categories: Unconditional Jump and
Conditional Jump.

Unconditional Jump

The 8085 instruction set includes one unconditional Jump instruction. The unconditional Jump
instruction enables the programmer to set up continuous loops.

Example:
JMP 8500

Description
 This is a 3-byte instruction
 The second and third bytes specify the 16 bit memory address. However, the second byte
specifies the low-order and the third byte specifies the high-order memory address
Conditional Jumps

 Conditional Jump instructions allow the microprocessor to make decisions based on


certain conditions indicated by the flags.
 After logic and arithmetic operations, flip-flops (flags) are set or reset to reflect data
conditions.
 The conditional Jump instructions check the flag conditions and make decisions to
change or not to change the sequence of a program.
 Four flags used by the Jump instructions are
1. Carry flag2. Zero flag3. Sign flag4. Parity flag
Instruction:
All conditional Jump instructions in the 8085 are 3-byte instructions; the second byte specifies
the low-order (line number) memory address, and the third byte specifies the high-order (page
number) memory address.
Example:

JC 8500 - Jump on Carry (if result generates carry and CY=1)

JNC 8500 - Jump on No Carry (CY =0)

JZ 8500 - Jump on Zero (if result is zero and Z = 1)

JNZ 8500 - Jump on No Zero (Z =0)

Assembly language program for 8085 microprocessor:

1. 8 bit – Addition

LABLE MNEMONICS COMMENT


LDA 8200H Get first data in A register
MOV B,A Move A to B
LDA 8201H Get second data in A register
MVI C,00H Clear C register
ADD B Add B and A and store in A register
JNC AHEAD If carry is 0 go to AHEAD
INR C If carry is 1 increment C register
AHEAD STA 8202H Store the sum on memory
MOV A,C Move the content C to A register
STA 8203H Store the carry in memory
HLT halt program execution
2. 8 bit – Subtraction

LABLE MNEMONICS COMMENT


MVI C,00H Move the immediate data 00h into the C register
LDA 9000H Load the content of 9000h into A register
MOV B,A Copy the content of A to B
LDA 9001H Load the content of 9001H into A register
SUB B Subtract the content of B from the accumulator content

JNC L1 (800EH) Jump on to L1 , if there is no carry

INR C Increment the content of C reg by 1


L1 Store accumulator content in the memory
STA 8500H

MOV A,C Copy the content of C to A register


Store the accumulator content in the memory
STA 8501H

HLT halt program execution

3. 8 bit – Multiplication

LABLE MNEMONICS COMMENT


MVI D, 00H Move the immediate data 00h into the D register
LDA 8500H Load the content of 8500h into A register
MOV B,A Copy the content of A to B
LDA 8501H Load the content of 8501H into A register
MOV C,A Copy the content of A to C
XRA A Clear the accumulator
L2 ADD B Add the content of A with B
JNC L1(8010H) Jump on to L1 , if there is no carry
INR D Increment the content of D register by 1
L1 DCR C Decrement the content of C register by 1
JNZ L2(800BH) Jump on to L2 , if there is no 0
STA 9000H Store the accumulator content in the memory
MOV A,D Copy the content of D to A register
STA 9001H Store the accumulator content in the memory
HLT halt program execution
4. 8bit - Division

LABLE MNEMONICS COMMENT


MVI C, 00H Move the immediate data 00h into the C register

LDA 8500H Load the content of 8500h into A register


MOV B,A Copy the content of A to B
LDA 8501H Load the content of 8501H into A register
L2 CMP B Compare accumulator value with B value
JC L1(8012H) Jump on to L1 , if there is no carry
SUB B Subtract the content of B from the accumulator
content
INR C Increment the content of C register by 1
JMP L2 Jump on to L2, without any condition
L1 Store the accumulator content in the memory
STA 9000H

MOV A,C Move C to A register


Store the accumulator content in the memory
STA 9001H

HLT halt program execution

5. Ascending order
(Write an program to sort on array of data in the Ascending order. The array is stored in
the memory starting from 4200H the first element of the array gives the count value for
the number of elements in the array)

LABLE MNEMONICS COMMENT


LDA 4200H Load the count value in A-register.
MOV B, A Set count for N-1 repetition
DCR B of N-1comparison
LOOP 2 LXI H,4200H Set pointer for array
MOV C, M Setv count for N-1 comparisons
DCR C
INX H Increment the pointer
LOOP 1 MOV A, M get one data of array in A-register.
INX H
CMP M Compare the next data of array with content of A-
register.
JC AHEAD If content of A is Less than memory, then go to
AHEAD
MOV D, M If the content of A is greater than content of memory,
then exchange the content of memory pointed by HL
and previous memory location
MOV M,A
DCX H
MOV M , D
INX H
AHEAD DCR C
JNZ LOOP 1 Repeat comparison until C- count is zero
DCR B
JNZ LOOP 2 Repeat N-1 comparison until B- count is zero
HLT halt program execution

6. Descending order
(Write an program to sort on array of data in the Descending order. The array is stored in
the memory starting from 4200H the first element of the array gives the count value for
the number of elements in the array)
LABLE MNEMONICS COMMENT
LDA 4200H Load the count value in A-register.
MOV B, A Set count for N-1 repetition
DCR B of N-1comparison
LOOP 2 LXI H,4200H Set pointer for array
MOV C, M Setv count for N-1 comparisons
DCR C
INX H Increment the pointer
LOOP 1 MOV A, M get one data of array in A-register.
INX H
CMP M Compare the next data of array with the content of A-
register.
JNC AHEAD If content of A is greater than content of memory
addressed by HL pair, then go to AHEAD
MOV D, M If the content of A is less than content of memory
addressed by HL pair, then exchange content of
memory pointed by HL and previous memory location
MOV M,A
DCX H
MOV M , D
INX H
AHEAD DCR C
JNZ LOOP 1 Repeat comparison until C- count is zero
DCR B
JNZ LOOP 2 Repeat N-1 comparison until B- count is zero
HLT halt program execution
7. Search for Smallest data in an array

(Write an assembly language program to search the smallest data in an array of N data stored
in memory from 4200H to (4200H+N). The first element of the array gives the number of
data in the array)

LABLE MNEMONICS COMMENT


LXIH, 4200H set pointer for array
MOV B, M set count for no. of elements in array
INX H
MOV A , M Set first element of array as smallest data
DCR B Decrement the count
LOOP INX H
CMP M Compare an element of array with current smallest
data
JC AHEAD If CF =1, go to AHEAD
MOV A, M If CF =0, Then content of memory is smaller than A-
register. Hence, if CF = 0, make memory as smallest
by moving to A - register
AHEAD DCR B
JNZ LOOP Repeat comparison until count is zero
STA 4300H Store the smallest data in memory
HLT halt program execution

8. Search for Largest data in an array


(write an assembly language program to search the largest data in an arrayof N data stored in
memory from 4200H to (4200H+N). The first element of the array gives the number of data
in the array)

LABLE MNEMONICS COMMENT


LXIH, 4200H set pointer for array
MOV B, M set count for no. of elements in array
INX H
MOV A , M Set first element of array as smallest data
DCR B Decrement the count
LOOP INX H
CMP M Compare an element of array with current smallest
data
JC AHEAD If CF =0, go to AHEAD
MOV A, M If CF =1, Then content of memory is larger than A-
register. Hence, if CF = 1, make memory content as
current largest by moving it to A - register
AHEAD DCR B
JNZ LOOP Repeat comparison until count is zero
STA 4300H Store the largest data in memory
HLT halt program execution

9. Square root of 8-bit binary number

(Write an assembly language program to find the Square root of an 8-bit binary number.The
binary number is stored in memory location 4200H and store the square root in 4201H.)

LABLE MNEMONICS COMMENT


LDA 4200H Get the given data(Y) in A-register
MOV B, A Save the data in B-register
MVI C, 02H Get the deviser (02H) in C-register
CALL DIV Call division subroutine to get initial value (X) in the
D- register.
REP MOV E, D Save the initial value in E- register
MOV A, B Get the dividend (Y) in A-register
MOV C, D Get the divisor (X) in C-register
CALL DIV Call division subroutine to get (Y/X) in D- register.
MOV A, D Move (Y/X) in A- register.
ADD E Get ((Y/X) / X) in A- register.
MVI C, 02H Get the deviser (02H) in C-register
CALL DIV Call division subroutine to get XNEW in the D-
register.
MOV A, E Get X in A- register.
CMP D Compare X and XNEW
JNZ REP If XNEW is not equal to X, then repeat.
STA 4201H save the square root in memory
HLT halt program execution

;division subroutine
DIV MVI D, 00H Clear the D- register for quotient
NEXT SUB C Subtract the divisor from dividend
INR D Increment the quotient
CMP C Repeat subtraction until the divisor is less than
JNC NEXT dividend.
RET Return to main program
10. Find the square of given number

(Find the square of the given numbers from memory location 6100H and store the result from
memory location 7000H)

LABLE MNEMONICS COMMENT


LXI H,6200H Initialize lookup table pointer
LXI D, 6100H Initialize source memory pointer
LXI B, 7000H Initialize Destination memory pointer
BACK LDAX D Get the number
MOV L,A A point to the square
MOV A, M Get he square
STAX B Store the result at destination memory location
INX D Increment source memory pointer
INX B Increment destination memory pointer
MOV A, C
CPI 05H Check for last number
JNZ BACK If not repeat
HLT halt program execution

Write short notes on look up table and its usage. (6)

 Lookup table is an array that replaces runtime computation with a


simpler array indexing operation.
 The savings in terms of processing time can be significant, since
retrieving a value from memory is often faster than undergoing an
'expensive' computation or input/output operation.
 The tables may be pre-calculated and stored in static program storage,
calculated (or "pre-fetched") as part of a program's initialization phase
(memorization), or even stored in hardware in application-specific
platforms.
 Lookup tables are also used extensively to validate input values by
matching against a list of valid (or invalid) items in an array.
2.5 LOOPING,COUNTING AND INDEXING
LOOPING:
The programming technique used to instruct the microprocessor to repeat tasks is
called looping.This task is accomplished by using jump instructions.
Classification Of Loops:
1.continuous loop
2.Unconditional loop
Continuous Loop:
Repeats a task continuously.A continuous loop is set up by using the unconditional
jump instruction.A program with a continuous loop does not stop repeating the tasks until
the system is reset.
Conditional Loop:
A conditional loop is set up by a conditional jump instructions. These instructions
check flags(Z,CY,P,S) and repeat the tasks if the conditions are satisfied. These loops
include counting and indexing.
Conditional Loop And Counter:
 A counter is a typical application of the conditional loop.
 A microprocessor needs a counter,flag to accomplish the looping task.
 Counter is set up by loading an appropriate count in a register.
 Counting is performed by either increment or decrement the counter.
 Loop is set up by a conditional jump instruction.
 End of counting is indicated by a flag.
Conditional Loop,Counter And Indexing:
Another type of loop which includes counter and indexing .
Indexing:
Pointing of referencing objects with sequential numbers. Data bytes are stored in
memory locations and those data bytes are referred to by their memory locations.
Example:
Steps to add ten bytes of data stored in memory locations starting ata given location
and display the sum.
The microprocessor needs
 A counter to count 10 data bytes.
 An index or a memory pointer to locate where data bytes are stored.
 To transfer data from a memory location to the microprocessor(ALU)
 To perform addition
 Registers for temporary storage of partial answers
 A flag to indicate the completion of the stack
 To store or output the result.

Figure 2.5.1 Looping flow chart


1. The initialization section establishes the starting values of loop counters for
counting how many times loop is executed, Address registers for indexing
which give pointers to memory locations and other variables.
2. The actual data manipulation occurs in the processing section. This is the section
which does the work.
3. The loop control section updates counters, indices (pointers) for the next iteration.
4. The result section analyzes and stores the results.

The processor executes initialization section and result section only once, while it
may execute processing section and loop control section many times. Thus, the execution
time of the loop will be mainly dependent on the execution time of the processing section
and loop control section. The flowchart 1 shows typical program loop. The processing
section in this flowchart is always executed at least once. If you position of the processing
and loop control section then it is possible that the processing section may not be executed
at all, if necessary.
LOGIC OPERATIONS : ROTATE AND COMPARE

ROTATE
Rotate accumulator left

 RLC none Each binary bit of the accumulator is rotated left by one position.
Bit D7 is placed in the position of D0 as well as in the Carry flag. CY is
modified according to bit D7. S, Z, P, AC are not affected.
 Example: RLC

LOGIC OPERATIONS :ROTATE AND COMPARE


Rotate accumulator right

 RRC none Each binary bit of the accumulator is rotated right by one position. Bit D0 is
placed in the position of D7 as well as in the Carry flag. CY is modified according to bit
D0. S, Z, P, AC are not affected.

 Example: RRC

LOGIC OPERATIONS :ROTATE

Rotate accumulator left through carry

 RAL none 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 D0. CY is modified according to bit D7. S, Z, P, AC are not affected.
 Example: RAL
LOGIC OPERATIONS :ROTATE (CONT.)
Rotate accumulator right through carry

 RAR none Each binary bit of the accumulator is rotated right by one position
through the Carry flag. Bit D0 is placed in the Carry flag, and the Carry flag is
placed in the most significant position D7. CY is modified according to bit D0. S,
Z, P, AC are not affected.

 Example: RAR
LOGICALOPERATIONS
⚫ Compare
⚫ Compare the contents of a register or memory location with the contents of the
accumulator.
⚫ CMP R/M :Compare the contents of the register or memory location to the contents
of the accumulator.
⚫ CPI # : Compare the 8-bit number to the contents of the
accumulator.

 The compare instruction sets the flags (Z, Cy, and S).
 The compare is done using an internal subtraction that does not change the contents of the
accumulator.

 A –(R / M / #)

BRANCHOPERATIONS
Two types:
Unconditional branch: Go to a new location no matter what.
Conditional branch : Go to a new location if the condition is true.
UnconditionalBranch
JMP Address : Jump to the address specified (Go to).
CALL Address: Jump to the address specified but treat it as a
subroutine.
RET : Return from a subroutine.
The addresses supplied to all branch operations must be 16-bits.

CONDITIONAL BRANCH
◾ Go to new location if a specified condition is met.
◾ JZ Address(Jump on Zero)Go to address specified if the Zero
flag is set.
◾ JNZ Address(Jump on NOT Zero)Go to address specified if the Zero flag is not set.
◾ JC Address(Jump on Carry)Go to the address specified if the
Carry flag is set.
◾ JNC Address(Jump on No Carry)Go to the address specified if the Carry flag is not set.
◾ JP Address(Jump on Plus)Go to the address specified if the
Sign flag is not set
◾ JM Address(Jump on Minus)Go to the address specified if the Sign flag is set.
DATAFORMATS

▣ In an 8-bit microprocessor, data can be represented in one of four


formats:
▣ ASCII
▣ BCD
▣ Signed Integer
▣ Unsigned Integer.
▣ It is important to recognize that the microprocessor deals with 0’s
and 1’s.It deals with values as strings of bits.
▣ It is the job of the user to add a meaning to these strings.

DATAFORMATS
⚫ Assume the accumulator contains the following value: 0100
0001.There are four ways of reading this value: It is an unsigned
integer expressed in binary, the equivalent decimal number would be
65.
⚫ It is a number expressed in BCD (Binary Coded Decimal) format.
That would make it, 41.
⚫ It is an ASCII representation of a letter. That would make it the
letter A.
⚫ It is a string of 0’s and 1’s where the 0th and the 6th bits are set to 1
while all other bits are set to 0.

You might also like