mschwartz / assembly-tutorial

Programming in assembly language tutorial
553 stars 44 forks source link

Non Negative Integer Representation, why? #15

Open Flavor-name opened 2 months ago

Flavor-name commented 2 months ago

Why do computer systems favor Non Negative Integers over Positive Integers? Zero has no value, therefore anything of value starts at 1. A group of mail boxes, 26 in total, A through Z, or 1 through 26. Whether the box is empty or contains information, it is a positive integer, never zero. To say, "The First box is zero." is confusing, where first = 1 already. Why did the pioneers of computing choose Non Negative Intetegers instead of the other possibilities?

mschwartz commented 2 months ago

It’s because of the way addressing works. If you have a register with the address of an array of numbers, say, the first number is at address +0. It’s the math.

If you add 1 to the address, you have skipped over the first element of the array.

make sense?

Flavor-name commented 1 week ago

Which math?

In the book, available at archive.org, Digital Computer Electronics by Albert Paul Malvino, page 5, figure 1-5, identifies a 5 bit register:

+---+---+---+---+---+
| 1 | 1 | 0 | 0 | 1 |
+---+---+---+---+---+

2^4 2^3 2^2 2^1 2^0 ---> The "zero-eth" register, is mathematical notation to verify value of the contents for that bit.

2 2 2 2 2 = 2^5 (5 bits) <--- This math tells you the number of possible values.

The contents of bit 1 can be calculated using 2^0. The contents of bit 2 can be calculated using 2^1. ...and so on. Bit 1, as well as all bits, contains either No value (zero), a Low value (.5v) or a High value (5v). Bit 1 is the only non-carry bit. Bit's 2+ can contain carries. 1's and zero's are symbols we apply to represent voltage values. The only true zero state is empty bits which contains neither a Low voltage (0) or a High voltage (1). DNA is a Quaternary structure that has no zero, C-TAG, the Nucleic Acids are something not zero.

What confuses me is Mail box 1 is the zero-eth location. Where is the math applied to what in order to start counting from 0?

mschwartz commented 1 week ago

I answered already.

it is due to the way address math works.

if you are familiar with arrays…. The first element of the array is at array_address + 0. The second is at + 1, and so on.

if you want to start with 1, you can, but you would waste memory - the first element of the array would be ignored.

as for the electricity involved, there is tri state logic where you have bits that are on, off, or neither.

see https://en.m.wikipedia.org/wiki/Three-state_logic

Flavor-name commented 1 week ago

Searching for 'Address Arithmetic' I found this:

6.7.6 Address arithmetic

Address arithmetic is the foundation on which you can build data structures like arrays, records (see Structures) and objects (see Object-oriented Forth).

Standard Forth does not specify the sizes of the data types. Instead, it offers a number of words for computing sizes and doing address arithmetic. Address arithmetic is performed in terms of address units (aus); on most systems the address unit is one byte. Note that a character may have more than one au, so chars is no noop (on platforms where it is a noop, it compiles to nothing).

The basic address arithmetic words are + and -. E.g., if you have the address of a cell, perform 1 cells +, and you will have the address of the next cell.

Standard Forth also defines words for aligning addresses for specific types. Many computers require that accesses to specific data types must only occur at specific addresses; e.g., that cells may only be accessed at addresses divisible by 4. Even if a machine allows unaligned accesses, it can usually perform aligned accesses faster.

Ref: https://gforth.org/manual/Address-arithmetic.html Also, University of Wisconsin's Computer Science curriculum offers a PDF of CH04 discussing computers using Non-Negative Integers.

Thanks for the guidance.

mschwartz commented 1 week ago

I'm a big fan of Forth.

You're welcome!