MOAL.computer_organization package

Submodules

MOAL.computer_organization.bcd module

MOAL.computer_organization.bcd._show_bcd(num, decimals, binary, bcd)[source]
MOAL.computer_organization.bcd.dec_to_bcd_8421(num)[source]

Convert a decimal to binary, and decompress into Binary Coded Decimal. Adds trailing bits to the left to enforce a 4-bit “nibble” on all digits. Uses 8421 notation [see wikipedia.org/wiki/Binary-coded_decimal]

MOAL.computer_organization.bcd.dec_to_bcd_excess3(num, bias=3)[source]

Converts a binary to Binary Coded Decimal, then converts again to excess-3 BCD, which has a ‘bit bias’ of bias, where bits are shifted by the given bias. See wikipedia.org/wiki/Excess-3 for more.

MOAL.computer_organization.bit_field module

MOAL.computer_organization.bitwise_operations module

MOAL.computer_organization.bitwise_operations.DELAY = 0.4

wikipedia.org/wiki/Bitwise_operation#Bitwise_operators

(__)

/ – __________/__

/_| | BinaryToday |
|___ | 01101010 |
| —- | |
|_____

*____/|________|> *

MOAL.computer_organization.bitwise_operations._checkbits(bits1, bits2)[source]
MOAL.computer_organization.bitwise_operations._logical_shift_left(bits)[source]
MOAL.computer_organization.bitwise_operations._logical_shift_right(bits)[source]
MOAL.computer_organization.bitwise_operations._shift_circular(bits)[source]
MOAL.computer_organization.bitwise_operations._shift_left(bits)[source]

Shift bits to the left <<<, carrying the bit over to the right; e.g. 0100 << 1000

MOAL.computer_organization.bitwise_operations._shift_right(bits)[source]

Shift bits to the right >>>, carrying the bit over to the left, which preserves the sign; e.g. 0100 >> 0010

MOAL.computer_organization.bitwise_operations._show(bits, op, res)[source]
MOAL.computer_organization.bitwise_operations._show2(bits1, bits2, op, res)[source]
MOAL.computer_organization.bitwise_operations.bitwise_and(bits1, bits2)[source]

Do a logical AND on each pair of bits, by way of multiplication: e.g. 1 x 1 = 1, 0 x 0 / 0 x 1 = 0

MOAL.computer_organization.bitwise_operations.bitwise_not(bits)[source]

NOT is straightforward - a simple one’s complement will do, since the bits are merely flipped.

MOAL.computer_organization.bitwise_operations.bitwise_or(bits1, bits2)[source]

Do a logical OR on each pair of bits: e.g. 1/1 = 1, 0/1 = 1, 0/0 = 0

MOAL.computer_organization.bitwise_operations.bitwise_xor(bits1, bits2)[source]

Do a logical XOR on each pair of bits: e.g. 1/1 = 0, 0/1 = 1, 0/0 = 0

MOAL.computer_organization.bitwise_operations.rotate_through_carry(bits)[source]

Like circular, but the end bits are stored as flags.

MOAL.computer_organization.bitwise_operations.shift_circular(bits, count=1)[source]

A circular shift, where the ends are rotated; e.g. [0]10[1] > [1]10[0]

MOAL.computer_organization.bitwise_operations.shift_left(bits, count=1, logical=False)[source]
MOAL.computer_organization.bitwise_operations.shift_right(bits, count=1, logical=False)[source]
MOAL.computer_organization.bitwise_operations.test_operator_pairs(pairs, bitwise_op)[source]

Test and assert two bit groups against the return value of given op

MOAL.computer_organization.data_types module

class MOAL.computer_organization.data_types.BaseDataType[source]

Bases: object

The irony here is that these data types take up significantly more actual memory as a class representation in a bytecode interpreted language. The classing is merely for intuitive understanding and representing the hierarchy/operations.

__str__()[source]
static _alter(old_bit, new_bit, value)[source]

An interesting visualization of flipping bits – not very robust; primarily meant as a basic learning exercise – see other relevant modules for more specific implementations.

static add(binval, amount)[source]
static decrement(value)[source]
static divide(binval, amount)[source]
get_least_significant()[source]
get_max_binvals()[source]
get_most_significant()[source]
static increment(value)[source]
static multiply(binval, amount)[source]
static subtract(binval, amount)[source]
class MOAL.computer_organization.data_types.Bit(value)[source]

Bases: MOAL.computer_organization.data_types.BaseDataType

__init__(value)
__str__()
class MOAL.computer_organization.data_types.Byte(value)[source]

Bases: MOAL.computer_organization.data_types.Nibble

Also sometimes referred to as an Octet

__init__(value)
class MOAL.computer_organization.data_types.Halfword(value)[source]

Bases: MOAL.computer_organization.data_types.Nibble

__init__(value)
class MOAL.computer_organization.data_types.Nibble(value)[source]

Bases: MOAL.computer_organization.data_types.Bit

__init__(value)
class MOAL.computer_organization.data_types.Word(value)[source]

Bases: MOAL.computer_organization.data_types.Nibble

__init__(value)
MOAL.computer_organization.data_types.bin_dec(value)
MOAL.computer_organization.data_types.bin_inc(value)
MOAL.computer_organization.data_types.update_animation(steps, func, instance)[source]

MOAL.computer_organization.endianness module

MOAL.computer_organization.endianness.DEBUG = False

‘Endianness’ represents byte ordering when memory is addressed in a computer - little endian stores the smallest byte first, whereas the biggest bit is stored first as big endian.

See betterexplained.com/articles/
understanding-big-and-little-endian-byte-order/ for a nice overview.

Also, see linux.die.net/man/3/<function> for real network conversion functions e.g. [htons, htons, ntohl, ntohs]

And see linux.die.net/man/3/endian for base conversion functions e.g. [htobe16, htole16, be16toh, le16toh, htobe32, htole32, be32toh, le32toh, htobe64, htole64, be64toh, le64toh]

Free online copy of Gulliver’s travels available here: literatureproject.com/gulliver-travel/

Character list: cliffsnotes.com/literature/g/gullivers-travels/character-list

class MOAL.computer_organization.endianness.Endian[source]

A machine would probably never MIX little AND big endian byte ordering, but this is an abstraction for practicing some concepts.

__contains__(key)[source]
__getitem__(value)[source]
__init__()[source]
__setitem__(key, value)[source]
__str__()[source]
_store(bits)[source]
add_bom(string)[source]
endian_adapter(bits, order='big')[source]
read_bom(string)[source]
store_big_endian(bits)[source]
store_little_endian(bits)[source]

MOAL.computer_organization.factoradic module

MOAL.computer_organization.factoradic.DEBUG = False

Factoradic numeral system: en.wikipedia.org/wiki/Factorial_number_system

MOAL.computer_organization.factoradic.decimal_to_factoradic(*args, **kwargs)[source]
MOAL.computer_organization.factoradic.factoradic_to_decimal(*args, **kwargs)[source]

Converts a factoradic number to decimal form.

Args:
factoradic (int) - the factoradic number to convert
Returns:
res (int) - the converted decimal
>>> factoradic_to_decimal('341010')
>>> 463
MOAL.computer_organization.factoradic.factpos(num, pos)[source]

Take a number and position and get the position factorial multiplied by the number.

Args:
num (int) - the integer number to use pos (int) - the position to find the factorial over
Returns:
(int) - the integer result
>>> factpos(2, 2)
>>> 4

MOAL.computer_organization.numerical_encoding_basic module

MOAL.computer_organization.numerical_encoding_basic._scale(base, maximum)[source]

Creates a range of numbers up to maximum, given a base. The scale is used as an intuitive reference for visually converting numbers.

See youtube.com/watch?v=e3mCABVEK88 for some good examples.

e.g.

Note: The visual order is reversed, but the returned list is not.

MOAL.computer_organization.numerical_encoding_basic._tostr_joined(ints)[source]
MOAL.computer_organization.numerical_encoding_basic._tounicode(ints)[source]
MOAL.computer_organization.numerical_encoding_basic.all_powers_n(num, power)[source]

Powers of two check, extended to any exponent

MOAL.computer_organization.numerical_encoding_basic.all_powers_two(num)[source]

Find the various powers of two for a given num – often used for finding binary values of a num, since binary is base 2.

MOAL.computer_organization.numerical_encoding_basic.bin_to_dec(num)[source]

Convert binary to integer

MOAL.computer_organization.numerical_encoding_basic.bin_to_hex(num)[source]

Convert binary to hexadecimal. Uses integer conversion as an intermediate step.

MOAL.computer_organization.numerical_encoding_basic.bin_to_oct(binary)[source]

Convert binary to octal. Uses integer conversion as an intermediate step.

MOAL.computer_organization.numerical_encoding_basic.bina()[source]
MOAL.computer_organization.numerical_encoding_basic.compare_to_native(native, custom, count=20, assert_results=False, prefix=None, supress_other=True)[source]

Compares custom numerical converter to the native python version with a list of nums.

MOAL.computer_organization.numerical_encoding_basic.dec_to_bin(num)[source]

Convert integer to binary. Only handles positive integers.

MOAL.computer_organization.numerical_encoding_basic.dec_to_hex(num)[source]

Convert integer to hexadecimal

MOAL.computer_organization.numerical_encoding_basic.dec_to_oct(num)[source]

Convert an integer to an octal, using ‘divide by base’ technique.

MOAL.computer_organization.numerical_encoding_basic.deca()[source]
MOAL.computer_organization.numerical_encoding_basic.divide_by(num, base, to_int=False, joined=False)[source]

General purpose numeral converter, using the divide-by N + modulo technique. Returns a list of integers, or optionally, a joined string, or a converted list of integers as strings.

MOAL.computer_organization.numerical_encoding_basic.genr()[source]
MOAL.computer_organization.numerical_encoding_basic.hex_to_bin(hexnum)[source]

Convert hexadecimal to binary

MOAL.computer_organization.numerical_encoding_basic.hex_to_dec(hexnum)[source]

Convert hexadecimal to integer

MOAL.computer_organization.numerical_encoding_basic.hex_to_oct(hexnum)[source]

Convert hexadecimal to octal

MOAL.computer_organization.numerical_encoding_basic.hexa()[source]
MOAL.computer_organization.numerical_encoding_basic.make_groups(string, count)[source]

Make a set of groups with a string.

e.g. >>> make_groups(‘ABCDEFGH’, 2) = [[‘AB’, ‘CD’, ‘EF’, ‘GH’]] Raises ValueError if modulus operation results in uneven group distribution.

MOAL.computer_organization.numerical_encoding_basic.oct_to_bin(num)[source]

Convert octal to binary instructions used for algorithm from: wikipedia.org/wiki/Octal#Octal_to_binary_conversion

MOAL.computer_organization.numerical_encoding_basic.oct_to_dec(num)[source]

Convert an octal to integer. algorithm/notes from robotroom.com/numSystems4.html

MOAL.computer_organization.numerical_encoding_basic.oct_to_hex(octnum)[source]

Convert octal to hexadecimal. Uses binary conversion as an intermediate step. Instructions used for algorithm from: wikipedia.org/wiki/Octal#Octal_to_hexadecimal_conversion

MOAL.computer_organization.numerical_encoding_basic.octa()[source]
MOAL.computer_organization.numerical_encoding_basic.powers_mult(num, power)[source]

Visualization of all values of given num raise up to a max power.

MOAL.computer_organization.numerical_encoding_basic.powers_ten(num)[source]

A visualization of the powers of ten technique for a given num

MOAL.computer_organization.one_twos_complement module

MOAL.computer_organization.one_twos_complement.ones_complement(binary)[source]
MOAL.computer_organization.one_twos_complement.twos_complement(binary)[source]

Conversion algorithm from cs.cornell.edu/~tomf/notes/cps104/twoscomp.html#twotwo

MOAL.computer_organization.positional module

MOAL.computer_organization.positional.dec_to_n(num, base, alphabet=None)[source]

Convert a decimal to any base, using the divide by N technique. See divide_by for details.

MOAL.computer_organization.positional.dec_to_unary(num)[source]

Tally representation e.g. | = 1, / = 5

MOAL.computer_organization.positional.n_to_dec(num, base, alphabet=None)[source]

Convert a number to decimal from any given base, using the powers technique (e.g. 9203 = 9^3 + 2^2 + 0^1 + 3^0)

Research examples from: www.cs.trincoll.edu/~ram/cpsc110/inclass/conversions.html

MOAL.computer_organization.positional.test_base(base_info, nums=[4, 256, 512, 4096, 9999])[source]

Convert a base with a set of numbers, and display the results.

MOAL.computer_organization.skew_binary module

MOAL.computer_organization.skew_binary.skew_to_dec(dec)[source]