Sem 6‎ > ‎

MPRC LAB

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
AHEADSHLD 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 Order

if 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, Mn-1 comparisons
DCR C
OUTERLOOPMOV D, C
LXI H, 2501(1st of series)
INNERLOOPMOV A, M
INX H
CMP M
JNC AHEADA < B => Carry.

if Carry, Swap
=> DESC Order
MOV B, MSTART SWAP STTMENTS:
MOV M, A
DCX H
MOV M, B
INX HEND 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? 
Repeated addition of the number. 
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.
OTHERWISE, JUMP TO GO, AND STORE CONTENTS OF 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.
OTHERWISE, JUMP TO GO, AND STORE CONTENTS OF 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 MneumonicsComments
 200021 LXI H, 2500Load Immediately 2500 to H
 200100
 200225

 20037EMOV A. M m points to the content (value) of the register H. That value is copied or moved to accumulator.
 20042FCMACompliment that value.
 200532STA 2502Store it in 2502.
 200602    
 200725
 200823 INX HNow 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 MneumonicsComments
 200021 LXI H, 2501Load Immediately 2501 to H-L pair.
 200101
 200225

 200306 MVI B, 00 Reg B holds the carry, if any.
 200400  
 20057E MOV A,M 
 20062F CMA
 2007C6ADI 01
 200801  
 200932 STA 2503 
 200A03  
 200B25  
 200CD2 JNC 2010Jump 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)

 200D10
 
 200E20  
 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 MneumonicsComments
 25003A LDA 2000H3A = Load Address to Accumulator
 250100LSB First (00 of 2000)
 250220MSB Second (20 of 2000)
 25032FCMA
 250432STA 2001HStores accumulator value
250502LSB (02)
250620MSB(20)
250776HLTHalt (End of Program)

ALGO: 2's compliment

Memory Address Opcode MneumonicsComments
 25003A LDA 2000H3A = Load Address to Accumulator
 250100LSB First (00 of 2000)
 250220MSB Second (20 of 2000)
 25032FCMA
 2504 3C INR AAdd 1 to compliment to get 2's compliment
 250532STA 2001HStores accumulator value
250602LSB (02)
250720MSB(20)
250876HLTHalt (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 ]


ALGO TO ADD:

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 MneumonicsComments
 25003A LDA 2000H3A = Load Address to Accumulator
 250100LSB First (00 of 2000)
 250220MSB Second (20 of 2000)
 250347MOV B,A
 25043ALDA 2001H
 250501
 250620
 250790SUB BReg A(@2001) - Reg B (@2000)
 250832STA 2002HStores accumulator value
 250902LSB (02)
 250A20MSB(20)
 250B76HLTHalt (End of Program)






OUTPUT: (ADD)

    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).

Hexadecimal addition even allows the following:

0 B    ( 0 11)
1 3    ( 1  03)
___

1 E


1-10 of 10