Simple CPU Emulator Binary arithmetics

Topics

Boolean operators

A boolean operator is basically a logic gate, or for the programmers amongst us a function which takes two arguments A and B. A and B are both binary numbers, which means that they can either be true or false. For an electronical circuit like your home computer this means physically, that there can either be current or no current.

AND

The AND operator is a logical conjunction of A and B. This function is referred to as the AND operator because it is only TRUE IF A AND B ARE TRUE.
The following chart will summarize the results for the AND operation.

ABResult
000
010
100
111

OR

The OR operator is a logical disjunction of A and B. This function is referred to as the OR operator because it is TRUE IF EITHER A OR B ARE TRUE.
The following chart will summarize the results for the OR operation.

ABResult
000
011
101
111

NOT

The NOT operator is a logical negation of only one binary value A. This function is referred to as the NOT operator because it is only TRUE IF NOT A IS TRUE.
The following chart will summarize the results for the NOT operation.

AResult
01
10

XOR

The XOR operator is an exclusive disjunction of A and B. The XOR operator can most simply be described as follows: It is only TRUE IF A, B ARE DIFFERENT.
The following chart will summarize the results for the XOR operation.

ABResult
000
011
101
110

Binary addition & subtraction

To calculate binary numbers more than one wire is laid side by side. Now let's begin by adding some single-digit binary numbers and see what problems we will encounter. See also: Adder (electronics)

ABSumCarry
0000
0110
1010
1101

It seems obvious that, within a single-digit result a "Sum" seems to be the same as a XOR operation. The problem begins when both bits are true, because we have to go on with a carry bit (and while A2 and B2 may both be true already we won't be able to cheat the carry in there either!). The carry has influence on every subsequent bits to calculate, and each of them will be given a carry bit. Let's have a look at the results chart.

CarrypreviousABSumCarrynext
00000
00110
01010
01101
1 0 0 1 0
1 0 1 0 1
1 1 0 0 1
1 1 1 1 1

The first four lines will be plausible, since the results are the same as above. The other lines, marked in red give us completely different results. Regarding A and B the effect on the sum part is NOT(A XOR B), and for the carry it's A OR B.
The following chart shows the circuit for the full adder from Wikipedia.
S = (A XOR B) XOR Cin
The carry is either set if both a and be or both the "sum" and Cin are set.
Cout = (A AND B) OR ((A XOR B) AND Cin)

Full adder circuit diagram Inputs: {A, B, CarryIn} → Outputs: {Sum, CarryOut}

To calculate binary subtraction, I will first introduce negative values of binary numbers, because subtraction can basically be cut short as an addition with negative numbers. To calculate numbers negatively, we will use one (the most significant) bit of the number as a "sign bit".

BinaryUnsignedSigned
000000
000111
001022
001133
010044
010155
011066
011177
10008-8
10019-7
101010-6
101111-5
110012-4
110113-3
111014-2
111115-1

The trick seems plausible, as soon as someone came up with it. The problem is that the range of values must be reallocated. This means, that any number less than -8 and bigger than 7, so instead of deactivating bit 4 (15+1 != 0 / 0-1 != 15), activation of bit 4 will toggle the overflow flag (7+1 != -8 / -8-1 != 7).
The above example covers any wider integers' range to according values, as shown in the following table.

Width (in bits)Unsigned rangeSigned range
8from 0 to 255from -128 to 127
16from 0 to 65535from -32768 to 32767
32from 0 to 4294967295from -2147483648 to 2147483647
64from 0 to 18446744073709551615from -9223372036854775808 to 9223372036854775807

(GPL Copyleft) Martti Kühne, 26.8.2008