Unit 1&2
Unit 1&2
The main memory can be seen as an array of adjacent cells each capable of storing a binary digit
that is bit having value of 1 or 0. These cells are organized in the form of groups of fixed number,
say n, of cells that can be dealt with as an atomic entity. An entity consisting of 8 bits is called a
byte. In many systems, the entity consisting of n bits that can be stored and retrieved in and out of
the memory using one basic memory operation is called a word (the smallest addressable entity).
Typical size of a word ranges from 16 to 64 bits. It is, however, customary to express the size of
the memory in terms of bytes. For example, the size of a typical memory of a personal computer
is 256 Mbytes, that is, 256 * 220 = 228 bytes. In order to be able to move a word in and out of the
memory, a distinct address has to be assigned to each word. This address will be used to determine
the location in the memory in which a given word is to be stored. This is called a memory write
operation. Similarly, the address will be used to determine the memory location from which a word
is to be retrieved from the memory. This is called a memory read operation. The number of bits, l,
needed to distinctly address M words in a memory is given by l = log2 M. For example, if the size
of the memory is 64 M (read as 64 megawords), then the number of bits in the address is given by
log2 (64 *220) = log2 (226) = 26 bits. Alternatively, if the number of bits in the address is l, then
the maximum memory size (in terms of the number of words that can be addressed using these l
bits) is M = 2l.
There are two basic memory operations. These are the memory write and memory read operations.
During a memory write operation, a word is stored into a memory location whose address is
specified. During a memory read operation a word is read from a memory location whose address
is specified. Typically, memory read and memory write operations are performed by the central
processing unit (CPU).
Three basic steps are needed in order for the CPU to perform a write operation into a specified
memory location:
1. The word to be stored into the memory location is first loaded by the CPU into a specified
register, called the memory data register (MDR).
2. The address of the location into which the word is to be stored is loaded by the CPU into a
specified register, called the memory address register (MAR).
3. A signal, called write, is issued by the CPU indicating that the word stored in the MDR is
to be stored in the memory location whose address in loaded in the MAR.
Example illustrates the operation of writing the word given by 7E (in hex) into the memory
location whose address is 2005. The first part of the figure shows the status of the registers and
memory locations involved in the write operation before the execution of the operation. Part b
of the figure shows the status after the execution of the operation. It is worth mentioning that
the MDR and the MAR are registers used exclusively by the CPU and are not accessible to the
programmer.
Similar to the write operation, three basic steps are needed in order to perform a memory read
operation:
1. The address of the location from which the word is to be read is loaded into the MAR.
2. A signal, called read, is issued by the CPU indicating that the word whose address is in
the MAR is to be read into the MDR.
3. After some time, corresponding to the memory delay in reading the specified word, the
required word will be loaded by the memory into the MDR ready for use by the CPU.
I. ADDRESSING MODES
1. Three-address
A three-address instruction takes the form operation add-1, add-2, add-3. In this form, each of
add-1, add-2, and add-3 refers to a register or to a memory location. Consider, for example,
the instruction ADD R1, R2, R3. This instruction indicates that the operation to be performed
is addition. It also indicates that the values to be added are those stored in registers R1 and R2
that the results should be stored in register R3. An example of a three-address instruction that
refers to memory locations may take the form ADD A, B, C. The instruction adds the contents
of memory location A to the contents of memory location B and stores the result in memory
location C.
2. Two-address
A two-address instruction takes the form operation add-1, add-2. In this form, each of add-1
and add-2 refers to a register or to a memory location. Consider, for example, the instruction
ADD R1, R2. This instruction adds the contents of register R1 to the contents of register R2
and stores the results in register R2. The original contents of register R2 are lost due to this
operation while the original contents of register R1 remain intact. This instruction is equivalent
to a three-address instruction of the form ADD R1, R2, R2. A similar instruction that uses
memory locations instead of registers can take the form ADD A, B. In this case, the contents
of memory location A are added to the contents of memory location B and the result is used to
override the original contents of memory location B.
3. One-address
A one-address instruction takes the form ADD R1. In this case the instruction implicitly refers
to a register, called the Accumulator Racc, such that the contents of the accumulator is added to
the contents of the register R1 and the results are stored back into the accumulator Racc. If a
memory location is used instead of a register, then an instruction of the form ADD B is used.
In this case, the instruction adds the content of the accumulator Racc, to the content of memory
location B and stores the result back into the accumulator Racc. The instruction ADD R1 is
equivalent to the three-address instruction ADD R1, Racc, Racc, or to the two-address instruction
ADD R1, Racc.
4. One-and-half address
Consider, for example, the instruction ADD B, R1. In this case, the instruction adds the
contents of register R1 to the contents of memory location B and stores the result in register
R1. Owing to the fact that the instruction uses two types of addressing, that is, a register and a
memory location, it is called a one-and-half-address instruction. This is because register
addressing needs a smaller number of bits than those needed by memory addressing.
5. Zero-address
These are the instructions that use stack operation. A stack is a data organization mechanism
in which the last data item stored is the first data item retrieved. Two specific operations can
be performed on a stack. These are the push and the pop operations. Figure illustrates these
two operations. As can be seen, a specific register, called the stack pointer (SP), is used to
indicate the stack location that can be addressed. In the stack push operation, the SP value is
used to indicate the location (called the top of the stack) in which the value (5A) is to be stored
(in this case it is location 1023). After storing (pushing) this value the SP is incremented to
indicate to location 1024. In the stack pop operation, the SP is first decremented to become
1021. The value stored at this location (DD in this case) is retrieved (popped out) and stored
in the shown register.
Instruction Classification
II. ADDRESSING MODES
The different ways in which operands can be addressed are called the addressing modes.
Addressing modes differ in the way the address information of operands is specified. The
simplest addressing mode is to include the operand itself in the instruction, that is, no address
information is needed. This is called immediate addressing. A more involved addressing mode
is to compute the address of the operand by adding a constant value to the content of a register.
This is called indexed addressing. Between these two addressing modes there exist a number
of other addressing modes including absolute addressing, direct addressing, and indirect
addressing
1. Immediate Mode
According to this addressing mode, the value of the operand is (immediately) available in the
instruction itself. Consider, for example, the case of loading the decimal value 1000 into a
register Ri. This operation can be performed using an instruction such as the following: LOAD
#1000, Ri. In this instruction, the operation to be performed is to load a value into a register.
The source operand is (immediately) given as 1000, and the destination is the register Ri. It
should be noted that in order to indicate that the value 1000 mentioned in the instruction is the
operand itself and not its address (immediate mode), it is customary to prefix the operand by
the special character (#).
The address of the memory location that holds the operand is included in the instruction.
Consider, for example, the case of loading the value of the operand stored in memory location
1000 into register Ri. This operation can be performed using an instruction such as LOAD
1000, Ri. In this instruction, the source operand is the value stored in the memory location
whose address is 1000, and the destination is the register Ri. Note that the value 1000 is not
prefixed with any special characters, indicating that it is the (direct or absolute) address of the
source operand. Figure shows an illustration of the direct addressing mode. For example, if the
content of the memory location whose address is 1000 was (2345) at the time when the
instruction LOAD 1000, Ri is executed, then the result of executing such instruction is to load
the value (2345) into register Ri. Direct (absolute) addressing mode provides more flexibility
compared to the immediate mode.
3. Indirect Mode
In the indirect mode, what is included in the instruction is not the address of the operand, but
rather a name of a register or a memory location that holds the (effective) address of the
operand. In order to indicate the use of indirection in the instruction, it is customary to include
the name of the register or the memory location in parentheses. Consider, for example, the
instruction LOAD (1000), Ri. This instruction has the memory location 1000 enclosed in
parentheses, thus indicating indirection. The meaning of this instruction is to load register Ri
with the contents of the memory location whose address is stored at memory address 1000.
Because indirection can be made through either a register or a memory location, therefore, we
can identify two types of indirect addressing. These are register indirect addressing, if a
register is used to hold the address of the operand, and memory indirect addressing, if a
memory location is used to hold the address of the operand. The two types are illustrated in
Figure
4. Indexed Mode
The address of the operand is obtained by adding a constant to the content of a register, called
the index register. Consider, for example, the instruction LOAD X(Rind), Ri. This instruction
loads register Ri with the contents of the memory location whose address is the sum of the
contents of register Rind and the value X. Index addressing is indicated in the instruction by
including the name of the index register in parentheses and using the symbol X to indicate the
constant to be added. Figure 2.8 illustrates indexed addressing. As can be seen, indexing
requires an additional level of complexity over register indirect addressing.
5. Relative mode
Recall that in indexed addressing, an index register, Rind, is used. Relative addressing is the
same as indexed addressing except that the program counter (PC) replaces the index register.
For example, the instruction LOAD X(PC), Ri loads register Ri with the contents of the
memory location whose address is the sum of the contents of the program counter (PC) and
the value X. Figure 2.9 illustrates the relative addressing mode.
The type of instructions forming the instruction set of a machine is an indication of the power
of the underlying architecture of the machine. Instructions can in general be classified as
following
1. Data Movement
Data movement instructions are used to move data among the different units of the machine.
Most notably among these are instructions that are used to move data among the different
registers in the CPU. A simple register to register movement of data can be made through the
instruction.
This instruction moves the content of register Ri to register Rj. The effect of the instruction is
to override the contents of the (destination) register Rj without changing the contents of the
(source) register Ri. Data movement instructions include those used to move data to (from)
registers from (to) memory. These instructions are usually referred to as the load and store
instructions, respectively. Examples of the two instructions are LOAD 25838, Rj STORE Ri,
1024 The first instruction loads the content of the memory location whose address is 25838
into the destination register Rj. The content of the memory location is unchanged by executing
the LOAD instruction. The STORE instruction stores the content of the source register Ri into
the memory location 1024. The content of the source register is unchanged by executing the
STORE instruction. Table shows some common data transfer operations and their meanings.
2. Arithmetic and logical operations
Arithmetic and logical instructions are those used to perform arithmetic and logical
manipulation of registers and memory contents. Examples of arithmetic instructions include
the ADD and SUBTRACT instructions. These are ADD R1,R2,R0 SUBTRACT R1,R2,R0
The first instruction adds the contents of source registers R1 and R2 and stores the result in
destination register R0. The second instruction subtracts the contents of the source registers R1
and R2 and stores the result in the destination register R0. The contents of the source registers
are unchanged by the ADD and the SUBTRACT instructions. In addition to the ADD and
SUBTRACT instructions, some machines have MULTIPLY and DIVIDE instructions. These
two instructions are expensive to implement and could be substituted by the use of repeated
addition or repeated subtraction. Therefore, most modern architectures do not have
MULTIPLY or DIVIDE instructions on their instruction set. Table 1 shows some common
arithmetic operations and their meanings. Logical instructions are used to perform logical
operations such as AND, OR, SHIFT, COMPARE, and ROTATE. As the names indicate, these
instructions perform, respectively, and, or, shift, compare, and rotate operations on register or
memory contents. Table 2 presents a number of logical operations.
3. Sequencing instructions
Control (sequencing) instructions are used to change the sequence in which instructions are
executed. They take the form of CONDITIONAL BRANCHING (CONDITIONAL JUMP),
UNCONDITIONAL BRANCHING (JUMP), or CALL instructions. A common characteristic
among these instructions is that their execution changes the program counter (PC) value. The
change made in the PC value can be unconditional, for example, in the unconditional branching
or the jump instructions. In this case, the earlier value of the PC is lost and execution of the
program starts at a new value specified by the instruction. Consider, for example, the
instruction JUMP NEW-ADDRESS. Execution of this instruction will cause the PC to be
loaded with the memory location represented by NEW-ADDRESS whereby the instruction
stored at this new address is executed. On the other hand, the change made in the PC by the
branching instruction can be conditional based on the value of a specific flag. Examples of
these flags include the Negative (N), Zero (Z), Overflow (V), and Carry (C). These flags
represent the individual bits of a specific register, called the CONDITION CODE (CC)
REGISTER. The values of flags are set based on the results of executing different instructions.
The meaning of each of these flags is shown in Table
For example,
LOAD #100, R1
The fourth instruction is a conditional branch instruction, which indicates that if the result of
decrementing the contents of register R1 is greater than zero, that is, if the Z flag is not set, then
the next instruction to be executed is that labeled by Loop. It should be noted that conditional
branch instructions could be used to execute program loops (as shown above). The CALL
instructions are used to cause execution of the program to transfer to a subroutine. A CALL
instruction has the same effect as that of the JUMP in terms of loading the PC with a new value
from which the next instruction is to be executed. However, with the CALL instruction the
incremented value of the PC (to point to the next instruction in sequence) is pushed onto the stack.
Execution of a RETURN instruction in the subroutine will load the PC with the popped value from
the stack. This has the effect of resuming program execution from the point where branching to
the subroutine has occurred.
4. Input/output Instructions
Input and output instructions (I/O instructions) are used to transfer data between the computer and
peripheral devices. The two basic I/O instructions used are the INPUT and OUTPUT instructions.
The INPUT instruction is used to transfer data from an input device to the processor. Examples of
input devices include a keyboard or a mouse. Input devices are interfaced with a computer through
dedicated input ports. Computers can use dedicated addresses to address these ports. Suppose that
the input port through which a keyboard is connected to a computer carries the unique address
1000. Therefore, execution of the instruction INPUT 1000 will cause the data stored in a specific
register in the interface between the keyboard and the computer, call it the input data register, to
be moved into a specific register (called the accumulator) in the computer. Similarly, the execution
of the instruction OUTPUT 2000 causes the data stored in the accumulator to be moved to the data
output register in the output device whose address is 2000. Alternatively, the computer can address
these ports in the usual way of addressing memory locations. In this case, the computer can input
data from an input device by executing an instruction such as MOVE Rin, R0. This instruction
moves the content of the register Rin into the register R0. Similarly, the instruction MOVE R0,
Rin moves the contents of register R0 into the register Rin, that is, performs an output operation.
The latter is called memory-mapped Input/Output.
II. INTRODUCTION TO ASSEMBLY LANGUAGE PROGRAMMING
In assembly language, programs are written using mnemonic instructions that represent basic
operations such as data movement, arithmetic, and logical operations. These mnemonic
instructions are then translated directly into machine code by an assembler.
One of the key advantages of assembly language programming is its efficiency and precise control
over system resources. It allows programmers to optimize code for performance and size, making
it ideal for tasks where speed and resource usage are critical, such as operating systems, device
drivers, and embedded systems.
However, assembly language programming also comes with challenges. It requires a deep
understanding of computer architecture and hardware, as well as meticulous attention to detail due
to its low-level nature. Additionally, programs written in assembly language are typically less
portable and harder to maintain compared to those written in higher-level languages.
1. Data Movement: Assembly language provides instructions to move data between memory
locations, registers, and other data storage areas. Understanding how to efficiently move
data is crucial for manipulating information within a program.
3. Control Flow Structures: Assembly language allows programmers to control the flow of
execution using conditional and unconditional branching instructions. Conditional
branching allows the program to make decisions based on certain conditions, while
unconditional branching enables jumping to different parts of the code.
4. Subroutines and Functions: Subroutines, also known as functions or procedures, are
reusable blocks of code that perform specific tasks. They allow for modular programming,
where complex tasks can be broken down into smaller, more manageable components.
Subroutine calls and returns are implemented using specialized instructions.
5. Stack Operations: The stack is a vital data structure used for managing function calls,
local variables, and passing parameters between subroutines. Assembly language provides
instructions for pushing data onto the stack, popping data off the stack, and adjusting the
stack pointer.
7. Error Handling and Exception Handling: Handling errors and exceptions is essential for
writing robust and reliable programs. Assembly language provides mechanisms for
detecting and responding to various types of errors and exceptions, such as divide-by-zero
errors, invalid memory access, and hardware interrupts.
9. Debugging and Testing: Debugging assembly language programs can be challenging due
to the low-level nature of the language. Techniques such as single-stepping, breakpoints,
and tracing can help identify and fix errors in the code. Testing is also essential for ensuring
that the program behaves correctly under various conditions.
Assembly language for simple processor
Program Counter (PC), Instruction Register (IR), Address Register (AR), Accumulator (AC), and
Data Register (DR). The PC contains the address of the next instruction to be executed. The IR
contains the operation code portion of the instruction being executed. The AR contains the address
portion (if any) of the instruction being executed. The AC serves as the implicit source and
destination of data. The DR is used to hold data.