0% found this document useful (0 votes)
19 views

ALP examples

Uploaded by

beautart00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

ALP examples

Uploaded by

beautart00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

DATA TRANSFER INSTRUCTION

instructions, which are used to transfer data from one register to


another register, from memory to register or register to memory are
Data Transfer Instruction. when data transfer instructions is executed,
data is transferred from the source to the destination without altering
the contents of the source.

Opcod
Operand Description
e

Rd,Rs
MOV M,Rs Copy from source to destination
Rd,M

Rd,Data
MVI Move immediate 8-bit
M,Data

LDA 16-bit address Load Accumulator

LDAX B/D Register Pair Load accumulator indirect

Register pair, 16-


LXI Load register pair immediate
bit data

LHLD 16-bit address Load H-L registers direct

STA 16-bit address Store accumulator direct


STAX Register pair Store accumulator indirect

SHLD 16-bit address Store H-L registers direct

XCHG None Exchange H-L with D-E

Copy H-L pair to the Stack Pointer


SPHL None
(SP)

XTHL None Exchange H–L with top of stack

Load program counter with H-L


PCHL None
contents

PUSH Register pair Push register pair onto stack

POP Register pair Pop stack to register pair

Copy data from accumulator to a


OUT 8-bit port address
port with 8- bit address

Copy data to accumulator from a


IN 8-bit port address
port with 8- bit address

Top

1) MOV instruction:
This instruction copies the contents of the source register into
the destination register. The contents of the source register are not
altered.If one of the operands is a memory location, its location is
specified by the contents of the HL registers.These instructions are
of one byte instruction and no flag is affected in these instructions.

(i) MOV reg1, reg2 (Moves to register from register)

where reg = A, B, C, D, E, H or L

This instruction is moves / copies the data in the given register to the
given register.
i.e

[reg1] < - [reg2]

(ii) MOV reg, M (Moves to register from memory)

This instruction is indirect read instruction. It moves / copies the data


stored in memory location whose address is given in H-L register pair,
to the given register.
i.e

[reg] < - [MHL]

(iii) MOV M, reg (Moves to memory from register)

This instruction moves / copies the data in the given register to the
memory location addressed by H-L register pair.
i.e

[MHL] < - [reg]

For example let D = 4E H, H = 23 H, L = 00 H Then after execution of


the instruction MOV M, D will produce the result:
[M2300] < - [4E]

M-
Operand Bytes Flags Affected T-States
Cycles

Rd,Rs 1 None 1 4

Rd,M 1 None 2 7

M,Rs 1 None 2 7

2) MVI instruction:

This an instruction to load register with 8-bit value. This


instruction uses immediate addressing for specifying the data.If the
operand is a memory location, its location is specified by the contents
of the H-L registers.These instructions are two byte instructions and no
flag is affected.

(i) MVI reg, 8 bit data (Moves 8 bit data to register)

This instruction transfers the given data to the register.


i.e

[reg] < - [8 bit data]

(ii) MVI M, 8 bit data (Moves 8 bit data to memory)

This instruction transfers the given data to the memory location


pointed by H-L pair.
i.e

[MHL] < - [8 bit data]


For example the given data 2BH will be copied into the memory
location whose address is given in H-L register pair. If H = 21 H, L = 00 H
then,

MVI M,2B

[M2100] < - [2B]

M-
Operand Bytes Flags Affected T-States
Cycles

Rd,data 2 None 2 7

M,data 2 None 3 10

3) LDA instruction:

This is mnemonic for Loads the accumulator direct. It transfers


the content stored in the addressed memory location (given by
address) to accumulator. No flag is affected in this instruction. It
is three byte instruction.

[A] < - [Maddress]

For example if 2A H data is stored in memory location 2500H before the


execution of

LDA 2500H

instruction, then after the execution of this instruction, the data


2AH will be transferred to accumulator.

[A] < - [2A]


Operand Bytes Flags Affected M-Cycles T-States

16-bit address 3 None 4 13

4) LDAX instruction:

This is mnemonic for Loads the Accumulator Indirect. the


contents already stored in the memory location addressed by the
register pair (rp). Here rp represents B-C or D-E register pair.

The H-L register pair is not included in this instruction.the


instruction LDAX H does not exist, because the contents stored in the
memory location addressed by H-L register pair may be loaded to
accumulator by the instruction MOV A, M.The possible combinations of
the instruction are:

LDAX B

LDAX D

For example, if [D] = 25 H, [E] = 00 H and M 2500 H = 34 H, then after the


execution of the instruction LDAX D, the accumulator will have:

[A] < - [M2500] , A = 34 H

Operand Bytes Flags Affected M-Cycles T-States

B/D Register Pair 1 None 2 7

5) LXI instruction:

This is mnemonic for Load eXtended register Immediate. Here, a


register pair is termed as extended register. It is an instruction that
loads register pair 'rp' with the 16-bit data. This instruction uses
immediate addressing for specifying the data.

where rp = BC,DE,HL

i.e LXI rp,16 bit data

For example if you want to load HL with F850 H then,

LXI H,F850H

Operand Bytes Flags Affected M-Cycles T-States

Reg. pair,16-bit
3 None 3 10
data

6) LHLD instruction:

This is mnemonic for Loads the H-L pair direct.This instruction


loads the H-L pair direct with two bytes already stored in two
consecutive memory locations starting at the specified memory
address. The contents stored in the memory location whose address is
given with the instruction will be loaded to the L-register; and the
contents stored in the next memory location (address + 1) will be
loaded to the H-register.

LHLD 2100 H

after the execution of the instruction LHLD 2100 H , the L-register will
have 2A H and H-register will have 2B H.

Operand Bytes Flags Affected M-Cycles T-States

16-bit address 3 None 5 16


7) STA instruction:

This is mnemonic for Stores the accumulator direct.It transfers


the content stored in the accumulator to addressed memory location
(given by address).

16H data is stored in the accumulator before the execution of STA


2100H instruction

STA 2100 H

after the execution of this instruction, the data 16H will be


transferred to the addressed memory location.

[Maddress] < - [A]

Operand Bytes Flags Affected M-Cycles T-States

16-bit address 3 None 4 13

8) STAX instruction:

This is mnemonic for Stores the Accumulator Indirect.The STAX


rp instruction does the reverse operation of LDAX rp. This instruction
stores the accumulator contents in the memory location addressed by
the register pair (rp). Here too rp represents B-C or D-E register pair.

The combination STAX H is not included in this instruction as


MOV A, M performs the same operation.The possible combination of
this instruction are:

STAX B
STAX D

For example, if B = 21 H, C = 00 H and A = 3A H,

STAX B

after the execution of the instruction STAX B, 3A H will be stored in the


memory location 2100 H.

[Mrp] < - [A]

Operand Bytes Flags Affected M-Cycles T-States

B/D Register Pair 1 None 2 7

9) SHLD instruction:

This is mnemonic for Stores the H-L pair direct.This instruction


does the reverse operation of LHLD. The instruction SHLD address
stores the contents of L-register to memory location whose address is
given with the instruction; and the contents of H-register are stored in
the next consecutive memory location (address + 1).

For example, if [L]= 3A H and[H] = 3B H,

SHLD 2200 H

after the execution of the instruction SHLD 2200 H will result.

[M2200H] < - 3A

[M2201H] < - 3B

Operand Bytes Flags Affected M-Cycles T-States


16-bit address 3 None 5 16

10) XCHG instruction:

This is mnemonic for Exchange the contents of H-L register with


D-E register.This is one byte instruction and no operand is needed with
it. It exchanges the contents of H and L register with D and E registers
respectively.

For example:If H = 25 H , L = 32 H and D = 12 H, E = 1B H,

XCHG

after the execution of XCHG instruction, we have:H = 12 H, L = 1B H and


D = 25 H, E = 32 H.

[H] < - > [D] and [L] < - > [E]

M-
Operand Bytes Flags Affected T-States
Cycles

None 1 None 1 4

11) SPHL instruction:

This is mnemonic for Copies HL to Stack Pointer SP.This is also


one byte instruction as no operand is used. The SPHL instruction copies
the contents of H-register to high order byte stack pointer (SP) and the
contents of L-register to low order byte of stack pointer (SP).

For example if H = 23 H and L = 45 H and SP = 2501 H,

SPHL
after the execution of the instruction SPHL will result SP = 2345 H.

[SP] < - > [HL] i.e. [SPH] < - > [H] and [SPL] < - > [L]

M-
Operand Bytes Flags Affected T-States
Cycles

None 1 None 1 6

12) XTHL instruction:

This is mnemonic for Exchanges the top of the stack with H-L
register pair.The XTHL is one byte instruction and does not require any
operand. The top byte of the stack is exchanged with L-register and
next byte of the stack is exchanged with H-register.

M-
Operand Bytes Flags Affected T-States
Cycles

None 1 None 5 16

For example if H = 21 H, L = 02 H and MSP =1A H, MSP+1 = 2C H,

XTHL

then after the execution of the instruction XTHL will result:

H = 2C H, L = 1A H and MSP = 02 H, MSP+1 = 21 H

as shown in figure

13) PCHL instruction:


This is mnemonic for Copies H-L to program counter (PC).This is
one byte instruction and no operand is needed with this instruction. It
copies the contents of H-register to high-order byte of the program
counter (PC) and the contents of L-register to low order byte of the
program counter.

For example if PC = 2106 H and HL = 2500 H,

PCHL

after the execution of the instruction PCHL will result: PC = 2500 H.

[PC] < - > [HL] i.e. [PCH] < - > [H] and [PCL] < - > [L]

M-
Operand Bytes Flags Affected T-States
Cycles

None 1 None 1 6

14) PUSH instruction:

This instruction use to push the contents of registers to the stack.


In the stack not only the return address of the main program during the
execution of CALL address instruction is pushed but also the contents of
registers may also be pushed to stack.

where rp = BC,DE,HL,PSW

PUSH rp

T-
Operand Bytes Flags Affected M-Cycles
States

Register pair 1 None 3 12


For example we have SP = 2500 H, HL = 5678 H,

PUSH H

After the execution of PUSH H instruction 56 H will be loaded to


memory location 24FF H and 78 H will be loaded to 24FE H memory
location.

as shown in figure

PSW :Here AF is register pair formed with Flag and Accumulator


register and also known as PSW(Processor Status Word).In PSW,
Accumulator is the MS Byte, and Flags register is the LS Byte.
15) POP instruction:

This instruction use To retrieve the contents of registers from the


stack.

where rp = BC,DE,HL,PSW

POP rp

T-
Operand Bytes Flags Affected M-Cycles
States

Register pair 1 None 3 10

For example the data saved in stack is shown in figure and A = 01 H, F =


00 H,D = 12 H, E = 6E H,
POP PSW

POP D

After the execution of these instructions we have the following


data in the registers.F = 59 H, A = 24 H, E = 36 H, D = 9F H. Now 2500 H
will be the new value of stack pointer.

16) OUT instruction:

This is mnemonic for Outputs the data to the port.This two byte
output instruction is used to send the contents of accumulator to the
specified port.

OUT port

Output < - [A]

Operand Bytes Flags Affected M-Cycles T-States

8-bit port
2 None 3 10
address

17) IN instruction:

This is mnemonic for Inputs the data from the port.This


instruction is basically used to read the data from the input devices
such as data read from key board, switches etc. during the computer
run.

IN port

[A] < - data from port


Operand Bytes Flags Affected M-Cycles T-States

8-bit port
2 None 3 10
address

1). Write assembly language program to subtract two 8-bit numbers


and Display Borrow.
Solution:

MVI A,A9H // Loads the A9h to Accumulator


MVI B,ABH // Loads the ABh to B-register
MVI C,00H // Loads the 00h to C-register
SUB B // Subtract the content of B-
register from Accumulator and result stored in Accumulator.
JNC SKIP // Jump on no Carry to SKIP
INR C // Increment C-register
CMA // Complements Accumulator content
INR A // Increment Accumulator
SKIP: STA 2100H // Stores the answer to memory
location 2100 H
MOV A,C // moves the content of C-register
to accumulator (A < - C)
HLT // Stop processing

2). Write assembly language program to find number of 1's and 0's in 8-
bit number stored at 2010 memory address.
Solution:

LDA 2010H // Loads the content of 2010H into


accumulator
MVI C,08H // Loads the 08h to C-register
MVI D,00H // Loads the 00h to D-register
MVI E,00H // Loads the 00h to E-register
START: RRC // All the bits of accumulator are
shifted or rotated right. The CY flag is modified as LSB.
JNC ZERO // Jump on no Carry(CY=0) to ZERO
JC ONE // Jump on Carry(CY=1) to ONE
ONE: INR D // Increment D-register to count
number of 1's
JMP END // Jump to END
ZERO: INR E // Increment E-register to count
number of 0's
JMP END // Jump to END
END: DCR C // Decrement C-register
JNZ START // Jump on no Zero(Z=0) to START
HLT // Stop processing

# ORG 2010H // Set origin to dataset


# DB ADH // initializes memory with one or more
byte values

3). Write assembly language program to find a factorial of a given


number.
Solution:

LXI H,2501H // Loads the address of the


number in H-L register pair
MOV B,M // Moves the content of memory to
B-register
MVI A,00H // Loads the 00h to accumulator
MOV D,B // Moves the content of B-register
to D-register
DCR B // Decrement B-register.
START: JZ CNT // Jump on ZERO(Z=1) to CNT
MOV E,B // Moves the content of B-register
to E-register
MUL: ADD D // Adds the contents of D-register
with the contents of accumulator and the answer is stored in
Accumulator
DCR E // Decrement E-register.
JNZ MUL // Jump on no ZERO(Z=0) to MUL
MOV D,A // Moves the content of Accumulator
to D-register
MVI A,00H // Loads the 00h to Accumulator
DCR B // Decrement B-register
JMP START // Jump to START
CNT: MOV A,D // Moves the content of D-register
to Accumulator
HLT // Stop processing

# ORG 2010H // Set origin to dataset


# DB 05H // initializes memory with one or more
byte values

4). Write assembly language program to Load the contents of memory


locations 2100 H and 2101 H in B-register and C-register respectively.
The content of memory locations 2100 H and 2101H are 16 H and 19 H
respectively.
Solution:

LDA 2100H // Loads the content of 2100H into


accumulator
MOV B,A // moves the content of accumulator to
B-register (B < - A)
LDA 2101H // Loads the content of 2101H into
accumulator
MOV C,A // moves the content of accumulator to
C-register (C < - A)
HLT // Stop processing

# ORG 2100H // Set origin to dataset


# DB 16H,19H // initializes memory with one or
more byte values

5). Write an assembly language program to find the 2's complement of


a hexadecimal number. The hexadecimal number 6A H is stored in
memory location 2100H and the answer is to be stored in 2101 H.
Solution:

LDA 2100H // Loads the content of 2100H into


accumulator
CMA // Complements the accumulator content
(1’s complement)
INR A // 1 is added to the accumulator
content to get the 2’s complement
STA 2101H // Loads the accumulator contents into
memory location 2101 H
HLT // Stop processing

# ORG 2100H // Set origin to dataset


# DB 6AH // initializes memory with one or more
byte values

6). Write an assembly language program to add two numbers (decimal)


38 and 64, then subtract decimal number 3 from the sum. The final
answer is to be stored in memory location 2100 H.
use Converter to convert decimal to hexadecimal number

Solution:

MVI A,26H // Loads the first number to


accumulator
MVI B,40H // Loads the second number to B-
register
MVI C,03H // Loads the third number to C-
register
ADD B // Adds the contents of B-register
with the contents of accumulator and the answer is stored in
A
SUB C // Content of C gets subtracted from
accumulator and difference is stored in A
STA 2100H // Answer is stored in 2100 H location
HLT // Stop processing

7). Write an assembly language program to interchange (swap) the


contents of two memory locations 2100 H and 2101 H.
Solution:

LDA 2100H // Loads the content of 2100H into


accumulator
MOV B,A // Moves the content of accumulator to
B-register
LDA 2101H // Loads the content of 2101H into
accumulator
STA 2100H // Loads the accumulator content to
2100 H location
MOV A,B // Moves the content of B-register to
accumulator
STA 2101H // Loads the accumulator content to
2101 H location
HLT // Stop processing

# ORG 2100H // Set origin to dataset


# DB 05H,07H // initializes memory with one or
more byte values

8). Write an assembly language program to multiply two decimal


numbers 23 and 9 and store the answer in some memory location.
use Converter to convert decimal to hexadecimal number

Solution:

MVI A,00H // Loads the accumulator with 00H


MVI B,17H // Loads B-register with 17H
MVI C,09H // Loads C-register with 09H
AGAIN: ADD B // Adds the content of B-register
to accumulator
DCR C // Decrements C-register
JZ END // Checks for zero; if zero jump to
END
JMP AGAIN // Repeats the addition
END: STA 2100H // Stores the answer to memory
location 2100 H
HLT // Stop processing

9). Write an assembly language program to find the smaller of two


numbers stored in memory locations 2501 H and 2502 H. Store the
result in 2503 H memory location.
Solution:
LXI H,2501H // Loads the address of the first
number in H-L register pair
MOV A,M // Saves the first number in
accumulator
INX H // Increments the H-L register
pair
CMP M // compares the two numbers
JC NEXT // if carry smaller number is in
accumulator go to NEXT
MOV A,M // If no carry then move the
smaller number to accumulator
NEXT: STA 2503H // Stores the smaller number in
2503 memory location
HLT // Stop processing

# ORG 2501H // Set origin to dataset


# DB 12H,34H // initializes memory with one or
more byte values

10). Write an assembly language program to find the sum of a series


1+2+3+....+10 (or sum of first 10 natural numbers).
Solution:

MVI B,0AH // Loads B-register with 0AH


MVI A,00H // Loads the accumulator with 00H
LOOP: ADD B // Adds the content of B-register
to accumulator
DCR B // Decrements B-register
JNZ LOOP // Checks for No zero; if No zero
jump to LOOP
STA 2500H // Stores the answer to memory
location 2500H
HLT // Stop processing

11). Write an assembly language program to check the even parity or


the odd parity of the number stored in memory location 2010 H. Send
00 H or EE H at the output port 02 H if the parity is odd or even
respectively.
Solution:

LXI H,2010H // Initializes the H-L register


pair with the address of the location
MOV A,M // Moves the number to
accumulator
ORA A // ORing of A with A will load
the same number to accumulator. The parity flag will be
affected with this operation
JPO ODD // Jump to ODD if parity is odd
MVI A,EEH // Load EE H to accumulator for
even parity
OUT 02H // EE is sent to output port 02H
JMP END // Jump to END
ODD: MVI A,00H // Load 00 H to accumulator for
odd parity
OUT 02H // 00 is sent to output port 02H
END: HLT

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