4CS3 MPI Unit 3
4CS3 MPI Unit 3
4CS3 MPI Unit 3
(4CS3-04)
UNIT 3
MVI B, YY ADD XX
2 8002 06, YY Move immediately to [B] the data
B/SUB B STA
YY
3 8004 80/(90)
F000 Add(or subtract) [B] with [A]
4 8005 32,00,F0
JNC (Ahead 1) Store contents of [A] in specified
D2,10,80
5 8008 location
3E,01 MVIA, 01
Jump on no carry to specified
6 800B
C3,12,80 JMP(Ahead 2) location
7 800D Move immediately to [A] the
3E,00 MVIA, 00
data(01)H
8 8010 STA F001 Jump unconditionally ahead
32,01,F0
RST1
9 8012
Move immediately to [A] the
CF Ahead 1
10 8015 data(00)H
Ahead 2 Store contents of [A] in specified
location
Restart
INPUT : XX, YY
1) 2B, 1A
2) 2B, FA
OUTPUT
2 8001 2A, 00, F0 LHLD F000 XCHG Load memory from specified
locations
3 8004
EB LHLD F100 DAD D Exchange contents of HL & DE
4 8005 pairs
SHLD F200
2A,00,F1 Load memory from specified
5 8008 locations
19 JNC (Ahead) INR A
Add HL & DE pairs
6 8009 22,00,F2
STA F202 RST1
7 800C D2,10,80 Store memory to specified
3C locations
8 800F Jump on no carry to ahead
32,02,F2
address
9 8010
CF Increment contents of
LXIB F100 LXID Load BC pair with specified data Load DE pair
2 8003 01,00,F1
F200 MVI A, (NN) with specified data Specify the number of bytes
3 8006 11,00,F2
STA 8100 XRA A Save the count
4 8009 3E,(NN) LDAX B ADC M
Clear accumulator and flags
5 800B 32,00,81 STAX D INX H
AF 0A LO O P INX B INX D Load [A] indirectly from address specified by
6 800E
BC pair
LDA 8100 DCR
8E Add memory to [A] with carry
7 800F
A
8 8010 12
Store contents of [A] indirectly in location
JNZ (LO O P) specified by DE pair Update memory
9 8011 23
JNC (Ahead 1)
03 Update BC register pair Update DE
10 8012
MVI A, 01
11 8013 13 register pair
JMP (Ahead 2)
12 8014 3A,00,81
MVI A, 00 STAX Load [A] from specified location Decrement
3D C2,0B,80
13 8015 D RST 1
D2,24,80 3E,01 count
14 8018
C3,26,80 3E,00
Jump on no zero to perform loop Jump on no
15 8019
12
16 801C carry to ahead address
CF
17 801F Move immediately to [A] the data(01) H
Jump unconditionally to ahead address
18 8021
Ahead 1
Ahead 2
INPUT
8100:810:8102:8103:8104:8105:8106:8107:8108:8109:810A:810B:810C:810D:810E:810F
00 : 01: 02 : 03 : 04 : 05 : 06 : 07 : 08 : 09 : 0A : 0B : 0C : 0D : 0E : 0F
OUTPUT
8200:8201:8202:8203:8204:8205:8206:8207:8208:8209:820A:820B:820C:820D:820E:820F
00 : 01: 02 : 03 : 04 : 05 : 06 : 07 : 08 : 09 : 0A : 0B : 0C : 0D : 0E : 0F
6) PROGRAM TO BLOCK TRANSFER IN REVERSE ORDER
INPUT
8100:810:8102:8103:8104:8105:8106:8107:8108:8109:810A:810B:810C:810D:810E:810F
00 : 01: 02 : 03 : 04 : 05 : 06 : 07 : 08 : 09 : 0A : 0B : 0C : 0D : 0E : 0F
OUTPUT
820F:820E:820D:820C:820B:820A:8209:8208:8207:8206:8205:8204:8203:8202:8201:8200
0F : 0E : 0D : 0C : 0B : 0A : 09 : 08 : 07 : 06 : 05 : 04 : 03 : 02 : 01 : 00
7) PROGRAM TO FIND 2`S COMPLEMENT OF 8 BIT/ 16 BIT NUMBER
1 8000 06,01/(02) MVI B,01/(02) Move to [B] (01) for 8-bit & (02) for 16-bit
complement of data Load DE pair with
2 8002 11,01,00
LXID 0001 specified data
3 8005 21,00,F0 LXIH F000
Load HL pair with specified data Copy
4 8008 7E
MOV A,M
LOOP contents of memory to [A]
5 8009 2F
CMA Complement contents of [A]
6 800A 77
Copy contents of [A] to memory
MOV M,A
7 800B 05 Decrement [B] by one
DCR B Jump on zero to ahead address Update
8 800C CA, 13,80
JZ (Ahead) memory
9 800F 23
INX H
10 8010 C3,08,80 Jump unconditionally to perform loop
MVI C, 15H
LOOP DCR C
JNZ LOOP
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.
Using a Register Pair as a Loop Counter
The following is an example of a loop set up with
a register pair as the loop counter.
LXI B, 1000H
LOOP DCX B
MOV A, C
ORA B
JNZ LOOP
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.
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-States
LOOP 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.
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
• Total Delay
– TDelay= 57412 X 0.5 μSec = 28.706 mSec
Increasing the delay
PUSH B
PUSH D
...
POP D
POP B
Conditional CALL
RESTART, CONDITIONAL CALL
& RETURN INSTRUCTIONS
Conditional RETURN
A Proper Subroutine
C00C: OUT 00
C00E: CALL 1420
C011: JMP C003
Writing Subroutines
DELAY: C014: MVIB, FF
C016: MVIC, FF
C018: DCR C
C019: JNZ C018
C01C: DCR B
C01D: JNZ C016
C020: RET
Nesting Subroutines
Macros
• Macro is a group of instructions. The Macros in Microprocessor
assembler generates the code in the program each time where the
macro is ‘called’. Macros can be defined by MACRO and ENDM
assembler directives. Creating macro is very similar to creating a
new opcode that can be used in the program.