Register mode

advertisement
Chapter 2-3
Addressing Modes



Addressing Modes
Stacks and Queues
Next Lecture
 Subroutines
 Parameter Passing
 Stack Frame
 Additional Instructions
Addressing modes





Register mode : the operand is the contents of a CPU register named
in the instruction
 Move R1,R2
Immediate mode: the operand is given explicitly in the instruction
 Move #200,R0
 200 is data (at the time of exe.)
Absolute mode : the operand is in a memory location given explicitly
in the instruction
 Move 200,R0
 200 is an address pointing to contents to be loaded to R0
Register indirect mode: the effective address of the operand is the
contents of a register. The location appears in the register in the
instruction. The register or memory location that contains the address
of the operand is called a pointer
 Move (R1),R2
 R1 contains an address
Indirect mode: the effective address of the operand is the contents of
a main memory location whose address appears in the instruction.
 Move (200),R2
 200 is an address, which contains another
address pointing to the actual operand
2
Addressing Mode Field for MIPS ISA

Formats:
rs: first source, rt: second source, rd: destination
shamt: shift amount, funct: function
6bits[31-26] 5bits[25-21] 5bits[20-16] 5bits[15-11] 5bits[10-6]
R-type
op
rs
rt
I-type
op
rs
rt
J-type
op
Operation code
3
rd
shamt
6bits[5-0]
funct
16 bit constant/address
26 bit address
Operands
Example of Register Indirect Addressing
- to access successive numbers in the list
-----------------------------------------------------------------------------------------Address
Contents
-----------------------------------------------------------------------------------------Move
N,R1
| absolute
Move
#NUM1,R2
| Initialization
Clear
R0
| immediate
+
LOOP
Add
(R2),R0
|
Increment
R2
|
Decrement
R1
+---------------------------- Branch>0
LOOP
Move
R0,SUM
• N is an address, pointing to the mem location, containg the
number of integers. Operation: (R1)  (N)
• #NUM is an immediate value and is the address of the location
of the first data to be added. Operation: (R2)  NUM1
• R2 is used as a pointer
4
N
n
More Addressing Modes

5
Index mode: the effective address of the operand is generated
by adding a constant value to the contents of a register
 The register may either a special register provided for this
purpose in some computers or a more commonly a general
purpose register
 This register is called the index register
 Notation X(R) => E.A. = X + [R]
 X is called offset or displacement.
 Usage: For array access
 By manipulating R
Two Ways of Using the Index Mode
6
Example of using index addressing modes
Student information stored in a computer
7
Example of Using Index Mode (cont.)



A four-word item is used to store relevant information for each
student
There are n students in the class
We wish to compute the sum of the scores of tests 2 and 3
LOOP
8
Move
Move
Clear
Clear
Add
Add
Add
Decrement
Branch>0
Move
Move
#LIST,R0
N,R1
R2
R3
2(R0),R2
3(R0),R3
#4,R0
R1
LOOP
R2,SUM2
R3,SUM3
|
| Initialization
|
|
Example of Using Index Mode (cont.)





R0 is used as the index register and is initially set to
point to the ID location of the first student record
R1 serves as a loop counter
R2 and R3 are used to accumulate the sums of the
scores obtained on tests 2 and 3
There are several basic variation of the basic
addressing modes presented above
(Ri,Rj): the content of a second register may be used
as the index constant X => the effective address is
the sum of the contents of Ri and Rj
X(Ri,Rj): the effective address is the sum of the
constant X and the contents of Ri and Rj
Q?
9
Relative Addressing Mode

10
Branch > 0 LOOP
 When LOOP designates a relative distance from
the PC, which typically points to the next
instruction
More Addressing Modes

Auto increment 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 incremented to point to the
next item on the list
LOOP
Move
Move
Clear
Add
Decrement
Branch>0
Move
N,R1
#NUM1,R2
R0
(R2)+,R0
R1
LOOP
R0,SUM
The autoincrement mode makes it
possible to eliminate the Increment
instruction (i.e., Increment R2)
11
|
|
|
Initialization
N
More Addressing Modes

Autodecrement mode : the content of a register specified
in the instruction is decremented first. This content is
then used as the effective address of the operand
notation : -(R4)
Q?
12
Translation

High-level Language: C = A + B

Assembly Language: ADD r1,r2,r3

Machine Language: 0x04010203

Linking
Loading
Executing


13
?
Assembly Language





14
Machine instructions are represented by patterns
of 0s and 1s
These patterns are difficult to deal with when
writing a program
Instead we use symbols to represent the patterns
A complete set of these symbols and some rules
for their use constitute a programming language
called assembly language
The symbolic names are called mnemonics
Assembly Language (cont.)




15
The rules are called the syntax of the language
Programs written in an assembly language can be
automatically translated into a sequence of machine
instructions
The translator is commonly called an assembler
Example:
MOVE R0, SUM
ADD #5,R3 or ADDI 5,R3
MOVE #5,(R2) or MOVEI 5,(R2)
Assembler Directives and Symbols



16
How to interpret names and symbols for constants
Where to place the instructions in the memory
Where to place the data operands in the memory
Assembler Directives and Symbols





17
SUM EQU 200
 this is not an instruction that will be executed when the
object program is run.
ORIGIN
 This tells the assembler where in the main memory to place
the code or data block
DATA
 command used to inform the assembler about the data block
LABEL
 a label is assigned a value equal to the address location
RETURN
 identifies the point at which execution should be terminated
Example: A Complete Code Section
Memory
Addressing
address
Operation
or data
label
information
-----------------------------------------------------------------------------------Assembler
SUM
EQU
200
Directives
ORIGIN
201
N
DATAWORD
300
NUM1
RESERVE
301
ORIGIN
100
Statements that START
MOVE
N,R1
Generate
MOVE
#NUM1,R2
machine
CLR
R0
Instructions
LOOP
ADD
(R2),R0
INC
R2
DEC
R1
BGTZ
LOOP
MOVE
R0, SUM
Assembler
RETURN
Directives
END
START
18
Assembler






19
A source program written in an assembly language must be
assembled into a machine code object program before it can be
executed
The assembler replaces all names and symbols with the numerical
values that they represent
A key part of the assembly process is determining the values that
replace the names
The assembler computes the branch offset and puts it into the
machine instruction
 A branch instruction is usually implemented in machine code
by specifying the distance between its next instruction and the
instruction at the target branch: branch offset (relative
addressing)
As the assembler scans the program, it keeps track of all names
and their corresponding numerical values in a symbol table.
What about forward branches: the name appears as an operand
before it is given a value
Two Pass Assembler


20
First pass: the assembler creates a symbol table
Second pass: the assembler goes through the
source program and substitutes values for all
names from the symbol table
Program Development Process
Program Development
Program Execution
Write program in
high-level language
Load the application into
computer’s memory
Compiler
Compile program into
assembly language
Execute the application
Assembler
Assemble program into
machine language
Linker
Link multiple
machine-language programs
into one application
21
Loader
Linker


22
A linker binds a subprogram (object module)
together with any other library modules it refers to
into an executable, complete program.
A linker resolves cross-references between separately
compiled or assembled object modules and then
assigns final addresses to create a single relocatable
load module.
Loader




23
Loader performs a sequence of input operations
needed to transfer the machine language program
from the disk into the memory
Loader must know the length of the program and the
address in main memory where it will be stored
Assembler usually places the above information in
the header preceding the object code
After loading the object code, the loader starts
executing the first instruction
Debugger



Assembler usually detects and reports syntax errors
Debugger helps the user find other mistakes and
logical errors
Debugger enables the user to interrupt executions in
order to examine the contents of various registers
and memory locations
Number notation
 ADD #93,R1 : decimal
 ADD #%01011101, R1 : binary
 ADD #$5D,R1 : hex
24
Direct I/O and Memory Mapped I/O

I/O mapped I/O (Direct I/O)



Special I/O instructions are provided
Data can only be transferred between special registers and
locations in the I/O address space using special instructions
such as In, Out
Example:
In Rs, port1 and
Out Rs, port1

Memory mapped I/O -- Some memory address values are used to refer
to peripheral device buffers such as DATAIN and DATAOUT



No special instructions are needed
Data can be transferred between these registers and CPU
using regular memory access instructions such as Move,
Load or Store
Example:
Move DATAIN,R1 and
Move R1,DATAOUT
25
Basic I/O Operations w/ an Example



26
Consider a task that requires a character input from the
keyboard of a video terminal and produces a character
output that is displayed on the screen of the same
terminal
The rate of data transfer from the keyboard to the
computer is limited by the typing speed of the user:
few characters per second
The rate of output transfers from the computer to the
terminal is higher and is limited by the rate at which
characters can be transmitted over the link between the
computer and the terminal: much faster than key input
Block Diagram of the Example
27
Program Controlled I/O

Moving a character code from the keyboard to the CPU





28
Striking a key stores the corresponding character code in an 8bit buffer associated with keyboard: DATAIN
To inform the CPU that a valid character is in DATAIN, a flag
(SIN) is set to 1
A program monitors (polls) SIN, and when it equals 1, it reads
the contents of DATAIN
When character is transferred to the CPU, SIN is automatically
cleared to 0
If a second character is entered at the keyboard, SIN is again
set to 1 and the process repeats
Example: Output


29
Moving a Character from the CPU to the Display
 A buffer DATAOUT and a flag SOUT are used for the transfer
 When SOUT is 1, the display is ready to receive a character
 The CPU monitors SOUT and when SOUT is set to 1, the CPU
transfers a character code to DATAOUT, which clears SOUT
 When the character has been displayed and the display device
is ready, SOUT is again set to 1
 The buffers DATAIN and DATAOUT and the flags SIN and
SOUT are part of the Device interface circuitry
The control flags SIN and SOUT are automatically cleared when
the DATAIN and DATAOUT are referenced
Execution Process of the Example

The CPU can monitor SIN and transfer a character from DATAIN
to R1 by the following sequence of operations
READWAIT Branch to READWAIT if SIN = 0;
Input from DATAIN to R1


no character
The input operation also resets SIN to 0
Transferring output to the display
WRITEWAIT Branch to WRITEWAIT if SOUT = 0; display not rdy
output from R1 to DATAOUT

30
The output operation also clears SOUT to 0
Using the IOSTATUS register

Usually SIN and SOUT are part of a single register called IOSTATUS
Assume bit b3 is SIN and bit b4 is SOUT

Read operation

b7
b6
b5
SOUT
SIN
b2
b1
READWAIT Tesbit #3, IOSTATUS
Branch=0 READWAIT
Move DATAIN,R1

Write operation
WRITEWAIT Tesbit #4, IOSTATUS
Branch=0 WRITEWAIT
Move R1,DATAOUT

31
The testbit instructions test the state of one bit in IOSTATUS
b0
Example: read a line of characters from the keyboard
and send them out to the display device
Move
READ
TesBit #3, INSTATUS
Branch=0 READ
MoveByte DATIN, (R0)
ECHO
TestBit #3, OUTSTATUS
Branch=0 ECHO
MoveByte (R0), DATOUT
LOC
32
#LOC, R0
; initialize pointer register R0 to point to the
address of the first location in the memory
where the characters are to be stored
; wait for the character to be entered
in the keyboard buffer DATAIN
transfer the character from DATIN
into the memory (this clears SIN to 0)
; wait for the display to become ready
; move the character just read to the display
buffer register (this clears SOUT to 0)
Compare, #CR, (R0)+
; check if the character just read is CR
(carriage return). If it not CR, then
Branch ≠ 0 READ
; branch back and read another character.
Also increment the pointer to store the
next character.
(buffer for a line of characters)
Stacks and Queues



Both stacks and queues are data storages
Stack: last-in-first-out (LIFO) or first-in-last-out (FILO)
Queue: first-in-first-out (FIFO)
Queue
33
Typical Pushdown Stack





34
A stack is a list of data
elements, usually words with
the access restriction that
elements can be added or
removed at one end of the
list only
Top of the stack: one end of
the list where items are
added and from which items
are removed
Bottom of the stack: the
other end
SP: stack pointer
Last-in-first-out (LIFO)
Push and Pop
SP
35
Operations On a Stack





Push: place a new item on the stack
Pop: remove a top item from the stack
Convention: the stack grows in the direction of decreasing
memory addresses
SP: stack pointer
Example implementation for byte addressable memory with 32
bit word length

Push
NEWITEM
Subtract
Move

Pop
Move
Add
36
#4, SP
NEWITEM, (SP)
Decrement SP
ITEM
(SP), ITEM
#4, SP
Increment SP
Stack Properties







37
Because stack operations are used frequently, some processors
provide a single instruction to perform push and pop
Also if a processor has the autoincrement and autodecrement
addressing modes, then the push and pop operation can be
performed with a single instruction
Push: Move NEWITEM, -(SP)
Pop : Move (SP)+, ITEM
A stack may be allocated a fixed amount of space in the
memory
Avoid pushing an item onto the stack when it has reached its
maximum size
Avoid popping an item off an empty stack
Queue





Data are stored and retreived from a queue on a first-in-firstout (FIFO) basis
Assume the queue grows in the direction of increasing
addresses in memory
New data is added at the back (high address end)
Data is removed from the front (low address end)
Example: a network queue
Differences between stack and queue
 Adding and removing data
 Only one end of the stack moves => a single pointer is needed
by the stack (top of stack)
 Both ends of the queue can move => two pointers are needed,
one for each of the queue
 Without control, a queue can keep moving through the memory
 Using a circular queue
38
Download