Microprocessors and Microcontrollers: Unit II: (b) Looping, Counting, Time Delays & Code Conversion

Looping, Counting and Indexing

Question : 1. Explain the loop structure with counting and indexing in 8085 programming. 2. Write an assembly program to multiply a number by 8 program using the rotate instruction. 3. Write an assembly language program for arranging an array of 8-bit unsigned number in ascending order. 4. Write an assembly language program for adding a set of a numbers. 5. Write an 8085 Assembly language program to multiply two numbers in memory locations 4200 and 4201 and store the product in memory locations 4202 and 4203.

Looping, Counting and Indexing

AU : Dec.-05, 08, 09, 10, 11, 12, 14, 16, 17, 19, May-10, 13, 16, 17, 18, June-09, 12, 13

Before going to implement these techniques, we get conversant with these techniques and understand the use of them.

Looping : In this technique, the program is instructed to execute certain set of instructions repeatedly to execute a particular task number of times. For example, to add ten numbers stored in the consecutive memory locations we have to perform addition ten times.

Counting : This technique allows programmer to count how many times the instruction/set of instructions are executed.

Indexing : This technique allows programmer to point or refer the data stored in sequential memory locations one by one. Let us see the program loop to understand looping, counting and indexing.

The program loop is the basic structure which forces the processor to repeat a sequence of instructions. Loops have four sections.

1. Initialization section. 2. Processing section.

3. Loop control section 4. Result section.

Flowchart :


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.

Note : 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 interchange the position of the processing and loop control section then it is possible that the processing section may not be executed at all, if necessary. Refer flowchart 2.

 

Program Examples

Lab Experiment 3.1.1 : Calculate the sum of series of numbers.


Statement : Calculate the sum of series of numbers. The length of the series is in memory location 2200H and the series itself begins from memory location 2201H.

a. Assume the sum to be 8 bit number so you can ignore carries. Store the sum at memory location 2300H.

b. Assume the sum to be 16 bit number. Store the sum at memory locations 2300H and 2301H.

a. Sample problem :

2200H        = 04H

2201H        = 20H

2202H        = 15H

2203H        = 13H

2204H        = 22H

Result         = 20 + 15 + 13 + 22 = 6AH

2300H        = 6AH

Source program :

LDA 2200H

MOV C, A ; Initialize counter

SUB A ; sum = 0

LXI H, 2201H ; Initialize pointer

BACK : ADDM ; SUM = SUM + data

INX H ; Increment counter

DCR C ; Decrement counter

JNZ BACK ; If counter ≠ 0 repeat

STA 2300H ; Store sum

HLT ; Terminate program

        ; execution

 

b. Sample problem :


2200H = 04H       2201H = 9AH

2202H = 52H       2203H = 89H 2204H = 3EH

Result = 9AH + 52H + 89H + 3EH = 1B3H

A 2300H = B3H Lower byte 2301H = 01H Higher byte

Source program:

LDA 2200H

MOV C, A ; Initialize counter

LXI H, 2201H ; Initialize pointer

SUB A ; Sumlow = 0

MOV B, A ; Sumhigh = 0

BACK: ADD M ; Sum = sum + data

JNC SKIP

INR B ; Add carry to MSB of SUM

SKIP : INX H ; Increment pointer

DCR C ; Decrement counter

JNZ BACK ; Check if counter ≠ 0 repeat

STA 2300H ; Store lower byte

MOV A, B

STA 2301H ; Store higher byte

HLT ; Terminate program execution

 

Lab Experiment 3.1.2 : Data transfer from memory block Bl to memory block B2.

Statement : Transfer ten bytes of data from one memory to another memory block. Source memory block starts from memory location 2200H where as destination memory block starts from memory location 2300H.


Source program :

MVI C, 0AH ; Initialize counter

LXI H, 2200H ; Initialize source memory

             ; pointer

LXI D, 2300H ; Initialize destination

             ; memory pointer

BACK: MOV A, M ; Get byte from source

               ; memory block

STAX D ; Store byte in the

       ; destination memory

             ; block

INX H ; Increment source

      ; memory pointer

INX D ; Increment destination

      ; memory pointer

DCR C ;Decrement counter

JNZ BACK ; If counter ≠ 0 repeat

HLT ; Terminate program

    ; execution

 

Lab Experiment 3.1.3 : Multiply two 8-bit numbers.


Statement : Multiply two 8-bit numbers stored in memory locations 2200H and 2201H. Store the result in memory locations 2300H and 2301H.

Sample problem

(2200 H) = 03H

(2201H) = B2H

Result = B2H + B2H + B2H

= 216H

(2300) = 16H

(2301H) = 02H

Note : In 8085 multiplication can be done by repetitive addition.

Source program :

LDA 2200H

MOV E, A

MVI D, 00 ; Get the first number in DE

                  ; register pair

LDA 2201H

MOV C, A ; Initialize counter

LXI H, 0000H ; Result = 0

BACK: DAD D ;  Result = result + first number

DCR C ; Decrement count

JNZ BACK ; If count ≠ 0 repeat

SHLD 2300H ; Store result

HLT ; Terminate program execution

 

Lab Experiment 3.1.4 ; Divide 16-bit number by 8-bit number.

Statement : Divide 16 bit number stored in memory locations 2200H and 2201H by the 8 bit number stored at memory location 2202H. Store the quotient in memory locations 2300H and 2301H and remainder in memory locations 2302H and 2303H.


Sample problem :

(2200H)      = 60H

(2201H)      = AOH

(2202H)      = 12H

Result         = A060H/12H = 8E8H Quotient

and 10H remainder

(2300H)      = E8H

(2301H)      = 08H

(2302H)      = 10H

(2303H)      = 00H

Source program :

LHLD 2200H ; Get the dividend

LDA 2202H

MOV C, A ; Get the divisor

LXI D, 0000H ; Quotient = 0

BACK ; MOV AA, L

SUB C ; Subtract divisor

MOV L, A ; Save partial result

JNC SKIP ; If CY ≠ 1jump

DCR H ; Subtract borrow of previous

; subtraction

SKIP : INX D ; Increment quotient

MOV A, H

CPI, 00 ; Check if dividend < divisor

JNZ BACK ; If no repeat

MOV A, L

CMP C

JNC BACK

SHLD 2302H ; Store the remainder

XCHG

SHLD 2300H ; Store the quotient

HLT ; Terminate program execution

 

Lab Experiment 3.1.5 : Find the negative numbers in a block of data.

Statement : Find the number of negative elements (most significant bit 1) in a block of data. The length of the block is in memory location 2200H and the block itself begins in memory location 2201H. Store the number of negative elements in memory location 2300H.


Sample problem :

(2200H)      = 04H

(2201H)      = 56H

(2202H)      = A9H

(2203H)      = 73H

(2204H)      = 82H

Result         = 02 since 2202H and 2204H contain numbers with a MSB of 1.

Source program :

LDA 2200H

MOV C, A ;Initialize count

MVI B, 00 ; Negative number = 0

LXI H, 2201H; Initialize pointer

BACK: MOV A, M ; Get the number

ANI 80H ; Check for MSB

JZ SKIP ; If MSB = 1

INR B ; Increment negative

; number count

SKIP : INX H ; Increment pointer

DCR C : Decrement count

JNZ BACK ; If count ≠ 0 repeat

MOV A, B;

STA 2300H ; Store the result

HLT ; Terminate program

; execution

 

Lab Experiment 3.1.6: Find the largest of given numbers.

Statement:

Find the largest number in a block of data. The length of the block is in memory location 2200H and the block itself start from memory location 2201H. Store the maximum number in memory location 2300H. Assume that the number in the block are all 8 bit unsigned binary numbers.

Sample problem :

(2200H) = 04

(2201H) = 34H

(2202H) = A9H

(2203H) = 78H

(2204H) = 56H

Result = (2202H) = A9H.


Source program:

LDA 2200H

MOV C, A ; Initialize counter

XRA A Maximum = Minimum

; possible value = 0

LXI H, 2201H ; Initialize pointer

BACK CMP M ; Is number > maximum

JNC SKIP

MOV A, M Yes, replace maximum

SKIP : INX H

DCR C

JNZ BACK

STA 2300H ; Store maximum number

HLT ; Terminate program execution

 

Lab Experiment 3.1.7 : Arrange numbers in the ascending order.

AU : Dec.-05, 18, May-10, 16, June-12, 13

Statement : Write a program to sort given 10 numbers from memory location 2200H in the ascending order.

Source program :

MVI B, 09  ; Initialize counter 1

START : LXI H, 2200H          ; Initialize memory pointer

MVI C, 09H         ; Initialize counter 2

BACK : MOV A, M ; Get the number

INXH          ; Increment memory pointer

CMP M ; Compare number with next number

JC SKIP ; If less, don't interchange

JZ SKIP ; If equal, don’t interchange

MOV D, M

MOV M, A

DCX H

MOV M, D

INX H ; Interchange two numbers

SKIP : DCR C ; Decrement counter 2

JNZ BACK ; If not zero, repeat

DCR B ; Decrement counter 1

JNZ START ; If not zero, repeat

HLT ; Terminate program execution

Flowchart :


 

Lab Experiment 3.1.8 : Calculate the sum of series of even numbers.

Statement : Calculate the sum of series of even numbers from the list of numbers. The length of the list is in memory location 2200H and the series itself begins from memory location 2201H. Assume the sum to be 8 bit number so you can ignore carries and store the sum at memory location 2210H.


Sample problem :

2200H = 4H

2201H = 20H

2202H = 15H

 2203H = 13H

2204H = 22H

Result = 20 + 22 = 42H

2210H = 42H

Source program :

LDA 2200H

MOV C, A ; Initialize counter

MVI B, 00H ; sum = 0

LXI H, 2201H ; Initialize pointer

BACK: MOV A, M ; Get the number

ANI 01H ; Mask Bit1 to Bit7

JNZ SKIP ; Don't add if number is

; ODD

MOV A, B ; Get the sum

ADD M ; SUM = SUM + data

MOV B, A ; Store result in B register

SKIP : INX H ; Increment pointer

DCR C ; Decrement counter

JNZ BACK ; If counter 0 repeat

STA 2210H ; Store sum

 

Lab Experiment 3.1.9 ; Search a byte in a given numbers.

Statement : Search the given byte in the list of 50 numbers stored in the consecutive memory locations and store the address of memory location in the memory locations 2200H and 2201H. Assume byte is in the C register and starting address of the list is 2000H. If byte is not found store 00 at 2200H and 2201H.


Source program :

LXI H, 2000H ; Initialize memory

; pointer

MVI B, 52H ; Initialize counter

BACK : MOV A, M ; Get the number

CMP C ; Compare with the

; given byte

JZ LAST ; Go last if match

; occurs

INXH ; Increment memory

; pointer

DCRB ; Decrement counter

JNZ B ; If not zero, repeat

LXI H, 0000H

SHLD 2200H

JMP END ; Store 00 at 2200H

; and 2201H

LAST : SHLD2200H ; Store memory

; address

END : HLT ; Stop

 

Lab Experiment 3.1.10 : Transfer contents to overlapping memory blocks.

Statement : Write assembly language program to with proper comments for the following : A block of data consisting of 256 bytes is stored in memory starting at 3000H. This block is to be shifted (relocated) in memory from 3050H onwards. Do not shift the block or part of the block anywhere else in the memory.

Source program :

Two blocks (3000 - 30FF and 3050 - 314F) are overlapping. Therefore it is necessary to transfer last byte first and first byte last.

MVI C, FFH ; Initialize counter

LXI H, 30FFH ; Initialize source memory pointer

LXI D, 314FH ; Initialize destination memory pointer

BACK : MOV A, M ; Get byte from source memory block

STAX D ; Store byte in the destination memory block

DCX H ; Decrement source memory pointer

DCX ; Decrement destination memory pointer

DCR C ; Decrement counter

JNZ BACK ; If counter ^ 0 repeat

HLT ; Stop execution

 

Lab Experiment 3.1.11 : Multiply two eight bit numbers with shift and add method.

Statement : Multiply the 8-bit unsigned number in memory location 2200H by the 8-bit unsigned number in memory location 2201H. Store the 8 least significant bits of the result in memory location 2300H and the 8 most significant bits in memory location 2301H.

Sample problems :

(2200) = 1100 (0CH)

(2201) = 0101 (05H)

Multiplicand 1100 (1210)  Multiplier 0101(510) Result 12 × 5 = 6010

For simplicity, Multiplicand and Multiplier are taken 4-bit each.


Source program:

LXI H, 2200H ; Initialize the memory pointer

MOV E, M ; Get multiplicand

MVI D, 00H ; Extend to 16-bits

INX H ; Increment memory

; pointer

MOV A, M ; Get multiplier

LXI H, 0000H; Product = 0

MVI B, 08H ; Initialize counter with

; count 8

MULT DAD H ; Product = product x 2

RAL

JNC SKIP ; Is carry from multiplier 1 ?

DAD D : Yes,

; Product = Product + Multiplicand

SKIP : DCR B ; Is counter = zero

JNZ MULT ; no, repeat

SHLD 2300H; Store the result

HLT ; End of program


 

Lab Experiment 3.1.12 : Divide 16-bit number with 8-bit number using shifting technique.

Statement : Divide the 16-bit unsigned number in memory locations 2200H and 2201H (most significant bits in 2201H) by the 8-bit unsigned number in memory location 2300H store the quotient in memory location 2400H and remainder in 2401H.

Assumption : The most significant bits of both the divisor and dividend are zero.

Sample problem :

For simplicity, Dividend and divisor are taken 8-bit and 4-bit respectively

Dividend = 0110 0001 (61H) Divisor = 0111 (07H)


Source program :

MVI E, 00 ; Quotient = 0

LHLD 2200H ; Get dividend

LDA 2300 ; Get divisor

MOV B, A ; Store divisor

MVI C, 08 ; Count = 8

NEXT  : DAD H ; Dividend = Dividend × 2       

MOV A, E

RLC

MOV E, A ; Quotient = Quotient × 2

MOV A, H

SUB B ; Is most significant byte of Dividend : > divisor

JC SKIP ; No, go to Next step

MOV H, A ; Yes, subtract divisor

INR E ; and Quotient = Quotient + 1

SKIP DCR C ; Count = Count – 1

JNZ NEXT ; Is count ≠ 0 repeat

MOV A, E

STA 2401H ; Store Quotient  

MOV A, H 

STA 2401H ; Store remainder

HLT  ; End of program.

Flowchart :

 

 

Lab Experiment 3.1.13 : Write an assembly language program to generate fibonacci number.

MVI D, COUNT ; Initialize counter

MVI B, 00  ; Initialize variable to store previous number

MVI C, 01  ; Initialize variable to store current number

BACK : MOV A, B ; [Add two

ADD C ; numbers ]

MOV B, C ; Current number is now previous number

MOV C, A ; Save result as a new current number

DCR D ; Decrement count

JNZ BACK ; If count ≠ 0 go to BACK

HLT ; Stop.

 

Lab Experiment 3.1.14 : Program to evaluate a2 + b2

Statement : Write 8085 assembly language program to perform the following, a2 + b2, where a and b are 8 - bit binary numbers. Explain with algorithm and flowchart.


Algorithm

1. Read number a.

2. Find a2 by performing a × a and store result 1.

3. Read number b.

4. Find b2 by performing b × b and store result 2.

5. Result = Result 1 + Result 2

Refer Lab experiment 3.1.11 for the flowchart of multiplication routine.

Program

MVI E, Number a ; Get the number a

CALL MULTIPLY ; Call multiply subroutine

SHLD 2200H ; Store result 1

MVI E, Number b ; Get the number b

CALL MULTIPLY ; Call multiply subroutine

XCHG ; Store result 2 in DE

LHLD 2200H ; Get the result 1 in HL

DAD D ; HL → HL + DE

HLT : Stop

MULTIPLY: MVI D, 00 ; Extend to 16-bit

MOV A, E ; Multiplier = multiplicand

LXI H, 0000H ; Product = 0

MVI B, 08H ; Initialize counter with

; count 8

MULT: DAD H ; Product = product × 2

RAL

JNC SKIP ; Is carry from multiplier 1?

DAD D ; Yes, Product = Product + Multiplicand

SKIP: DCR B : Is counter = zero

JNZ MULT ; no repeat

RET ; Return to main program

 

Lab Experiment 3.1.15 : Program to count given data in a set of numbers

Statement : Write a program to count the number of times the data 02 is present in a set of 20 numbers.

It is assumed that set of 20 numbers are stored from memory location 2000H.

LXI, 2000H ; Initialize memory pointer

MVI B, 14H ; Initialize counter

MVI C, 00H ; Count = 0

BACK: MOV A, M Get the number

CPI 02H ; Compare with 02

JNZ NEXT ; If not zero skip next instruction

INR C ; Increment count

NEXT : INXH ; Increment memory pointer

DCR B ; Decrement counter

JNZ BACK ; If not zero, repeat

HLT ; Stop

 

Lab Experiment 3.1.16 : Program to multiply two 16-bit numbers

AU : June-09, Marks 12

Statement : Write an assembly language program in 8085 to multiply two 16-bit numbers.

LXI SP, 27FFH ; Initialize stack pointer

LXI H, 0000H ; Result = 0 (Lower word)

SHLD 2000H ; Result = 0 (Higher word)

LXI D, number 1 ; Multiplicand

LXI B, number 2 ; Multiplier as a counter

BACK: DAD D ; Result (HL) = HL + DE

JNC NEXT ; If no carry goto NEXT

PUSH H ; Save HL register

LHLD 2000H ; Get the higher word

INXH ; Increment word by 1

SHLD 2000H ; Save higher word

POP H ; Get the lower word in HL

DCX B ; Decrement counter

MOV A, B ; Check whether

ORA C ; BC is zero

JNZ BACK ; if not zero, repeat

XCHG ; Exchange DE and HL

LHLD 2000H ; Get the higher word in HL

HLT ; Stop

The 32-bit answer of multiplication is in HL and DE register. The lower word is in DE register and higher word is in HL register.

 

Lab Experiment 3.1.17 : Find the factorial of a number.

Statement : Write a program to calculate the factorial of a number between 0 to 8.

Main program :

LXI SP, 27FFH ; Initialize stack pointer

LDA 2200H ; Get the number

CPI 02H ; Check if number is greater than 1

JC LAST   

MVI D, 00H  ; Load number as a result

MOV E, A 

DCR A      

MOV C, A ; Load counter one less than number

CALL FACTO ; Call subroutine facto

XCHG ; Get the result in HL

SHLD 2201H ; Store result in the memory

JMP END  

LAST : LXI H.0001H ; Store result = 01

SHLD 2201H      

END: HLT 

Flowchart :


Subroutine program : 

FACTO : LXI H.0000H

MOV B, C ; Load counter

BACK : DAD D  

DCR B ;     

JNZ BACK ; Multiply by successive addition

XCHG ; Store result in DE

DCR C ; Decrement counter

CNZ FACTO ; Call subroutine FACTO

RET ; Return to main program

 

Lab Experiment 3.1.18 : Count the number of even and odd numbers.

AU : Dec.-19

Statement : Write a 8085 program to count the number of even and odd numbers in a given set of numbers. It is assumed that set consists of twenty 8-bit numbers.

LXI, 2000H ; Initialize iteration counter

MVI D, OOH ; Odd number count = 0

MVI E, OOH ; Even number count = 0

BACK : MOV A, M ; Get the number

ANI 01 H ; Mask bit 1 to bit 7

JZ SKIP ; If even goto SKIP

INR D ; Increment odd count

JMP LAST

SKIP : INT E Increment even count

LAST : INX H ; Increment memory pointer

DCRB ; Decrement iteration counter

JNZ BACK ; If not zero repeat

HLT ; Terminate the program execution

Note : D = Number of odd numbers, E = Number of even numbers

Review Question

1. Explain the loop structure with counting and indexing in 8085 programming.

2. Write an assembly language program in 8085 to add N one byte binary numbers stored from location X + 1, where N is stored at location X, store the result in location Y and Y + 1. Display the result in address field.

3. Write an assembly language program in 8085 to find the maximum number from the given n numbers.

4. Write an 8085 program to count the number of even and odd numbers in a given set of numbers.

5. Write an 8085 program to find the largest in the set of an 8-bit numbers.

6. Write an assembly program to multiply a number by 8 program using the rotate instruction.

7. Write an ALP to add 5 data bytes stored in memory locations starting at 4500H and display the sum in next memory location.

8. Sixteen bytes are stored in memory locations at XX50h to XX5Fh. (Refer lab experiment 3.1.1) Transfer the entire block of data to new memory locations starting at XX70h.

9. Write an assembly language program for arranging an array of 8-bit unsigned number in ascending order.

10. Write an 8085 assembly language program to multiply two 8-bit numbers.

AU : Dec.-11, 12, Marks 8

12. Six bytes of data are stored in memory locations starting at XX50h. Add all the data bytes. Use register B to save any carries generated while adding data bytes. Display the entire sum at two consecutive memory location XX70h and XX71h.

13. Write an 8085 assembly langauge program to sort numbers ascending orders.

AU : Dec.-05, May-10, 13, Marks 8

14. Write an assembly language program for adding a set of a numbers.

15. Write an assembly language program to sort numbers in ascending order.

16. Write an 8085 Assembly language program to multiply two numbers in memory locations 4200 and 4201 and store the product in memory locations 4202 and 4203.

Microprocessors and Microcontrollers: Unit II: (b) Looping, Counting, Time Delays & Code Conversion : Tag: : - Looping, Counting and Indexing