Microprocessors and Microcontrollers: Unit II: (a) 8085 Instruction Set and ALP

Programming Examples

Question : 1. Describe with a suitable 8085 assembly language program the use of subroutine instructions.

Programming Examples

Lab Experiment 2.5.1 : Store 8-bit data in memory.

Statement : Store the data byte 52H into memory location 2000H.

Program 1 :

MVI A, 52H ; Store 52H in the accumulator

STA 2000H          ; Copy accumulator contents at address 2000H

HLT  ; Terminate program execution

Program 2 :

LXI H, 2000H ; Load HL with 2000H

MVI M, 52H ; Store 52H in memory location pointed

                      ; by HL register pair (2000H)

HLT  ; Terminate program execution

The result of both programs will be the same. In program 1 direct addressing instruction is used, whereas in program 2 indirect addressing instruction is used.

 

Lab Experiment 2.5.2 : Exchange the contents of memory locations.

Statement : Exchange the contents of memory locations 1000H and 2000H

Program 1 :

LDA 1000H ; Get the contents of memory location 1000H into accumulator

MOV B, A ; Save the contents in B register

LDA 2000H ; Get the contents of memory location 2000H into accumulator.

STA 1000H ; Store the contents of accumulator at address 1000H.

MOV A, B ; Get the saved contents back into A register

STA 2000H ; Store the contents of accumulator at address 2000H

HLT ; Terminate program execution

Program 2 :        

LXI H, 1000H      ; Initialize HL register pair as a pointer

                              ; to memory location 1000H

LXI D, 2000H      ; Initialize DE register pair as a pointer

                             ; to memory location 2000H

MOV B, M ; Get the contents of memory location 1000H into B register

LDAX D ; Get the contents of memory location 2000H into A register

MOV M, A ; Store the contents of A register into memory location 1000H

MOV A, B ; Copy the contents of B register into accumulator

STAXD ; Store the contents of A register into memory location 2000H.

HLT  ; Terminate program execution

In Program 1 direct addressing instructions are used, whereas in Program 2 indirect addressing instructions are used.

 

Lab Experiment 2.5.3 : Add two 8-bit numbers.

Statement : Add the contents of memory locations 2000H and 2001H and place the result in memory location 2002H.


Sample problem

(2000H) = 14H

(2001H) = 89H

Result = 14H + 89H = 9DH

Source program

LXI H, 2000H      ; HL points 2000H

MOV A, M ; Get first operand

INX H         ; HL points 2001H

ADD M      ; Add second operand

INX H         ; HL points 2002H

MOV M, A ; Store result at 2002H

HLT  ; Terminate program execution

 

Lab Experiment 2.5.4 : Subtract two 8-bit numbers.

Statement : Subtract the contents of memory location 2001H from the memory location 2000H and place the result in memory location 2002H.


Sample problem

(2000H) = 51H

(2001H) = 19H

Result = 51H – 19H = 38H

Source program

LXI H, 2000H ; HL points 2000H

MOV A, M ; Get first operand

INX H ; HL points 2001H

SUB M ; Subtract second operand

INX H ; HL points 2002H

MOV M, A ; Store result at 2002H

HLT ; Terminate program

; execution

 

Lab Experiment 2.5.5 : Add two 16-bit numbers.

Statement : Add the 16-bit number in memory locations 2000H and 2001H to the 16-bit number in memory locations 2002H and 2003H. The most significant eight bits of the two numbers to be added are in memory locations 2001H and 2003H. Store the result in memory locations 2004H and 2005H with the most significant byte in memory location 2005H.

Sample problem

(2000H) = 15H

(2001H) = 1CH

(2002H) = B7H

(2003H) = 5AH

Result = 1015 + 5AB7H = 76CCH

(2004H) = CCH

(2005H) = 76H


Source program 1

LHLD 2000H ; Get first 16-bit number in HL

XCHG ; Save first 16-bit number in DE

LHLD 2002H ; Get second 16-bit number in HL

MOV A, E ; Get lower byte of the first number

ADD L ;  Add lower byte of the second number

MOV L, A ; Store result in L register

MOV A, D ; Get higher byte of the first number

ADC H ; Add higher byte of the second number

             ; with carry

MOV H, A ; Store result in H register

SHLD 2004H ; Store 16-bit result in memory locations

                        ; 2004H and 2005H.

HLT ; Terminate program execution

Source program 2

LHLD 2000H ; Get first 16-bit number

XCHG ; Save first 16-bit number in DE

LHLD 2002H ; Get second 16-bit number in HL

DAD D ; Add DE and HL

SHLD 2004H ; Store 16-bit result in memory locations

; 2004H and 2005H.

HLT ; Terminate program execution

In program 1 eight bit addition instructions are used (ADD and ADC) and addition is performed in two steps. First lower byte addition using ADD instruction and then higher byte addition using ADC instruction. In program 2 16-bit addition instruction (DAD) is used.

 

Lab Experiment 2.5.6 : Subtract two 16-bit numbers.

Statement : Subtract the 16-bit number in memory locations 2002H and 2003H from the 16-bit number in memory locations 2000H and 2001H. The most significant eight bits of the two numbers are in memory locations 2001H and 2003H. Store the result in memory locations 2004H and 2005H with the most significant byte in memory location 2005H.

Sample problem

(2000H) = 19H

(2001H) = 6AH

(2002H) = 15H

(2003H) = 5CH Result = 6A19H - 5C15H = 0E04H

Source program


LHLD 2000H ; Get first 16-bit number in HL

XCHG ; Save first 16-bit number in DE

LHLD 2002H ; Get second 16-bit number in HL

MOV A, E ; Get lower byte of the first number

SUB L ; Subtract lower byte of the second number

MOV L, A ; Store the result in L register

MOV A, D ; Get higher byte of the first number

SBB H ; Subtract higher byte of second

             ; number with borrow

MOV H, A ; Store 16-bit result in memory

                    ; locations 2004H and 2005H.

SHLD 2004H ; Store 16-bit result in memory

                        ; locations 2004H and 2005H.

HLT ; Terminate program execution.

 

Lab Experiment 2.5.7 : Check results after execution of INR B, INR C and INX B instructions.

Statement : If the contents of B = FFH and C = FFH then after execution of following instructions give the contents of register B and register C.


 

Lab Experiment 2.5.8 : Check results after execution of OCR C, DCR B and OCX B instructions.

Statement : If the contents of B = OOH and C = OOH then after execution of following instructions give the contents of register B and register C.


 

Lab Experiment 2.5.9 : Find the 1's complement of a number.


Statement : Find the l's complement of the number stored at memory location 2200H and store the complemented number at memory location 2300H.

Sample problem

(2200H) = 55H

Result = (2300H) = AAH

Source program

LDA 2200H  ; Get the number

CMA ; Complement number

STA 2300H          ; Store the result

HLT  ; Terminate program execution

 

Lab Experiment 2.5.10 : Find the 2's complement of a number.


Statement : Find the 2's complement of the number stored at memory location 2200H and store the complemented number at memory location 2300H.

Sample problem

(2200H) = 55H

Result = (2300H) = AAH + 1 = ABH

Source program

LDA 2200 H ; Get the number

CMA ; complement the number

ADI, 01H ; Add one in the number

STA 2300H ; Store the result

HLT ; Terminate program execution

 

Lab Experiment 2.5.11 : Pack the two unpacked BCD numbers.


Statement : Pack the two unpacked BCD numbers stored in memory locations 2200H and 2201H and store result in memory location 2300H. Assume the least significant digit is stored at 2200H.

Sample problem

(2200H) = 04

(2201H) = 09

Result = (2300H) = 94

Source program

LDA 2201H ; Get the Most significant BCD digit

RLC

RLC

RLC

RLC  ; Adjust the position

ANI FOH    ; Make least significant BCD digit zero

MOV C, A ; Store the partial result

LDA 2200H         ; Get the lower BCD digit

ADD C       ; Add lower BCD digit

STA 2300H          ; Store the result

HLT  ; Terminate program execution

 

Lab Experiment 2.5.12 : Unpack the BCD number.


Statement : Two digit BCD number is stored in memory location 2200H. Unpack the BCD number and store the two digits in memory locations 2300H and 2301H such that memory location 2300H will have lower BCD digit.

Sample problem

(2200H) = 58

Result = (2300H) = 08 and (2301H) = 05

Source program

LDA 2200H ; Get the packed BCD number

ANI FOH ; Mask lower nibble

RRC

RRC

RRC

RRC  ; Adjust higher BCD digit as a lower digit

STA 2301H ; Store the partial result

LDA 2200H  ; Get the original BCD number

ANI 0FH ; Mask higher nibble

STA 2201H  ; Store the result

HLT  ; Terminate program execution

 

Lab Experiment 2.5.13 : Sample subroutine program.

Statement : Read the program given below and state the contents of all registers after the execution of each instruction in sequence.

Main program :

6000H        LXI SP, 27FFH

6003H        LXI H, 2000H

6006H        LXI B, 1020H

6009H        CALL SUB

600CH        HLT

Subroutine program :

6100H SUB : PUSH B

6101H        PUSH H

6102H        LXI B, 4080H

6105H        LXI H, 4090H

6108H        DAD B

6109H        SHLD 2200H

610CH        POP H

610DH        POP B

610EH        RET

Solution : The Table 2.5.1 shows the instruction sequence and the contents of all registers and stack after execution of each instruction.


 

Lab Experiment 2.5.14 : Add contents of two memory locations.


Statement : Add the contents of memory locations 2000H and 2001H and place the result in the memory locations 2002H and 2003H.

Sample problem :

(2000H) = 7FH

(2001H) = 89H

Result = 7FH + 89H = 108H

(2002H) = 08H

(2003H) = 01H

Source Program

LXI H, 2000H ; HL Points 2000H

MOV A, M ; Get first operand

INX H ; HL Points 2002H

MOV M, A ; Store the lower byte of result at 2002H

MVI A, 00 ; Initialize higher byte result with 00H

ADC A ; Add carry in the high byte result.

INX H ; HL Points 2003H

MOV M, A ; Store the higher byte of result at 2003H

HLT ; Terminate program execution

 

Lab Experiment 2.5.15 : Right shift data within the 8-bit register.


Statement : Write a program to shift an eight bit data four bits right Assume that data is in register C.

Source program :

MOV A, C

RAR

RAR

RAR

RAR

MOV C, A

HLT

 

Lab Experiment 2.5.16 : Right shift data within 16-bit register.


Statement : Write a program to shift an 16-bit data 1 bit right. Assume data is in the BC register pair.

Source program :

MOV A, B

RAR

MOV B, A

MOV A, C

RAR

MOV C, A

HLT

 

Lab Experiment 2.5.17 : Left shift 16-bit data within 16-bit register.

Statement : Write a program to shift an 16-bit data 1 bit left. Assume data is in the HL register pair.

Sample problem :


Source program :

DAD H

 

Lab Experiment 2.5.18 : Alter the contents of flag register.

Statement : Write a set of instructions to alter the contents of flag register in 8085.

Source Program :

PUSH PSW ; Save flags on stack

POP H ; Retrieve flags in ‘L’

MOV A, L ; Flags in accumulator

CMA ; Complement accumulator

MOV L, A ; Accumulator in ‘L’

PUSH H ; Save on stack

POP PSW ; Back to flag register

HLT  ; Terminate program execution

 

Lab Experiment 2.5.19 : Find 2's complement of 16-bit number.


Source Program :

LDA 2000H ; Load accumulator with contents of 2000H location

CMA ; Complement the lower byte

ADD 01H ; Add 01 to the accumulator

STA 4000H ; Store the accumulator data to 4000H location

LDA 2001H ; Load accumulator with data from memory location 2001H

CMA ; Complement the higher byte

ADC OOH ; Add with carry to accumulator

HLT  ; Stop

 

Lab Experiment 2.5.20 : Simulation of CALL and RET instructions.

Statement : If CALL and RET instructions are not provided in the 8085, could it be possible to write subroutines for this microprocessor ? If so how will you call and return from the subroutine ?

Solution : We know that,

1. CALL instruction transfers the program control to the subroutine program by loading the address of the subroutine program in the program counter, and before transferring program control it saves the address of the instruction after the CALL instruction on the stack.

2. RET instruction loads the program counter with return address from the stack and thus transfers the program control back to the instruction following the CALL.

Now our task is to implement the operation performed by these two instructions with the help of other instructions of 8085.

Let us implement RET instruction first. To implement this it is necessary to load program counter with return address. To load program counter with 16-bit address we have two instructions : JMP address and PCHL. But JMP address instruction cannot be used for our purpose because JMP address loads program counter with fix address and the return address is not fix, it depends on, from where the subroutine is 'CALLed' in the main program. The other instruction, PCHL loads 16-bit contents of HL register pair into PC. If we manage to load return address into HL register pair, every time subroutine is called, then it is possible to implement RET instruction with the help of

PCHL instruction. We can load the HL register pair with return address before we make a CALL for subroutine program.

The task to implement CALL instruction is simplified because we are going to store return address into the HL register pair before we make a 'CALL' for subroutine program. Now it is only required to load program counter with the subroutine address. This can be implemented by executing JMP instruction. Here JMP instruction is suitable because subroutine starting address is a 'fix' address. The table shows how we can 'Call' and 'Return' from the subroutine without using CALL and RET instructions.


Review Question

1. Describe with a suitable 8085 assembly language program the use of subroutine instructions.

AU : May-13, Marks 8

Microprocessors and Microcontrollers: Unit II: (a) 8085 Instruction Set and ALP : Tag: : - Programming Examples