Computer Organization and Architecture (18EC35) - Machine Instructions and Programs - Part 2 (Module 2)
Computer Organization and Architecture (18EC35) - Machine Instructions and Programs - Part 2 (Module 2)
and Architecture
Carl Hamacher, Zvonko Vranesic, Safwat Zaky,
Computer Organization, 5th Edition,
Tata McGraw Hill, 2002.
Machine Instructions
and Programs – Part 2
Module 2
Addressing Modes
Generating Memory Addresses
⚫ How to specify the address of branch target?
⚫ Can we give the memory operand address
directly in a single Add instruction in the loop?
⚫ Use a register to hold the address of NUM1;
then increment by 4 on each pass through
the loop.
Addressing Modes
⚫ The way in which the location of an operand
is specified in an instruction is called
addressing mode.
Addressing Modes..
Addressing Modes..
⚫ Immediate mode
⚫ The operand is given explicitly in the instruction.
⚫ Move #200,R0
⚫ Add #10,R1
⚫ Register mode
⚫ The operand is the contents of a processor
register; the name (address) of the register is
given in the instruction.
⚫ Move R1,R2
⚫ Add R0,R1
Addressing Modes..
⚫ Absolute mode (Direct mode)
⚫ The operand is in a memory location; the address
of this location is given explicitly in the instruction.
⚫ Move LOC,R1
⚫ Add A,R0
⚫ Indirect mode
⚫ The effective address of the operand is the
contents of a register or memory location whose
address appears in the instruction.
⚫ Move (R0),R1
⚫ Add (LOC),R2
Addressing Modes..
Addressing Modes..
Addressing Modes..
⚫ Index mode
⚫ The effective address of the operand is
generated by adding a constant value to the
contents of a register.
⚫ We indicate the Index mode symbolically as
X(Ri)
⚫ X(Ri): EA = [Ri]+X
Addressing Modes..
⚫ The value X defines an offset (also called a
displacement) from this address to the location
where the operand is found.
⚫ Move 20(R0),R1
⚫ Add 1000(R1),R2
⚫ The constant X may be given either as an
explicit number or as a symbolic name
representing a numerical value.
⚫ If X is shorter than a word, sign-extension is
needed.
Addressing Modes..
Addressing Modes..
⚫ Consider a simple example involving a list of
test scores for students taking a given
course.
⚫ A four-word memory block comprises a
record that stores the relevant information for
each student.
⚫ Each record consists of the student's
identification number (ID), followed by the
scores the student earned on three tests.
Addressing Modes..
Addressing Modes..
⚫ Suppose that we wish to compute the sum of
all scores obtained on each of the tests and
store these three sums in memory locations
SUMI, SUM2, and SUM3.
Addressing Modes..
Addressing Modes..
⚫ Base with Index mode
⚫ The effective address is the sum of the contents
of registers specified in the instruction.
⚫ (Ri,Rj): EA = [Ri]+[Rj]
⚫ Move (R0,R1),R2
⚫ Add (R1,R2),R0
Addressing Modes..
⚫ Base with Index and Offset mode
⚫ The effective address is the sum of the constant X
and the contents of registers specified in the
instruction.
⚫ X(Ri,Rj): EA = [Ri]+[Rj]+X
⚫ Move 20(R0,R1),R2
⚫ Add 1000(R1,R2),R0
Addressing Modes..
⚫ Relative mode
⚫ The effective address is determined by the Index
mode using the program counter in place of the
general-purpose register Ri.
⚫ Branch>0 LOOP
⚫ X(PC) : EA = [PC] + X
⚫ – note that X is a signed number
⚫ This location is computed by specifying it as an offset from
the current value of PC.
⚫ Branch target may be either before or after the branch
instruction, the offset is given as a singed num.
Addressing Modes..
⚫ Autoincrement mode
⚫ The effective address of the operand is the
contents of a register specified in the instruction.
⚫ After accessing the operand, the contents of this
register are automatically incremented to point to
the next item in a list.
⚫ Add (R2)+,R0
⚫ The increment is 1 for byte-sized operands, 2
for 16-bit operands, and 4 for 32-bit operands.
Addressing Modes..
Go to Figure 2.25
Addressing Modes..
⚫ Autodecrement mode
⚫ The contents of a register specified in the
instruction are first automatically decremented
and are then used as the effective address of the
operand.
⚫ Add -(R2),R0
⚫ The decrement is 1 for byte-sized operands, 2 for 16-
bit operands, and 4 for 32-bit operands.
Assembly Language
Assembly Language
⚫ Machine instructions are represented by
patterns of 0s and 1s.
⚫ Awkward to deal with when discussing or preparing
programs.
⚫ We use symbolic names
⚫ Assembly language – A complete set of symbolic
names and rules for their use
⚫ Syntax - The set of rules for using the
mnemonics in the specification of complete
instructions and programs
⚫ Mnemonics
Assembly Language..
⚫ Assembler – A program which translates
programs written in an assembly language
into a sequence of machine instructions
⚫ Source program - The user program in its
original alphanumeric text format
⚫ Object program - The assembled machine
language program
Assembly Language..
⚫ MOVE R0,SUM
⚫ The mnemonic MOVE represents the binary
pattern, or OP code (Operation Code)
⚫ R0 is Source operand and SUM is destination
operand
Assembly Language..
⚫ The assembly language must indicate which
mode is being used.
⚫ For example
⚫ A numerical value or a name used by itself, such
as SUM in the preceding instruction, may be used
to denote the Absolute mode.
⚫ The sharp sign usually denotes an immediate
operand.
⚫ ADD #5,R3
Assembly Language..
⚫ In some assembly languages, the intended
addressing mode is indicated in the OP-code
mnemonic.
⚫ In this case, a given instruction has different OP-
code mnemonics for different addressing modes.
⚫ ADDI 5,R3
⚫ Indirect addressing is usually specified by putting
parentheses around the name or symbol denoting
the pointer to the operand.
⚫ MOVE #5,(R2)
⚫ MOVEI 5,(R2)
Assembler Directives
⚫ These are commands used by the assembler
while it translates a source program into an
object program.
⚫ SUM EQU 200
Assembler Directives..
Assembler Directives..
⚫ To produce an object program, an assembler
has to know
⚫ How to interpret the names
⚫ Where to place the instructions in the memory
⚫ Where to place the data operands in the memory
Assembler Directives..
Assembler Directives..
⚫ EQU (equate) - informs the assembler that
the variable name should be replaced by the
value specified.
⚫ In this case, SUM should be replaced by the
value 200 wherever it appears in the program
⚫ ORIGIN - tells the assembler program where
in the memory to place the data block that
follows.
⚫ In this case, the location specified has the
address 204
Assembler Directives..
⚫ DATAWORD - informs the assembler that the
data value specified is to be placed in the
memory word
⚫ In this case, the data value 100 is to be placed in
the memory word at address 204.
Assembler Directives..
⚫ Any statement that results in instructions or
data being placed in a memory location may
be given a memory address label.
⚫ The label is assigned a value equal to the
address of that location.
Assembler Directives..
⚫ RESERVE - declares that a memory block of
specified bytes is to be reserved for data
⚫ In this case, a memory block of 400 bytes is to be
reserved for data, and that the name NUM1 is to
be associated with address 208.
⚫ END - tells the assembler that this is the end
of the source program text.
Assembler Directives..
⚫ Most assembly languages require statements in a source program
to be written in the form
Label Operation Operand(s) Comment
⚫ The Label is an optional name associated with the memory address
⚫ Address of machine instructions
⚫ Addresses of data items
⚫ The Operation field contains the OP-code mnemonic of the desired
instruction or assembler directive.
⚫ The Operand field contains addressing information for accessing
one or more operands, depending on the type of instruction.
⚫ The Comment field is ignored by the assembler program.
⚫ Used for documentation purposes
⚫ Makes the program easier to understand.
Assembly and Execution of
Programs
⚫ A source program written in an assembly
language must be assembled into a machine
language object program before it can be
executed.
⚫ This is done by the assembler program
⚫ Assembler replaces all symbols denoting
operations and addressing modes with the
binary codes used in machine instructions,
and replaces all names and labels with their
actual values.
Assembly and Execution of
Programs..
⚫ A key part of the assembly process is
determining the values that replace the names.
⚫ Easy in some cases
⚫ Branch offset
⚫ As the assembler scans through a source
program, it keeps track of all names and the
numerical values that correspond to them in a
symbol table.
⚫ When a name appears a second time, it is replaced
with its value from the table.
Assembly and Execution of
Programs..
⚫ Two-pass assembler
⚫ During the first pass, it creates a complete
symbol table. At the end of this pass, all
names will have been assigned numerical
values.
⚫ The assembler then goes through the source
program a second time and substitutes values
for all names from the symbol table.
Assembly and Execution of
Programs..
⚫ The assembler stores the object program
on a magnetic disk.
⚫ The object program must be loaded into
the memory of the computer before it is
executed.
⚫ A utility program called a loader is used to
load the object program into memory.
Assembly and Execution of
Programs..
⚫ The assembler can detect and report
syntax errors.
⚫ Debugger program helps the user find
other programming errors, the system
software
⚫ This program enables the user to stop
execution of the object program at some
points of interest and to examine the contents
of various processor registers and memory
locations.
Number Notation
⚫ It is often convenient to use the familiar decimal
notation.
⚫ ADD #93,R1
⚫ These values are stored in the computer as binary
numbers.
⚫ In some situations, it is more convenient to
specify the binary patterns directly.
⚫ ADD #%01011101,R1
⚫ Binary numbers can be written more compactly
as hexadecimal, or hex, numbers
⚫ ADD #$5D,R1
Basic Input/Output
Operations
I/O
⚫ The data on which the instructions operate
are not necessarily already stored in memory.
⚫ Data need to be transferred between
processor and outside world (disk, keyboard,
etc.)
⚫ I/O operations are essential, the way they are
performed can have a significant effect on the
performance of the computer.
Program-Controlled I/O
Example
⚫ Consider a task that reads in character input
from a keyboard and produces character output
on a display screen.
⚫ Rate of data transfer (keyboard, display, processor)
⚫ Limited by the typing speed of the user
⚫ The rate of output transfers from the computer to the
display is much higher.
⚫ Determined by the rate at which characters can be
transmitted over the link between the computer and the
display device
⚫ Difference in speed between processor and I/O device
creates the need for mechanisms to synchronize the
transfer of data.
Program-Controlled I/O
Example..
⚫ A solution:
⚫ On output, the processor sends the first
character and then waits for a signal from the
display that the character has been received.
⚫ It then sends the second character.
⚫ Input is sent from the keyboard in a similar way.
⚫ The processor waits for a signal indicating that a
character key has been struck and that its code is
available in some buffer register associated with the
keyboard.
⚫ Then the processor proceeds to read that code.
Program-Controlled I/O
Example..
Bus
Processor
DATAIN DATAOUT
SIN SOUT
- Registers
- Flags K eyboard Display
- Device interface
MoveByte DATAlN,R1
MoveByte R1,DATAOUT
Program-Controlled I/O
Example..
⚫ Let us assume that bit b3 in registers INSTATUS
and OUTSTATUS corresponds to SIN and SOUT,
respectively.
⚫ The read operation may be implemented by the
machine instruction sequence
READWAIT Testbit #3, INSTATUS
Branch=0 READWAIT
MoveByte DATAIN, R1
⚫ The write operation may be implemented as
WRITEWAIT Testbit #3, OUTSTATUS
Branch=0 WRITEWAIT
MoveByte R1,DATAOUT
Program-Controlled I/O
Example..
Program-Controlled I/O
Example..
⚫ Assumption – the initial state of SIN is 0 and the
initial state of SOUT is 1.
⚫ Any drawback of this mechanism in terms of
efficiency?
⚫ Two wait loops→processor execution time is wasted
⚫ Alternate solution?
⚫ Interrupt
Stacks and Queues
Stacks
⚫ A stack is a list of data elements, usually words or bytes,
with the accessing restriction that elements can be
added or removed at one end of the list only.
⚫ This end is called the top of the stack, and the other end is called
the bottom.
⚫ The structure is sometimes referred to as a pushdown
stack.
⚫ Last-In-First-Out (LIFO) stack - the last data item placed
on the stack is the first one removed when retrieval
begins.
⚫ The terms push and pop are used to describe placing a
new item on the stack and removing the top item from
the stack, respectively.
Stacks..
⚫ Assume that the first element is placed in
location BOTTOM, and when new elements are
pushed onto the stack, they are placed in
successively lower address locations.
⚫ Figure 2.21 shows a stack of word data items in
the memory of a computer.
⚫ It contains numerical values, with 43 at the
bottom and -28 at the top.
⚫ Stack pointer (SP) - A register used to keep track
of the address of the element of the stack that is
at the top at any given time.
Stacks..
Stacks..
⚫ Assuming 32-bit word length, the PUSH
operation will decrement the stack pointer by 4
and copies the source into the stack.
⚫ Can be implemented as
Subtract #4,SP
Move NEWITEM,(SP)
⚫ Or using autodecrement mode as
Move NEWITEM,-(SP)
Stacks..
⚫ Assuming 32-bit word length, the POP operation
will copy the content from the stack to the
destination and increment the stack pointer by 4.
⚫ Can be implemented as
Move (SP),ITEM
Add #4,SP
⚫ Or using autoincrement mode as
Move (SP)+,ITEM
Stacks..
Stacks..
⚫ Stack is usually allocated a fixed amount of
space in the memory.
⚫ We must avoid pushing an item on a full stack
and popping an item from an empty stack.
⚫ Could result from a programming error.
⚫ Suppose that a stack runs from location 2000
(BOTTOM) down to 1500.
⚫ The stack pointer is loaded initially with address
value 2004.
Stacks..
Stacks..
⚫ The Compare instruction
Compare src,dst
performs the operation
[dst] - [src]
and sets the condition code flags according to
the result.
⚫ It does not change the value of either operand.
Queues
⚫ A queue is a list of data elements that works on
first-in-first-out (FIFO) basis.
⚫ Data are stored in and retrieved from a queue
on a first-in-first-out (FIFO) basis.
⚫ If we assume that the queue grows in the
direction of increasing addresses in the memory,
new data are added at the back (high-address
end) and retrieved from the front (low-address
end) of the queue.
Differences between Stacks
and Queues
Stacks Queues
One end of the stack is Both ends of a queue move
fixed (the bottom), while the to higher addresses as
other end rises and falls as data are added at the back
data are pushed and and removed from the
popped. front.
A single pointer is needed Two pointers are needed to
to point to the top of the keep track of the two ends
stack at any given time. of the queue.
A stack is limited by the top A queue would
and bottom of the stack in continuously move through
the memory. the memory of a computer
in the direction of higher
addresses.
Queues..
⚫ One way to limit the queue to a fixed region in memory is
to use a circular buffer.
⚫ Let us assume that memory addresses from
BEGINNING to END are assigned to the queue.
⚫ The first entry in the queue is entered into location BEGINNING,
and successive entries are appended to the queue by entering
them at successively higher addresses.
⚫ By the time the back of the queue reaches END, space will have
been created at the beginning if some items have been removed
from the queue.
⚫ Hence, the back pointer is reset to the value BEGINNING and
the process continues.
Subroutines
⚫ In a given program, it is often necessary to
perform a particular subtask many times on
different data values.
⚫ Such a subtask is usually called a subroutine.
⚫ For example, a subroutine may evaluate the
sine function or sort a list of values into
increasing or decreasing order.
Subroutines..
⚫ To save space, only one copy of the instructions
of the subroutine is placed in the memory, and
any program that requires the use of the
subroutine simply branches to its starting
location.
⚫ When a program branches to a subroutine, we
say that it is calling the subroutine.
⚫ The instruction that performs this branch
operation is named a Call instruction.
Subroutines..
⚫ After a subroutine has been executed, the
calling program must resume execution,
continuing immediately after the instruction that
called the subroutine.
⚫ The subroutine is said to return to the program
that called it by executing a Return instruction.
Subroutines..
⚫ While a subroutine is called, provision must be
made for returning to the appropriate location.
⚫ The location where the calling program resumes
execution is the location pointed to by the
updated PC while the Call instruction is being
executed.
⚫ Hence, the contents of the PC must be saved by
the Call instruction to enable correct return to the
calling program.
Subroutines..
⚫ Subroutine linkage method - The way in which a
computer makes it possible to call and return
from subroutines.
⚫ Simplest method - save the return address in a
specific location, which may be a register
dedicated to this function.
⚫ Such a register is called the link register.
⚫ When the subroutine completes its task, the
Return instruction returns to the calling program
by branching indirectly through the link register.
Subroutines..
⚫ The Call instruction is a special branch
instruction that performs the following
operations:
⚫ Store the contents of the PC in the link register
⚫ Branch to the target address specified by the
instruction
⚫ The Return instruction is a special branch
instruction that performs the operation:
⚫ Branch to the address contained in the link register
Subroutines..
Subroutine Nesting and the
Processor Stack
⚫ Subroutine nesting – subroutine calling
another subroutine
⚫ In this case, the return address of the second
call is also stored in the link register,
destroying its previous contents.
⚫ The contents of the link register has to be saved
in some other location before calling another
subroutine.
⚫ Otherwise, the return address of the first
subroutine will be lost.
Subroutine Nesting and the
Processor Stack..
⚫ Subroutine nesting can be carried out to any
depth.
⚫ Eventually, the last subroutine called,
completes its computations and returns to the
subroutine that called it.
⚫ The return address needed for this first return
is the last one generated in the nested call
sequence.
⚫ That is, return addresses are generated and
used in a last-in-first-out order.
Subroutine Nesting and the
Processor Stack..
⚫ The return addresses associated with
subroutine calls should be pushed onto a
stack.
⚫ Many processors do this automatically as one of
the operations performed by the Call instruction.
⚫ A particular register is designated as the
stack pointer, SP, to be used in this operation.
⚫ The stack pointer points to a stack called the
processor stack.
Subroutine Nesting and the
Processor Stack..
⚫ The Call instruction pushes the contents of
the PC onto the processor stack and loads
the subroutine address into the PC.
⚫ The Return instruction pops the return
address from the processor stack into the
PC.
Parameter Passing
⚫ Parameter passing - The exchange of
information between a calling program and a
subroutine.
⚫ When calling a subroutine, a program must
provide to the subroutine the parameters, that
is, the operands or their addresses, to be
used in the computation.
⚫ Later, the subroutine returns other
parameters, in this case, the results of the
computation.
Parameter Passing..
⚫ The parameters may be placed in registers or
in memory locations, where they can be
accessed by the subroutine.
⚫ Alternatively, the parameters may be placed
on the processor stack used for saving the
return address.
Parameter Passing..