Sem 6‎ > ‎

### MPRC LAB Subscribe to posts

#### P9 Largest of a series.

posted Apr 7, 2013, 3:52 AM by Neil Mathew   [ updated Apr 7, 2013, 3:52 AM by Neil Mathew ]

Program:

 LXI H, 2500 MOV C,M Count INX H MOV A,M First Number DCR C LOOP INX H CMP M JNC AHEAD A < M => Carry MOV A,M << if M greater. AHEAD DCR C JNZ LOOP STA 2600 (largest of series) HLT

JNC : LARGER
JC   : SMALLER

#### P8: Sum of two 16 bit numbers with carry.

posted Apr 7, 2013, 3:33 AM by Neil Mathew   [ updated Apr 7, 2013, 3:45 AM by Neil Mathew ]

This program uses the following instructions which I haven't used in the other ones.

Use of LHLD <16bit memory> instruction to load the 16 bit number stored in two separate 8 bit memory locations to the H and L Register. SHLD <16bit memory> for storing ans.

Use of DAD rp instruction to add the contents of the H-L pair with the specified Register pair.

Use of XCHG instruction to swap values of DE pair and HL pair.

PROGRAM:

 MVI C, 00 LHLD 2500 XCHG LHLD 2502 DAD D JNC AHEAD INR C AHEAD SHLD 2504 MOV A,C STA 2506

#### P7: Sort in ascending and descending order

posted Apr 7, 2013, 2:13 AM by Neil Mathew   [ updated Apr 7, 2013, 3:20 AM by Neil Mathew ]

Bubble sort.

This pic should help. Two loops are there in a bubble sort. One inner loop to go through the elements and push the largest number to the end (while the other numbers like bubbles float towards the top) and the Outer loop which reduce the set of numbers, removing the last largest number from the set.

Four Registers required:

A is used as a temporary register while traversing the series.
B is used as a temporary register to swap numbers.
C is used as the iterator for the outer loop
D is used as the iterator for the inner loop

Program:

 LXI H, 2500 (count) (n) MOV C, M n-1 comparisons DCR C OUTERLOOP MOV D, C LXI H, 2501 (1st of series) INNERLOOP MOV A, M INX H CMP M JC AHEAD A < B => Carry => ASC Orderif No Carry, Not ASC, Swap. MOV B, M START SWAP STTMENTS: MOV M, A DCX H MOV M, B INX H END SWAP STTMENTS DCR D JNZ INNERLOOP DCR C JNZ OUTERLOOP RST1

FOR DESCENDING ORDER, USE JNC in place of JC.

 LXI H, 2500 (count) (n) MOV C, M n-1 comparisons DCR C OUTERLOOP MOV D, C LXI H, 2501 (1st of series) INNERLOOP MOV A, M INX H CMP M JNC AHEAD A < B => Carry.if Carry, Swap => DESC Order MOV B, M START SWAP STTMENTS: MOV M, A DCX H MOV M, B INX H END SWAP STTMENTS DCR D JNZ INNERLOOP DCR C JNZ OUTERLOOP RST1

#### P6: Division of two 8 bit numbers.

posted Apr 7, 2013, 2:11 AM by Neil Mathew   [ updated Apr 7, 2013, 2:12 AM by Neil Mathew ]

Division.
Clarify the terms Divisor, Dividend, Quotient and Remainder:

Like I explained in multiplication, division is nothing but repeated subtraction. However, the programming is handled differently. In multiplication, the loop continued n number of times (for multiplication m x n). This was done by decrementing the register holding the value n and a condition when it would become zero. (JNZ).

In division however, we need to count the number of possible subtractions/deductions that can take place and therefore we increment a register to record the quotient. And this should be in an unconditional loop(JMP), broken only when the divisor is smaller than the divident. We'll use the accumulator as the dividend which will later hold the remainder once the loop is over.

We need 3 registers in this case.

A for storing the divident, which later holds the remainder.
B for storing the divisor
C for storing the quotient

PROGRAM:

 MVI C, 00 LXI H, 2500 MOV A, M INX H MOV B, M LOOP CMP B JC OVER SUB B INR C JMP LOOP OVER STA 2601 (remainder) MOV A,C STA 2600 (quotient) RST1

#### P5: Multiplication of two 8 bit numbers.

posted Apr 7, 2013, 1:57 AM by Neil Mathew   [ updated Apr 7, 2013, 1:57 AM by Neil Mathew ]

A little intro before I just type the program.

What is multiplication exactly?
So if I was to perform 2 x 3, I'd get the answer by adding 2 three times. 2 + 2 + 2.

Take 4 registers. Say A, B, C, D.

A (Accumulator) should store the Product.
B should store the carry, if any.
C should store the first number. (like 2)
D should store the second number. (like 3)

PROGRAM:

 MVI B, 00 MVI A,00 LXI H, 2500 MOV C, M INX H MOV D, M LOOP ADD C JNC AHEAD INR B AHEAD DCR D JNZ LOOP STA 2503H RST1

RST1 is a software interrupt which can be used by programmers to interrupt the program.
Preferred over HLT because that requires a reset using one of the physical buttons of the kit.

#### P4: Larger/Smaller of two 8 bit numbers.

posted Mar 29, 2013, 6:30 AM by Neil Mathew   [ updated Mar 29, 2013, 6:32 AM by Neil Mathew ]

Since the basic idea is understood, I am now directly writing the programs now.
Finding the LARGER of the two 8 bit numbers. the CMP would perform the following:

@2001 - @2000
reg A - reg B

So, if @2001 is greater, no carry. However, if @2001 is NOT greater, a carry/borrow is generated.

So, if carry/borrow is generated, then reg B is greater, and therefore, move the value of B back to A and print or store A as the greater of the two.
if carry/borrow is not generated, reg A is already the greater and is directly stored as greater of two.

A condition is required to perform the underline if there is a carry/borrow.

`The compare instruction (CMP) in the 8085 subtracts the two operands but does not store the results. What it does do is set flags, in particular, zero, negative, carry, parity, and auxillary carry, so you can follow up the instruction with a conditional branch or call instruction to perform some operation based on the comparison of those two numbers.`

 LDA 2000H MOV B,A LDA 2001H CMP B JNC GO MOV A,B GO STA 2002H

IF THERE IS CARRY, MOVE CONTENT OF B TO A.

To find the SMALLER of the two numbers, conveniently change the JNC to JC and reverse the meaning altogether.

 LDA 2000H MOV B,A LDA 2001H CMP B JC GO MOV A,B GO STA 2002H

IF THERE IS NO CARRY, MOVE CONTENT OF B TO A.

JNC : LARGER
JC   : SMALLER

#### P3: 1's compliment and and 2's compliment of 16 bit number.

posted Mar 29, 2013, 6:11 AM by Neil Mathew   [ updated Apr 7, 2013, 1:21 AM by Neil Mathew ]

1's Compliment.

16 bit numbers. That's the main difference here.
Before for 8 bit numbers, we used LDA and STA.

`LXI H ,2050H`
`Loads BC pair with value 2050H`
`H-> 20H`
`L-> 50H`

`Its similar to 2 MVI instruction`
`ie `
`MVI H,20H`
`MVI L,50H`

`which eventually implies..HL -> 2050H`
`the advantage of LXI over MVI is that LXI is 3 byte and requires 10T states.Two MVI instruction is 4Byte and require 14Tstates...`

`INX increments (and DCX decrements) the 16 bit register pairs`

`H-L pair holds the address of the location pointed by the memory pointer M. M can be used to access the actual values of H-L pair.`

What I understand is that it shifts the address where the register H is pointing at to the next concurrent memory space. (This way, it can be used to access concurrent data like in an array using LXI H, INX H commands.)

 Memory Address Opcode Mneumonics Comments 2000 21 LXI H, 2500 Load Immediately 2500 to H 2001 00 2002 25 2003 7E MOV A. M m points to the content (value) of the register H. That value is copied or moved to accumulator. 2004 2F CMA Compliment that value. 2005 32 STA 2502 Store it in 2502. 2006 02 2007 25 2008 23 INX H Now Register H points at 25 2009 7E MOV A,M 200A 2F CMA 200B 32 STA 2503 200C 04 200D 25 200E CF RST

OUTPUT:

2500    FF
2501    FF

After execution of program:

2502    00
2503    00

'Cause compliment of the hexadecimal 0 is F.
0000 (0) <==> 1111 (F)

2's Compliment.

A condition comes into play in this case. In the previous case of an 8 bit number, we increment the value after complimenting it to get the 2's compliment. But for 16 bit numbers, there are two parts: LSB, MSB.

First, we compliment the LSB, then add one to it. (using ADI 01 instead of INR A, so as to affect the carrys for our jump statement to work properly)

`ADI affects all status flags while INR affects all status flags ``except ``the carry flag.`

Second, if the LSB on getting incremented by 1 yields a carry, that carry should be passed on to the MSB. If no carry is generated, then MSB should not be incremented by 1.

 Memory Address Opcode Mneumonics Comments 2000 21 LXI H, 2501 Load Immediately 2501 to H-L pair. 2001 01 2002 25 2003 06 MVI B, 00 Reg B holds the carry, if any. 2004 00 2005 7E MOV A,M 2006 2F CMA 2007 C6 ADI 01 2008 01 2009 32 STA 2503 200A 03 200B 25 200C D2 JNC 2010 Jump to address 2010 if NO CARRY.This checks the last instruction which affects carry, that being CMA in this case, NOT ADI 01. (Reason why INR A was avoided) 200D 10 200E 20 200F 04 INR B If there was a carry, B becomes 01.Otherwise it is 00. 2010 23 INX H 2011 7E MOV A,M 2012 2F CMA 2013 80 ADD B 2014 32 STA 2504 2015 04 2016 25 2017 CF RST

JNC (Jump if Not Carry) label
This moves program control to the location specified by the label if there is no carry, (CY = 0).

I prefer to think of it this way:
If there is a carry, execute the command immediately after.

Similarly for JC (JUMP if CARRY) USED usually in the opposite sense.
If there is NO carry, execute the command immediately after.

OUTPUT:

2500    FF
2501    FF

After execution of program:

2502    01 (LSB)
2503    00 (MSB)

#### P2: 1's compliment and and 2's compliment of 8 bit number.

posted Mar 29, 2013, 5:17 AM by Neil Mathew   [ updated Mar 29, 2013, 5:17 AM by Neil Mathew ]

ALGO: 1's compliment

 Memory Address Opcode Mneumonics Comments 2500 3A LDA 2000H 3A = Load Address to Accumulator 2501 00 LSB First (00 of 2000) 2502 20 MSB Second (20 of 2000) 2503 2F CMA 2504 32 STA 2001H Stores accumulator value 2505 02 LSB (02) 2506 20 MSB(20) 2507 76 HLT Halt (End of Program)

ALGO: 2's compliment

 Memory Address Opcode Mneumonics Comments 2500 3A LDA 2000H 3A = Load Address to Accumulator 2501 00 LSB First (00 of 2000) 2502 20 MSB Second (20 of 2000) 2503 2F CMA 2504 3C INR A Add 1 to compliment to get 2's compliment 2505 32 STA 2001H Stores accumulator value 2506 02 LSB (02) 2507 20 MSB(20) 2508 76 HLT Halt (End of Program)

#### P0: 8085 Basics

posted Mar 29, 2013, 5:08 AM by Neil Mathew   [ updated Mar 29, 2013, 5:08 AM by Neil Mathew ]

 I think the first thing we learnt is to recognize how many bytes of memory each instruction takes.For eg:  MOV A,B takes 1 byte (8 bits) of memory.However, LDA 2000H takes 3 bytes since the complete string takes exactly one byte (represented by an opcode later) and each 8 bit operand takes one byte. So, LDA    20    00  =  3 bytes.

#### P1: 8085 Program to add/subtract two 8 bit numbers.

posted Mar 29, 2013, 4:55 AM by Neil Mathew   [ updated Mar 29, 2013, 5:59 AM by Neil Mathew ]

 Memory Address Opcode Mneumonics Comments 2500 3A LDA 2000H 3A = Load Address to Accumulator 2501 00 LSB First (00 of 2000) 2502 20 MSB Second (20 of 2000) 2503 47 MOV B,A 2504 3A LDA 2001H 2505 01 2506 20 2507 80 ADD B 2508 32 STA 2002H Stores accumulator value 2509 02 LSB (02) 250A 20 MSB(20) 250B 76 HLT Halt (End of Program)

ALGO TO SUBTRACT:

 Memory Address Opcode Mneumonics Comments 2500 3A LDA 2000H 3A = Load Address to Accumulator 2501 00 LSB First (00 of 2000) 2502 20 MSB Second (20 of 2000) 2503 47 MOV B,A 2504 3A LDA 2001H 2505 01 2506 20 2507 90 SUB B Reg A(@2001) - Reg B (@2000) 2508 32 STA 2002H Stores accumulator value 2509 02 LSB (02) 250A 20 MSB(20) 250B 76 HLT Halt (End of Program)

2000    01
2001    04

After execution of program:

2003    05

OUTPUT: (SUB)

2000    01
2001    04

After execution of program:

2003    03

Note the first number is stored at Reg B and the second one into Accumulator. Hence, when the SUB B command is executed, the first number is subtracted FROM the second number.

The memory address states the location where we are entering the hexadecimal Opcode. After this, we supply the two inputs (to add) at 2000H and 2001H. (Say, 01 is entered at 2000H and maybe 04 is entered at 2001H). The location of the start of the program would be at 2500H)

Execute the program at 2500H.

And on execution, check the value at location 2002H. (Answer should be 05).