This assignment is completed for the most part. I only need bug fixes and some code may need to be altered to fit the assignment. I would like to have someone read over the assignment for me and edit...

1 answer below »

This assignment is completed for the most part. I only need bug fixes and some code may need to be altered to fit the assignment. I would like to have someone read over the assignment for me and edit the program as such. thank you!


ICSI404 – Assignment 10 – Stack It Up!


This assignment builds on the previous assignment.



Our computer so far can implement all the instructions that can go inside a function (think about C). What it cannot do is call functions, pass parameters, return values and return to where we called from. With this assignment, we will implement the last 4 assembly instructions that we need in order to do these things.



The Stack


CPUs typically have a stack implementation built in. Remember that a stack is like a stack of cards. The last thing in is the first thing out (LIFO). With a stack, we can push and pop values.



Consider, for a moment, how this C program might work:


int add (int a, int b) {



return a+b;


}


void main() {



int c = add(3,4);


}


Of course, it does not output anything, but that’s not our emphasis here. Compilers would typically output something like this in assembly:


add: pop R15 // stack: 3 4


pop R1 // stack: 3



pop R2 // stack: empty



add R1 R2 R3



push R3 // stack: 7



push R15 // stack: 7 RN



return // after this, stack: 7



main: move 3 R1 // stack: empty



move 4 R2



push R1 // stack:3



push R2 // stack: 3 4



call add // stack 3 4 RN


RN: return // stack: 7



The parameters to the function are pushed onto the stack from registers. Inside the function they are popped off of the stack into registers. There are two other new instructions here – call and return. Call is just like jump, but it pushes the address of the NEXT instruction after the call onto the stack. When return (in add) happens, it pops the return address from the stack and moves it into PC.



For this assignment, you will need to create these four instructions (call, return, push, pop). The opcode for all four instructions will be: 0110.


Push will look like this:


0110 0000 0000 RRRR // R = register bit


Pop will look like this:


0110 0100 0000 RRRR // R = register bit


Call will look like this:


0110 10AA AAAA AAAA // A = address bit. These are like jump – absolute and not an offset


Return will look like this:


0110 1100 0000 0000 // No variable bits


You will need to create a stack pointer in your CPU. This is a longword that points to an address in memory that will indicate the NEXT place to write the stack. You should call this SP (stack pointer). It should be pre-initialized to 1020. Why? We are going to start at the end of memory (remember that we have 1024 bytes, so memory ranges from 0 – 1023). We read/write 4 bytes (32 bits) because that’s the size of a register. So the first push would go into bytes 1020, 1021, 1022, 1023. We would subtract 4 from the SP, giving 1016. The next push would go into bytes 1016, 1017, 1018, 1019. Pop would add 4 to the SP.


Call is a combination of push and jump. Return is a combination of pop and jump. Push doesn’t change the register that it is copying from.


You must add these instructions to your assembler.


You must test these new instructions using your assembler (as we did before). Create a small program to test these instructions and place it in cpu_test3.





You must submit buildable .java files for credit.

































































Rubric



Poor



OK



Good



Great



Comments



None/Excessive (0)



“What” not “Why”, few (5)



Some “what” comments or missing some (7)



Anything not obvious has reasoning (10)



Variable/Function naming



Single letters everywhere (0)



Lots of abbreviations (5)



Full words most of the time (8)



Full words, descriptive (10)



Unit Tests



None (0)



Partial Coverage


(7)



All methods covered, needs more cases (13)



All methods/cases covered (20)



Push



None (0)



Attempted (8)






Completely working (15)



Pop



None (0)



Attempted (8)






Completely working (15)



Call



None (0)



Attempted (8)






Completely working (15)



Return



None (0)



Attempted (8)






Completely working (15)








Answered Same DayMay 07, 2021

Answer To: This assignment is completed for the most part. I only need bug fixes and some code may need to be...

Sandeep Kumar answered on May 07 2021
134 Votes
Homework/HW1/Bit.java
Homework/HW1/Bit.java
public class Bit implements IBit, Comparable {
    private byte bit = 0;
    public Bit(int value) { //constructor, sets bit to 1 unless value is 0
        set(value);
    }
    public Bit() { //default constructor, sets bit to 0
        this(0);
    }
    public void set(int value) { //sets value of bit
        if (value != 0)
            bit = 1;
        else
            bit = 0;
    }
    public void set() { //sets value of bit to 1
        set(1);
    }
    public void clear() { //clears value of bit
        set(0);
    }
    public void toggle() { //toggles value of bit
        if (bit == 1)
            clear();
        else
            set();
    }
    public int getValue(){ //returns value of bit
        return bit;
    }
    public Bit and(Bit b){ //returns the result of this bit AND b
        if (bit == 1 && b.getValue() == 1)
            return new Bit(1);
        else
            return new Bit(0);
    }
    public Bit or(Bit b){ //returns the result of this bit OR b
        if (bit == 0 && b.getValue() == 0)
            return new Bit(0);
        else
            return new Bit(1);
    }
    public Bit xor(Bit b){ //returns the result of this bit XOR b
        if ((bit == 1 && b.getValue() == 0 )|| (bit == 0 && b.getValue() == 1))
            return new Bit(1);
        else
            return new Bit(0);
    }
    public Bit not(){ //returns the value of this value NOT
        if (bit == 0)
            return new Bit(1);
        else
            return new Bit(0);
    }
    @Override 
    public String toString(){
        return Integer.toString(bit);
    }
    @Override
    public int compareTo(Bit b){
        if (bit == b.getValue())
            return 0;
        else if (bit < b.getValue())
            return -1;
        else
            return 1;
    }
    public boolean equals(Bit b){ //checks whether this and b are equal
        return bit == b.getValue() ? true : false;
    }
}
Homework/HW1/bit_test.java
Homework/HW1/bit_test.java
public class bit_test {
    public static void main(String[] args) {
        runtests();
    }
    public static void runtests() {
        if ( testSetClearToggle())
            System.out.println("Set/Clear/Toggle Passed!");
        else
            System.out.println("Set/Clear/Toggle failed! :(");

        if (testAnd())
            System.out.println("AND Passed!");
  else
            System.out.println("AND failed! :(");
        if (testOr())
            System.out.println("OR Passed!");
        else
            System.out.println("OR failed! :(");

        if (testXor())
            System.out.println("XOR Passed!");
        else
            System.out.println("XOR failed! :(");
        if (testNot())
            System.out.println("NOT Passed!");
        else
            System.out.println("NOT failed! :(");
        if (testToString())
            System.out.println("ToString Passed!");
        else
            System.out.println("ToString failed! :(");
    }

    public static boolean testSetClearToggle(){
        if (new Bit(1).getValue() != 1) return false;
        if (new Bit(67).getValue() != 1) return false;
        if (new Bit(0).getValue() != 0) return false;
        Bit b = new Bit();
        b.set(79);
        if (b.getValue() != 1) return false;
        b.clear();
        if (b.getValue() != 0) return false;
        b.toggle();
        if (b.getValue() != 1) return false;

        return true;
    }
    public static boolean testAnd(){
        if (new Bit(0).and(new Bit(0)).getValue() != 0) return false;
        if (new Bit(0).and(new Bit(1)).getValue() != 0) return false;
        if (new Bit(1).and(new Bit(0)).getValue() != 0) return false;
        if (new Bit(1).and(new Bit(1)).getValue() != 1) return false;
        return true;
    }
    public static boolean testOr(){
        if (new Bit(0).or(new Bit(0)).getValue() != 0) return false;
        if (new Bit(0).or(new Bit(1)).getValue() != 1) return false;
        if (new Bit(1).or(new Bit(0)).getValue() != 1) return false;
        if (new Bit(1).or(new Bit(1)).getValue() != 1) return false;
        return true;
    }
    public static boolean testXor(){
        if (new Bit(0).xor(new Bit(0)).getValue() != 0) return false;
        if (new Bit(0).xor(new Bit(1)).getValue() != 1) return false;
        if (new Bit(1).xor(new Bit(0)).getValue() != 1) return false;
        if (new Bit(1).xor(new Bit(1)).getValue() != 0) return false;
        return true;
    }
    public static boolean testNot(){
        if (new Bit(0).not().getValue() != 1) return false;
        if (new Bit(1).not().getValue() != 0) return false;
        return true;
    }
    public static boolean testToString(){
        if (! new Bit(0).toString().equalsIgnoreCase("0")) return false;
        if (! new Bit(1).toString().equalsIgnoreCase("1")) return false;
        return true;
    }
}
Homework/HW1/IBit.java
Homework/HW1/IBit.java
public interface IBit {
    void set(int value); // sets the value of the bit
    void toggle(); // changes the value from 0 to 1 or 1 to 0
    void set(); // sets the bit to 1
    void clear(); // sets the bit to 0
    int getValue(); // returns the current value
    Bit and(Bit other); // performs and on two bits and returns a new bit set to the result
    Bit or(Bit other); // performs or on two bits and returns a new bit set to the result
    Bit xor(Bit other); // performs xor on two bits and returns a new bit set to the result
    Bit not(); // performs not on the existing bit, returning the result as a new bit
    @Override
    String toString(); // returns “0” or “1”
}
Homework/HW10/cpu_test3.java
Homework/HW10/cpu_test3.java
public class cpu_test3 {
    public static void main(String[] args) {
        runtests();
    }
    public static void runtests(){
        try {
            testCPU();
            System.out.println("CPU Passed All Tests!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void testCPU() throws Exception{
        Computer m1xmacbookpro = new Computer();

        try {
            m1xmacbookpro.assemble(new String[]{
                "move R0 5 ", //0
                "MOVE R1 7", //2
                "COMPARE R0 R1",// 4
                "BNE 8", //6
                "HALT", //8
                "MULTIPLY R0 R1 R7", //10
                "RETURN",
                "PUSH R1", //14
                "CALL 10",  //16
                "INTERRUPT 1", //18
                "MOVE R1 65",
                "POP R1",
                "INTERRUPT 0",
                "HALT",
            });

            m1xmacbookpro.run();
 
        } catch (Exception e) {
            throw e;
        }
        m1xmacbookpro = new Computer();
        try {
            m1xmacbookpro.assemble(new String[]{
                "MOVE R0 99 ", //0
                "MOVE R7 5 ", //2
                "MULTIPLY R0 R7 R2 ",
                "MOVE R1 1",
                "MULTIPLY R1 R2 R3 ",
                "COMPARE R2 R3", //10
                "BEQ 26", //12
                "INTERRUPT 0", 
                "HALT",
                "CALL 12", //18
                "RETURN",
                "PUSH R2", //22
                "MOVE R2 86",
                "INTERRUPT 0",
                "MULTIPLY R2 R0 R15",
                "POP R9",
                "RETURN",
                "HALT",
                "HALT", 
                "PUSH R0", //38
                "MOVE R0 5",
                "POP R6",
                "CALL 22",
                "ADD R6 R9 R11",
                "COMPARE R6 R11",
                "CALL 18",
            });
            m1xmacbookpro.run();
        }catch (Exception e) {
                throw e;
        }
        m1xmacbookpro = new Computer();
        try {
            m1xmacbookpro.assemble(new String[]{
                "MOVE R0 23 ", //0
                "MOVE R1 45 ", //2
                "MULTIPLY R0 r1 R2 ",
                "MOVE r3 69",
                "SUBTRACT R0 R2 R7 ",
                "COMPARE R0 R7", //10
                "BGT 6", //12
                "JUMP 12", //infinite loop if above cond is false
                "HALT", 
                "INTERRUPT 0", //18
                "JUMP 24",
                "HALT", //22
                "MOVE R7 1035",
                "COMPARE R2 R7",
                "BGE -6",
                "JUMP 0",
            });
            m1xmacbookpro.run();
        }catch (Exception e) {
                throw e;
        }

    }
}
Homework/HW2/Longword.java
Homework/HW2/Longword.java
public class Longword {

    public final byte LONGWORD_SIZE = 32;
    private Bit[] bits;
    public Longword(){ // default constructor, all bits set to 0
        bits = new Bit[LONGWORD_SIZE];
        for (int i = 0; i < LONGWORD_SIZE; i++) {
            bits[i] = new Bit();
        }
    }
    public Longword(Longword l){ // constructor, all bits copied from other longword
        bits = new Bit[LONGWORD_SIZE];
        copy(l);
    }
    public Longword(int value){ // constructor, with value as signed int
        this();
        set(value);
    }
    public Bit getBit(int i){  // Get bit i
        return bits[i];
    }
    public void setBit(int i, Bit value){ // set bit i's value
        bits[i].set(value.getValue());
    }
    public void setBit(int i, int value){ // set bit i's value
        bits[i].set(value);
    }
    public Longword and(Longword b){ // and two longwords, returning a third
        Longword a = new Longword();
        for (int i = 0; i < LONGWORD_SIZE; i++) {
            a.setBit(i, bits[i].and(b.getBit(i)));
        }
        return a;
    }

    public Longword or(Longword b){ // or two longwords, returning a third
        Longword a = new Longword();
        for (int i = 0; i < LONGWORD_SIZE; i++) {
            a.setBit(i, bits[i].or(b.getBit(i)));
        }
        return a;
    }
    public Longword xor(Longword b){ // xor two longwords, returning a third
        Longword a = new Longword();
        for (int i = 0; i < LONGWORD_SIZE; i++) {
            a.setBit(i, bits[i].xor(b.getBit(i)));
        }
        return a;
    }

    public Longword not(){ // negate this longword, creating another
        Longword a = new Longword();
        for (int i = 0; i < LONGWORD_SIZE; i++) {
            a.setBit(i, bits[i].not());
        }
        return a;
    }
    Longword rightShift(int amount){ // rightshift this longword by amount bits, creating a new longword
        Longword a = new Longword();
        for (int i = amount, j =0; i < LONGWORD_SIZE; i++, j++) {
            a.setBit(i, bits[j]);
        }
        return a;
    }
    Longword leftShift(int amount){ // leftshift this longword by amount bits, creating a new longword
        Longword a = new Longword();
        for (int i = 0, j = amount; j < LONGWORD_SIZE; i++, j++) {
            a.setBit(i, bits[j]);
        }
        return a;
    }
    @Override 
    public String toString(){ // returns the longword as a string i.e 0000 0000 0000 0001
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < bits.length; i++) {
            str.append(bits[i].getValue());
            if (i != 0 && (i+1)%4 == 0) //every 4 bits adds a space to seperate
                str.append(" ");
        }
        return str.toString();
    }
    public long getUnsigned() { // returns the value of this longword as a long
        long num = 0;
        long power = 1;
        for (int i = LONGWORD_SIZE - 1; i >= 0; i--) {
            Bit b = bits[i];
            num = num + (Long.parseLong(b + "") * power);
            power = power * 2;
        }
        return num;
    }
    public int getSigned(){ // returns the value of this longword as an int
        int num = 0;
        int power = 1;
        if(bits[0].getValue() == 1){
            for (int i = LONGWORD_SIZE - 1; i >= 1; i--) {
                Bit b = bits[i].not();
                num = num + (Integer.parseInt(b + "") * power);
                power = power * 2;
            }
            num +=1;
            num *= -1;
        } else {
            for (int i = LONGWORD_SIZE - 1; i >= 0; i--) {
                Bit b = bits[i];
                num = num + (Integer.parseInt(b + "") * power);
                power = power * 2;
            }
        }
        return num;
    }
    public Longword twosComplement(){ // returns the two's complement of the longword
        return new Longword(this.not().getSigned() + 1);
    }
    public Longword extendSignAt(int index){ // extends sign from bit[index] to first bit in longword
        if(index > LONGWORD_SIZE)
        index = LONGWORD_SIZE;
        Longword signExtended = new Longword(this);
        for (int i = 0; i < index && i < LONGWORD_SIZE; i++) {
            signExtended.setBit(i, bits[index]);
        }
        return signExtended;
    }
    public void copy(Longword l){ // copies the values of the bits from another longword into this one
        for (int i = 0; i < LONGWORD_SIZE; i++) {
            bits[i] = new Bit(l.getBit(i).getValue());
        }
    }
    public void set(int value){  // set the value of the bits of this longword (used for tests)
        String sbits = Long.toBinaryString(Integer.toUnsignedLong(value) | 0x100000000L).substring(1); //converts value into 32bit binary as a string
        for (int i = 0; i < LONGWORD_SIZE; i++) {
            bits[i].set(sbits.charAt(i) - 48); // 48 is dec for char 0, 49 = 1
        }
    }
    public Longword plus(Longword b){ // adds b to this longword, creating a new longword
        return RippleAdder.add(this, b);
    }
    public Longword plus(int b){ // adds b to this longword, creating a new longword
        return plus(new Longword(b));
    }
    public Longword minus(Longword b){ // subtracts b from this longword, creating a new longword
        return RippleAdder.sub(this, b);
    }
    public Longword minus(int b){ // subtracts b from this longword, creating a new longword
        return minus(new Longword(b));
    }
    public Longword times(Longword b){ // multiplies this longword times b, creating a new longword
        return Multiplier.multiply(this, b);
    }
    public Longword times(int b){ // multiplies this longword times b, creating a new longword
        return times(new Longword(b));
    }
}
Homework/HW2/longword_test.java
Homework/HW2/longword_test.java
public class longword_test {
    public static void main(String[] args) {
        runtests();
    }
    public static void runtests() {
        if ( testSetGet())
            System.out.println("SetBit/Get Passed!");
        else
            System.out.println("SetBit/Get failed! :(");
        if ( testSetCopy())
            System.out.println("Set/Copy Passed!");
        else
            System.out.println("Set/Copy failed! :(");

        if (testAnd())
            System.out.println("AND Passed!");
        else
            System.out.println("AND failed! :(");
        if (testOr())
            System.out.println("OR Passed!");
        else
            System.out.println("OR failed! :(");

        if (testXor())
            System.out.println("XOR Passed!");
        else
            System.out.println("XOR failed! :(");
        if (testNot())
            System.out.println("NOT Passed!");
        else
            System.out.println("NOT failed! :(");
        if (testLeftShift())
            System.out.println("LeftShift Passed!");
        else
            System.out.println("LeftShift failed! :(");

        if (testRightShift())
            System.out.println("RightShift Passed!");
        else
            System.out.println("RightShift failed! :(");
        if (testToString())
            System.out.println("ToString Passed!");
        else
            System.out.println("ToString failed! :(");
        if (testGetSigned())
            System.out.println("getSigned Passed!");
        else
            System.out.println("getSigned failed! :(");
        if (testGetUnsigned())
            System.out.println("getUnsigned Passed!");
        else
            System.out.println("getUnsigned failed! :(");
    }
    public static boolean testSetGet(){
        Longword l = new Longword();
        l.setBit(0, new Bit(1));
        if (l.getBit(0).getValue() != 1) return false;
        l.setBit(1, new Bit(67));
        if (l.getBit(1).getValue() != 1) return false;
        l.setBit(0, new Bit(0));
        if (l.getBit(0).getValue() != 0) return false;
        return true;
    }

    public static boolean testSetCopy(){
        Longword l = new Longword();
        Longword l2;
        l.set(56);
        if (l.getSigned() != 56) return false;
        l2 = new Longword(l);
        if (l2.getSigned() != l.getSigned()) return false;
        l.set(-58329);
        if (l.getSigned() != -58329) return false;
        l2.copy(l);
        if (l2.getSigned() != l.getSigned()) return false;
        l.set(1);
        if (l.getSigned() != 1) return false;
        l2.copy(l);
        if (l2.getSigned() != l.getSigned()) return false;
        return true;
    }
    public static boolean testAnd(){
        if (new Longword(0).and(new Longword(0)).getSigned() != 0) return false;
        if (new Longword(-2).and(new Longword(99)).getSigned() != 98) return false;
        if (new Longword(23).and(new Longword(-23)).getSigned() != 1) return false;
        if (new Longword(2327837).and(new Longword(-242913)).getSigned() != 2097437) return false;
        return true;
    }
    public static boolean testOr(){
        if (new Longword(0).or(new Longword(1)).getSigned() != 1) return false;
        if (new Longword(-2).or(new Longword(99)).getSigned() != -1) return false;
        if (new Longword(23).or(new Longword(-23)).getSigned() != -1) return false;
        if (new Longword(56).or(new Longword(604)).getSigned() != 636) return false;
        return true;
    }
    public static boolean testXor(){
        if (new Longword(1).xor(new Longword(3)).getSigned() != 2) return false;
        if (new Longword(-2).xor(new Longword(99)).getSigned() != -99) return false;
        if (new Longword(23).xor(new Longword(-23)).getSigned() != -2) return false;
        if (new Longword(56).xor(new Longword(604)).getSigned() != 612) return false;
        return true;
    }
    public static boolean testNot(){
        if (new Longword(0).not().getSigned() != -1) return false;
        if (new Longword(1).not().getSigned() != -2) return false;
        return true;
    }
    public static boolean testRightShift(){
        Longword l = new Longword(-1); //...1111111...
        l = l.rightShift(1);
        if (l.getSigned() != 2147483647) return false;
        l = new Longword(-1);
        l = l.rightShift(3);
        if (l.getSigned() != 536870911) return false;
        l = new Longword(-1);
        l = l.rightShift(5);
        if (l.getSigned() != 134217727) return false;
        return true;
    }
    public static boolean testLeftShift(){
        Longword l = new Longword(-1); //...1111111...
        l = l.leftShift(1);
        if (l.getSigned() != -2) return false;
        l = new Longword(-1);
        l = l.leftShift(3);
        if (l.getSigned() != -8) return false;
        l = new Longword(-1);
        l = l.leftShift(5);
        if (l.getSigned() != -32) return false;

        return true;
    }
    public static boolean testToString(){
        if (! new Longword(0).toString().equalsIgnoreCase("0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0")) return false;
        if (! new Longword(1).toString().equalsIgnoreCase("0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1")) return false;
        if (! new Longword(-1).toString().equalsIgnoreCase("1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1")) return false;
        return true;
    }
    public static boolean testGetSigned(){
        if (new Longword(-1).getSigned() != -1) return false;
        if (new Longword(53).getSigned() != 53) return false;
        if (new Longword(218430).getSigned() != 218430) return false;
        return true;
    }
    public static boolean testGetUnsigned(){
        if (new Longword(-1).getUnsigned() != 4294967295L) return false;
        if (new Longword(53).getUnsigned() != 53) return false;
        if (new Longword(218430).getUnsigned() != 218430) return false;
        return true;
    }
}
Homework/HW3/RippleAdder.java
Homework/HW3/RippleAdder.java
public class RippleAdder {
    public static Longword add(Longword a, Longword b){ //takes two longwords and adds them together
        if(a.getSigned() < 0) //if one of the longwords is negative
            return subtract(b, twosComplement(a));
        else if(b.getSigned() < 0)
            return subtract(a, twosComplement(b));

        Longword c = new Longword();
        int carry = 0;
        for (int i = c.LONGWORD_SIZE-1  ; i >= 0; i--) {
            int add =  a.getBit(i).getValue() + b.getBit(i).getValue() + carry;
            if (carry == 0 && add > 1){ // 1 + 1 + 0(carry)
                c.setBit(i, 0);
                carry++;
            }else if (carry > 0 && add > 2){ // 1 + 0 + 1(carry)
                c.setBit(i, 1);
            }else if (carry > 0 && add > 1){ // 1 + 1 + 1(carry)
                c.setBit(i, 0);
            }else { // 0 + 0 + 1(carry) or 1 + 0 + 1(carry) or 1 + 0 + 0(carry)
                c.setBit(i, add);
                if (carry != 0)
                    carry--;
            }
        }
        return c;
    }
    public static Longword subtract(Longword a, Longword b){ //takes two longword and subtracts one from the other
        if(a.getSigned() < 0 && b.getSigned() < 0) //if both longwords are negative
            return subtract(twosComplement(b), twosComplement(a));
        Longword aa = new Longword(a); //backups a because a can get modified during subtraction
        Longword c = new Longword();
        for (int i = c.LONGWORD_SIZE-1  ; i >= 0; i--) {
            int sub =  a.getBit(i).getValue() - b.getBit(i).getValue();
            if (sub < 0){ //0 - 1
                int borrow = borrow(i, a); //i tried skipping this step to save mem but it forces me to save the value 
                sub = a.getBit(i).getValue() - b.getBit(i).getValue() + borrow; //should now be 1 - 1 + 1
            }
            c.setBit(i, sub);
        }
        a.copy(aa); //sets a back to what it started as
        return c;
    }
    public static Longword sub(Longword a, Longword b){ //subtract with a shorter method name
        return subtract(a, b);
    }
    public static Longword twosComplement(Longword a){ // returns the two's complement of a
        return add(a.not(),new Longword(1));
    }
    private static int borrow(int index, Longword a){ //recursively borrows a 1 from the nearest bit = 1 
        if(index == 0){
            a.setBit(index, 1);
            return 1;
        }else if (a.getBit(index - 1).getValue() == 1){
            a.setBit(index - 1, 0);
            a.setBit(index, 1);
            return 1;
        }else{
            a.setBit(index, borrow(index - 1, a));
            return 1;
        }
    }

}
Homework/HW3/rippleAdder_test.java
Homework/HW3/rippleAdder_test.java
public class rippleAdder_test {
    public static void main(String[] args) {
        runtests();
    }
    public static void runtests(){
        try {
            testAdd();
            System.out.println("Add Passed!");
        } catch (Exception e) {
            System.err.println(e.toString());
        }
        try {
            testSub();
            System.out.println("Subtract Passed!");
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
    public static void testAdd() throws Exception{
        if (RippleAdder.add(new Longword(1), new Longword(1)).getSigned() != 2) throw new Exception("Add failed @ 1 + 1 = 2");
        if (RippleAdder.add(new Longword(23), new Longword(1)).getSigned() != 24) throw new Exception("Add failed @ 23 + 1 = 24");
        if (RippleAdder.add(new Longword(50), new Longword(50)).getSigned() != 100) {
            //System.out.println(RippleAdder.add(new Longword(50), new Longword(50)).getSigned());
            throw new Exception("Add failed @ 50 + 50 = 100");}
        if (RippleAdder.add(new Longwor...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here