Code Optimization

Interesting things about software development and code optimization

Hardware and Bits


As I said we would go from basic to complex things step by step and this post will clarify some important things that you must know about hardware and bits.

Each PC consists of different devices like monitor, HDD, keyboard, mouse, etc., and each device has its own controller (a chip) that controls device. For example, mouse has a small chip that controls laser and gets and sends commands and data to and from PC.

Almost each device has its own IRQ - this is interrupt number that assigned to a device by system to be able to get and send commands. So when you move your mouse it triggers a IRQ and provides data like DX and DY and system knows what direction should it move a cursor to and how many points the cursor should be moved.

This is general explanation and to be able to understand more you have to find a book and read it.

So devices talk to each other or to system via Bytes. They send a lot of bytes and each byte consists of 8 bits, like:


0 0 0 0   0 0 0 0

0 1 1 0   1 1 1 1


To understand what these bits represent in a more human readable way you have to know how to convert binary numeral system into hex-decimal or decimal numeral system. You can find it on internet or a book.

So, when I started to learn programming my first language was Assembler for Z-80 CPU. There was a book that had description for all 255 op-codes (commands of CPU) and some additional information, but it was not enough for me to understand anything and more over to create at least such simple program like the popular "Hello World!".

I did learn bits, bytes, bit operations like shift to the left/right, conditional operations like jump here or there, CPU registers, etc. And that was much but not enough to make the "Hello World!".

Then I did figure out memory structure (my brother did help me in this) and at some point there was like a flash in my mind - "Or God! That is how it all works!".

Main thing is to understand memory structure and how controllers works. In my case I had video memory mapped from 16384 address to 32767, and one part of this memory did represent pixels, and another part - colors.

Filling just pixel memory with correct bits will lead to this:

that is represented by bits:

1 1 0 0 1 1 0 0    = 0xCC
1 1 0 0 1 1 0 0    = 0xCC
1 1 0 0 1 1 0 0    = 0xCC
1 1 1 1 1 1 0 1    = 0xFD
1 1 1 1 1 1 1 1    = 0xFF
1 1 0 0 1 1 1 0    = 0xCE
1 1 0 0 1 1 0 1    = 0xCD
1 1 0 0 1 1 0 0    = 0xCC

So, as you can see you have to set bits in bytes in corresponding places and bytes into correct memory address.

Then you can color it as you need by setting color-bytes into right place in memory.

Nova days graphic memory structure is more simple and represented by 3 or 4 bytes RGB or RGBA. This leads to that, that modern PCs have to have more memory and resources and speed to provide such simplicity. 

Next step we will look into bits and bit shifting as well as bit operations - that is very important in software development.

Thank you.