Thursday, February 12, 2015

CPU and Registers

How does a CPU use the registers?

This is the first part in, I hope, a long series of short articles, which will explain some of the basics about the internal design of CPUs.

In this first part we’ll look at the registers, or more specifically, how the registers are used. Registers?

A CPU usually contains a register file. A register file consists of a number of registers plus status bits, which give information about the registers; for instance whether an overflow occurred. Registers are needed because a CPU cannot directly work with data that is stored in the memory. So if the CPU wants to work with data, it needs to copy this data to the registers and afterwards copy them back. When a CPU executes an instruction this usually takes 5 steps:

Instruction Fetch: the instruction(s) that need to be executed are grabbed from the memory, or more often (instruction) cache. Instruction Decode: the instruction is translated to the commands that are specific for the CPU.

Instruction Execute: the instruction is executed. Memory Access: if needed (only for load, store and brand instructions) data is written or read from the memory or cache.

Write Back: if needed (only for instructions where a result needs to be stored in a register) the result of the execution step is stored into a register.

The question is now, how do the registers fit in here. In general there are four ways to use the registers. I'll illustrate this with a simple calculation:

C = A + B

Now this calculation can be done via accumulator, stack, register-memory or register-register type of register-usage. Accumulator The calculation would require three steps. Load A Add B Store C Only one register is used, the Accumulator. Into this register, first, memory data A is placed. Then memory data B is added to the contents of the register. The register then contains the result of A+B.

Finally, the result is placed in memory C. This is the simplest way ofusing a register, but of course also a very limited one, since complex instructions that require more different values are much more difficult to code. Also a problem is that in each step the memory is accessed.

Memory is much slower than register-space and therefore the CPU clock is limited by the memory-speed. Therefore this way of register usage is often only used in microcontrollers. Stack A more efficient way of using registers is stack-based: Push A Push B Add Pop C The easiest way to explain this is to draw a picture.

After the first instruction - push A - the value of A is placed onto the stack, here with three registers. After the second instruction - push B - the first value is pushed down into the stack and the value B is placed on top. The third instruction adds the values of the two top-most registers in the stack, pops the topmost value off the stack which causes the value A to shift back to the topmost position. Then the result is placed on this topmost place and therefore overwrites value B with the result A+B.

The fourth and last instruction simply pops this A+B value off the stack and puts the result in memory place C. As one can see this is an improvement compared to the accumulator. Now more than one register is available for calculation. Therefore less memory-access is required. This way of register usage is for instance used in x87-based CPUs, better known as the FPU of PC processors such as the Intel Pentium III and AMD Athlon. But also modern Java processors such as the pico-Java I use stack-based registers. Of course the downside is obvious.

It is only possible to use the topmost registers. This limits the freedom of register-usage. This is also one of the reasons why even the powerful FPU units of the Pentium III and AMD Athlon are often no match for most RISC CPUs like the Alpha 21164 or MIPS R12000. Register-Memory A more efficient way of using registers is the register-memory way: Load reg1, A Add reg1, B Store C, reg1 In this simple example, only one register is used but it is not difficult to see that more freedom exists than in the stack-based case. Each register in the register-file can be accessed directly, and not just the top-most. This technique is often used in CISC based CPUs.

The reason lies in the past. The ‘glory-days’ of CISC lay in about 1960 to 1975. The reason for this is that memory was extremely expensive in this period. Therefore systems were equipped with relatively slow memory. To understand the importance of this we must look back at the 5 steps required for executing an instruction. Step 1, the instruction fetch, is determent by the speed of the memory (including cache memory).

All following steps (besides maybe storage back to memory) are CPU-speed dominated. CPU's in that time were partially pipelined, but to make things easier to understand we will assume they were fully pipelined. We would get: Figure 2, Fetch is the dominating step. Here we see a few cycles. As the picture show the decode-step takes less time than the fetch. However because during the decode step already a new fetch of another instruction takes place - remember the chip is assumed to be pipelined - the time saved cannot be used. So with those chips getting maximum performance was simply an issue of doing as much as possible in as little as possible instructions.

No matter how complex instructions are, they will still require less time than a fetch. This explains why CISC has many complex instructions and little instructions for a task are needed. Of course the downside is that increasing MHz is difficult with such a complex chip. However this was no issue since the (cache) memory wouldn't be able to catch up anyway. This however changed around 1975 when cheap and fast memory arrived. Register-Register When fast memory chips were available things changed. Now it didn't matter anymore how many cycles were needed for a task. If you pipeline a chip the effective throughput will be 1 instruction per cycle anyway. So now it was just a case of getting as much MHz out of the chip as possible. As a result of this register usage changed. Load reg1, A Load reg2, B Add reg1, reg2 Store C, reg1 More instructions are needed than in the previous case, but fewer actions need to be performed. Therefore it is easier to increase the MHz of a chip, which results in higher performance.

This is a policy very common in RISC designs. One downside is however that more registers are needed. Also the chip design is often kept simple in order to prevent nasty instructions, that are difficult to be optimised, from becoming bottlenecks. Therefore RISC chips often have, besides many registers, little instructions and deep pipelines. Final Words These are the four basic ways to use registers. Of course real world CPU's will not always fit in one category and make use of combinations. For instance the AMD Athlon is a CISC chip on the outside but RISC-like on the inside. Its FPU-design is still stack-based however. Still, I hope this article gave you some basic knowledge of how registers are being used in a chip

Wednesday, February 11, 2015

What is data encryption?

What is encryption?

Encryption is a technique for transforming information on a computer in such a way that it becomes unreadable. So, even if someone is able to gain access to a computer with personal data on it, they likely won’t be able to do anything with the data unless they have complicated, expensive software or the original data key. The basic function of encryption is essentially to translate normal text into ciphertext. Encryption can help ensure that data doesn’t get read by the wrong people, but can also ensure that data isn’t altered in transit, and verify the identity of the sender.

3 different encryption methods

There are three different basic encryption methods, each with their own advantages (list courtesy of Wisegeek):

Hashing
Hashing creates a unique, fixed-length signature for a message or data set. Each “hash” is unique to a specific message, so minor changes to that message would be easy to track. Once data is encrypted using hashing, it cannot be reversed or deciphered. Hashing, then, though not technically an encryption method as such, is still useful for proving data hasn’t been tampered with.

Symmetric methods
Symmetric encryption is also known as private-key cryptography, and is called so because the key used to encrypt and decrypt the message must remain secure, because anyone with access to it can decrypt the data. Using this method, a sender encrypts the data with one key, sends the data (the ciphertext) and then the receiver uses the key to decrypt the data.

Asymmetric methods
Asymmetric encryption, or public-key cryptography, is different than the previous method because it uses two keys for encryption or decryption (it has the potential to be more secure as such). With this method, a public key is freely available to everyone and is used to encrypt messages, and a different, private key is used by the recipient to decrypt messages.

Uses of hexadecimal numbers

Hexadecimal refers to the base-16 number system, which consists of 16 unique symbols, in contrast to the ten unique symbols of the commonly used decimal (i.e., base 10) numbering system. The numbers 0 through 9 are the same in both systems; however, the decimal numbers 10 through 15 are represented by the letters A through F. Thus, for example, the decimal number 11 is represented by B in the hexadecimal system and decimal 14 is represented by E.

The hexadecimal system is commonly used by programmers to describe locations in memory because it can represent every byte (i.e., eight bits) as two consecutive hexadecimal digits instead of the eight digits that would be required by binary (i.e., base 2) numbers and the three digits that would be required with decimal numbers. Some of the ports addresses are also in hexa-decimal.

In addition, it is much easier for humans to read hexadecimal numbers than binary numbers, and it is not much more difficult for computer professionals to read hexadecimal numbers than decimal numbers. 

Moreover, conversion between hexadecimal and binary numbers is also easy after a little practice. For example, to convert a byte value from hexadecimal to binary, all that is necessary is to translate each individual hexadecimal digit into its four-bit binary equivalent. 

Hexadecimal numbers are indicated by the addition of either an 0x prefix or an h suffix. For example, the hexadecimal number 0x2F5B translates to the binary number 0010 1111 0101 1011. int 0x80 is the assembly language instruction that is used to invoke system calls in Linux on x86 (i.e., Intel-compatible) processors. The 0x in it indicates that it is not a decimal 80 but rather a hexadecimal 80 (which is a decimal 128). A system call is a request in a Unix-like operating system made by a process for a service performed by the kernel.

A common use of hexadecimal numbers is to describe colors on web pages. Each of the three primary colors (i.e., red, green and blue) is represented by two hexadecimal digits to create 255 possible values, thus resulting in more than 16 million possible colors. For example, the HTML (hypertext markup language) code telling a browser to render the background color of a web page as red is and that telling it to render the page as white is .

Hexadecimal numbers

Hexadecimal

In mathematics and computinghexadecimal(also base 16, or hex) is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 09 to represent values zero to nine, and A, B, C, D, E, F (or alternatively af) to represent values ten to fifteen. Hexadecimal numerals are widely used by computer systems designers and programmers. Several different notations are used to represent hexadecimal constants in computing languages; the prefix "0x" is widespread due to its use in Unix and C (and related operating systems and languages). Alternatively, some authors denote hexadecimal values using a suffix or subscript. For example, one could write 0x2AF3 or 2AF316, depending on the choice of notation.

As an example, the hexadecimal number 2AF316 can be converted to an equivalent decimal representation. Observe that 2AF316is equal to a sum of (200016 + A0016 + F016 + 316), by decomposing the numeral into a series of place value terms. Converting each term to decimal, one can further write: (216 × 163) + (A16 × 162) + (F16 × 161) + (316 × 160),
(2 × 4096) + (10 × 256) + (15 × 16) + (3 × 1), or 10995.

Each hexadecimal digit represents four binary digits (bits), and the primary use of hexadecimal notation is a human-friendly representation of binary-coded values in computing and digital electronics. One hexadecimal digit represents a nibble, which is half of an octet or byte (8 bits). For example, byte values can range from 0 to 255 (decimal), but may be more conveniently represented as two hexadecimal digits in the range 00 to FF. Hexadecimal is also commonly used to represent computer memory addresses.

Computer Systems (FAST TRACK)

1     Data representation   1.1    Number systems   How and why computers use binary to represent all forms of data? •     Any form ...