Thursday, 24 August 2017

Instruction format (1 to 2 Bytes)


G G G G G M M X    X X X X A A A A 
                    X R R R X r r r

MM


MM = (00)
Register Mode 

MM = (01)
Address Mode

MM = (10)
Immediate Mode

MM = (11)
Page Mode (FUTURE)



GGGG = (00000)

LDR  Regxx,Regyy  (sets Zero Flag)
LDA  Regxx, Memory
LDI  Regxx, #Value

GGGG = (00001)
STA  RegXX,Memory

GGGG = (00010) 
INCR RegXX
INCA Memory

GGGG = (00011) 
ADDR RegXX,RegYY
ADDA RegXX,Memory
ADDI RegXX,Value

GGGG = (00100) 
SUBR RegXX,RegYY
SUBA RegXX,Memory
SUBI RegXX,Value

GGGG = (00101) 
DECR RegXX
DECA Memory

GGGG = (00110) 
MULTR RegXX, RegYY
MULTA RegXX,Memory
MULTI RegXX, Value

GGGG = (00111) 
CMPR REGxx,RegYY   (Carry, Zero)
CMPI REGxx,Value   (Carry, Zero)
CMPA REGxx,Address   (Carry, Zero)

GGGG = (01000) 
BRZ Address
BRC Address

GGGG = (01001) 
HALT

GGGG = (01010) 
OUT RegXX


GGGG = (01011) 

ANDR RegXX,RegYY
ANDI RegXX,Value
ANDA RegXX,Memory

GGGG = (01100)

ORR RegXX,RegYY
ORI RegXX,Value
ORA RegXX,Memory


GGGG = (01101)

NOTR RegXX



GGGG = (01110) 

XORR RegXX,RegYY
XORI RegXX,Value
XORA RegXX,Memory

GGGG = (11111) 
NOP

16 groups (5 bits) - 3 sub modes - (2 bits)

// Future Instructions


PUSH Reg
PUSHF
POPF
POP Reg

CALL Address
CALL Z
CALL NZ
CALL C
RET 
RETZ
RETC
RETNZ

Sunday, 20 August 2017

Monday, 14 August 2017

Tuesday, 25 July 2017

Tuesday, 11 July 2017

Full Adder and Half Adder




package com.johnny.adder;

/**
 *
 * @author johnny
 */
public class Adder {

static public AdderResult halfAdder(int a, int b) {
return new AdderResult(a ^ b, a & b);
}

static public AdderResult fullAdder(int a, int b, int carryIn) {
AdderResult res1 = halfAdder(a, b);
AdderResult res2 = halfAdder(res1.getSum(), carryIn);

return new AdderResult(res2.getSum(), res1.getCarry() | res2.getCarry());
}

}


package com.johnny.adder;

/**
 *
 * @author johnny
 */
public class AdderResult {

private int sum;
private int carry;

public AdderResult(int sum, int carry) {
this.sum = sum;
this.carry = carry;
}

public int getSum() {
return sum;
}

public void setSum(int sum) {
this.sum = sum;
}

public int getCarry() {
return carry;
}

public void setCarry(int carry) {
this.carry = carry;
}

@Override
public String toString() {
return " sum " + getSum() + " carry " + getCarry();
}
}




package com.johnny.adder;

import com.johnny.app.AppException;
import com.johnny.app.AppInf;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author johnny
 */
public class App implements AppInf {

@Override
public String getTitle() {
return "Full Adder";
}

@Override
public void execute(String[] args) {

assertAdder(Adder.fullAdder(0, 0, 0), 0, 0);
assertAdder(Adder.fullAdder(0, 1, 0), 1, 0);
assertAdder(Adder.fullAdder(1, 0, 0), 1, 0);
assertAdder(Adder.fullAdder(1, 1, 0), 0, 1);

assertAdder(Adder.fullAdder(0, 0, 1), 1, 0);
assertAdder(Adder.fullAdder(0, 1, 1), 0, 1);
assertAdder(Adder.fullAdder(1, 0, 1), 0, 1);
assertAdder(Adder.fullAdder(1, 1, 1), 1, 1);

for (int i = -8192; i < 8192; i++) {
for (int j = -8192; j < 8192; j++) {
try {
assertCond((add(i, j) == (i + j)), " sum failed " + i + "+" + j);
} catch (Exception ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}

public int add(int a, int b) {
int sum = 0;
int carryIn = 0;

//for each bit do fullAdder
for (int bitNumber = 0; bitNumber < 32; bitNumber++) {
if (a == 0 && b == 0 && carryIn == 0) {
break;
}
int abit = a & 1;
int bbit = b & 1;
AdderResult res = Adder.fullAdder(abit, bbit, carryIn);
sum |= (res.getSum() << bitNumber);
carryIn = res.getCarry();
a >>= 1;
b >>= 1;
}
return sum;
}

public void assertAdder(AdderResult result, int expectedSum, int expectedCarry) {

try {
assertCond(result.getSum() == expectedSum, "Sum not expected " + expectedSum + ":" + expectedCarry);
assertCond(result.getCarry() == expectedCarry, "Carry not expected " + expectedSum + ":" + expectedCarry);

} catch (AppException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}

}

public void assertCond(boolean condition, String failMessage) throws AppException {
if (!condition) {
throw new AppException(failMessage);
}
}

}


Thursday, 29 June 2017

Cracking the Coding Interview (add function without use of '+') - Attempt number 2


public class Adder2 
{

// full 1 bit Adder (Ci - Carry Input , a  parameter input, b parameter input, o is output , c (also an output) is carry flag used for the next bit addition (state))
/*
Ci  a  b    o    c
0   0  0    0    0
0   0  1    1    0
0   1  0    1    0
0   1  1    0    1
1   0  0    1    0
1   0  1    0    1
1   1  0    0    1
1   1  1 1    1
*/
static Integer [] sequentialBitIndexCheatSet = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30} ; // pre-ordered sequential bit positions starting from 0 to bit length of word - 1 


// n bit adder (depends on size of 'sequentialCheatSet')
static public int add(int a, int b) {

int [] output_logic  = {0,2,2,1,2,1,1,3} ;
int mask = 1 ;
int result = 0;
int carry_in = 0;
int carry_out ;
int output ;
int input_logic_index ;
int abit ;
int bbit ;
int bit_result ;

// This pre-sorted arrayList is purely here so we can skip using an 'add' instruction!

for(Integer bitNumber : sequentialBitIndexCheatSet) {

// 1 bit adder
abit = (1 & a) ;
bbit = (1 & b) ;

input_logic_index = (carry_in << 2) | (abit << 1) | (bbit << 0) ;

output = output_logic[input_logic_index] ;
carry_out = (output & 1) ;
bit_result = ((output & 2) >> 1) ;
carry_in = carry_out ;
result|=(bit_result<<bitNumber) ;
a>>>=1 ; b>>>=1 ;

}


return result  ; //| carry_out;
}

static public void main(String [] args) {
int failed = 0 ;
int tests = 0 ;
int numOfTests = 15 ; //(1<<bitLength) ;
int bitLength = sequentialBitIndexCheatSet.length - 1 ;
for (int i = 0 ; i <  numOfTests ; i++ ) {
for (int j = 0 ; j < numOfTests ; j++) {
tests++ ;
int fullResult = add(i,j) ;
int compare = i + j ;
if (compare != fullResult) {
System.out.println(" i, j " + i + "," + j + " full result = " + fullResult + "<->" + compare + "failed") ;
failed++ ;
}
}
}
System.out.println("Failure count " + failed  + " test count " + tests) ;
return ;
}


}









Cracking the Coding Interview Hard Problem (add function without use of '+') - First Attempt


public class Adder 
{

/*
Ci  a  b    o    c
0   0  0    0    0
0   0  1    1    0
0   1  0    1    0
0   1  1    0    1
1   0  0    1    0
1   0  1    0    1
1   1  0    0    1
1   1  1 1    1
*/

// 4 bit adder
static public int add(int a, int b) {
int [] output_logic  = {0,2,2,1,2,1,1,3} ;
int mask = 1 ;
int result = 0;
int carry_in = 0;
int carry_out ;
int output ;
int input_logic_index ;
int abit ;
int bbit ;
int bit_result ;
// 1 bit adder
abit = (1 & a) ;
bbit = (1 & b) ;

input_logic_index = (carry_in << 2) | (abit << 1) | (bbit << 0) ;

output = output_logic[input_logic_index] ;
carry_out = (output & 1) ;
bit_result = ((output & 2) >> 1) ;
carry_in = carry_out ;
result|=(bit_result<<0) ;
a>>>=1 ; b>>>=1 ;

// 1 bit adder
abit = (1 & a) ;
bbit = (1 & b) ;

input_logic_index = (carry_in << 2) | (abit << 1) | (bbit << 0) ;

output = output_logic[input_logic_index] ;
carry_out = (output & 1) ;
bit_result = ((output & 2) >> 1) ;
carry_in = carry_out ;
result|=(bit_result<<1) ;
a>>>=1 ; b>>>=1 ;

// 1 bit adder
abit = (1 & a) ;
bbit = (1 & b) ;

input_logic_index = (carry_in << 2) | (abit << 1) | (bbit << 0) ;

output = output_logic[input_logic_index] ;
carry_out = (output & 1) ;
bit_result = ((output & 2) >> 1) ;
carry_in = carry_out ;
result|=(bit_result<<2) ;
a>>>=1 ; b>>>=1 ;

// 1 bit adder
abit = (1 & a) ;
bbit = (1 & b) ;

input_logic_index = (carry_in << 2) | (abit << 1) | (bbit << 0) ;

output = output_logic[input_logic_index] ;
carry_out = (output & 1) ;
bit_result = ((output & 2) >> 1) ;
carry_in = carry_out ;
result|=(bit_result<<3) ;
a>>>=1 ; b>>>=1 ;

// 1 bit adder
abit = (1 & a) ;
bbit = (1 & b) ;

input_logic_index = (carry_in << 2) | (abit << 1) | (bbit << 0) ;

output = output_logic[input_logic_index] ;
carry_out = (output & 1) ;
bit_result = ((output & 2) >> 1) ;
carry_in = carry_out ;
result|=(bit_result<<4) ;
a>>>=1 ; b>>>=1 ;

return result  ; //| carry_out;
}

static public void main(String [] args) {
int failed = 0 ;
for (int i = 0 ; i < 15 ; i++ ) {
for (int j = 0 ; j < 15 ; j++) {
int fullResult = add(i,j) ;
int compare = i + j ;
if (compare != fullResult) {
System.out.println(" i, j " + i + "," + j + " full result = " + fullResult + "<->" + compare + "failed") ;
failed++ ;
}
}
}
System.out.println("Failure count " + failed ) ;
return ;
}
}