|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ASSEMBLY PROGRAMMING AND OS DESIGN
<HOME>
|
CH2.2 Registers
There are 8 general purpose registers, each of which are special in their own ways.
The registers will be introduced to you along with some actual assembly
language. Don't worry if you don't understand completely. All
will be made clear as you move on through the book. Let's begin by examining the most useful register, the accumulator register. AX = Accumulator Register The accumulator register is used for most general mathematics, and is also used as a transfer register to some of the more specialized registers. The following example uses the MOV and ADD commands to add 2 + 2. Ex.1 org 100h mov AX,2 add AX,2 As you can see from the example, the format of the MOV command is very simple. mov Destination,Source Destination can be either a register or a memory location. Source can be a register, memory location, or a constant value. There are a few other formats, but we'll cover those as we go. It is important to note that you cannot move straight from a memory location to another memory location. Now before we continue, I suppose we should explain what a memory location is. Imagine that your computer's memory is like a street that has millions of mailboxes. Each of those boxes has a numerical address and may or may not contain anything useful. In example 1, we used the command org 100h. This is actually not a command, but a direction to the assembler to begin placing the program at the address of 100h. So if we opened the box at address 100h, we would find the machine language code for the MOV instruction. The second instruction is the add instruction. The format of this instruction is: add Operand,Operand The Operands can either be registers, memory addresses, or a constant value. The result of the add operation is always stored in the AX register. Ex.2 org 100h mov AX,200h mov BX,[200h] add AX,BX In the above example we load two numbers into two registers and then add them together. This is what it would look like in algebra terms. a = 200h b = The value at address 200h (now you know what the brackets mean) a = a + b Example 2 introduces us to the BX register which is sometimes called the base register. The primary function of the register is to serve as a pointer to data in the data segment. But the register is most often used to hold a second value when we need to keep the value that is already in the AX register. We are also introduced to the indirect addressing mode. What this means is that we don't take the number inside the brackets, we take the number stored at the address that number points to. So, if the value stored at address 200h was 5, then the result of this code would be a 5 in the BX register, and 205h in the AX register. Another extremely useful register is CX, the counter register. We can use this register to hold general data as well, but there are actually some assembly commands that will increment or decrement CX automatically as part of their function. To illustrate it's usefulness, the following code will introduce two new commands. Ex.3 The first new instruction you see is DEC. DEC simply decrements whatever number is stored in the register you specify by one. The next instruction is JNZ. This is just one of the many conditional jump instructions. JNZ checks to see if the result of the last mathematical operation was zero. If it was Not Zero, then it will jump to the point specified by it's operand. In this case we jumped to the label DOUBLE where AX is added to itself. The following pseudocode will illustrate what happened in example 3. AX = 1 All the pseudocode in this book is basically ANSI C code. If you don't know C, don't worry about it. Most things will be explained. For this code, the one new thing you may not have seen is the '!=', this simply reads as 'is not equal to' and produces either a true or false result. In all languages false is represented by zero and true is any non zero result. So far we have covered 3 16 BIT registers. Since this book is here to help you write a 32 BIT OS, we are no going to learn the flexibility of the x86 in dealing with data. Each of the X registers, AX, BX, CX, and the final one we are now covering, DX, all can be looked at as either BYTE sized, WORD sized, or DWORD sized. There are actually two BYTE sized sections of these registers, a high BYTE and a low BYTE. The high BYTE, or H register represents BITs 8 through 15. The low BYTE, or L register represents BITs 0 through 7. It turns out that all of the general purpose registers on the x86 are 32 Bits in size. The X versions of these registers occupies BITs 0 through 15 of the full register. The full 32 BIT register is referred to as an extended register, or E register. The table below should clear up any confusion this paragraph just caused. General Purpose Registers
For now you will only use the 16 BIT versions of the registers, but beginning in CH4, everything will move to 32 BITs. The next few registers are used to manipulate data structures and only
come in 16 and 32 BIT forms. The first of these registers is known
as the Base Pointer (BP) register. It is commonly used to point to
the base of large tables of data, but can be used as a common 16 BIT
register. The Source Index (SI) and Destination Index (DI) registers
are often used in string operations and to move large quantities of
data. Finally, the Stack Pointer (SP) register is used to point to
the head of the current operating stack. Don't worry if you don't
know what a stack is...it will be explained before you use it. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| www.cynergysoft.com |
Email any questions to the author, Nathan Daniels. |
Copyright © 2001 |