Sem 6‎ > ‎MPRC LAB‎ > ‎

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

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

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


    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.

 200306 MVI B, 00 Reg B holds the carry, if any.
 20057E MOV A,M 
 20062F CMA
 2007C6ADI 01
 200932 STA 2503 
 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)

 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.


    2500    FF
    2501    FF

    After execution of program:

    2502    01 (LSB)
    2503    00 (MSB)