chapter 2-3 addressing modes addressing modes stacks and queues next lecture subroutines ...
TRANSCRIPT
Chapter 2-3Addressing Modes
Addressing Modes Stacks and Queues Next Lecture
Subroutines Parameter Passing Stack Frame Additional Instructions
2
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
3
Addressing Mode Field for MIPS ISA
Formats:rs: first source, rt: second source, rd: destinationshamt: shift amount, funct: function
op rs rt rd shamt funct
op rs rt 16 bit constant/address
op 26 bit address
R-type
I-type
J-type
6bits[31-26] 5bits[25-21] 5bits[20-16] 5bits[15-11] 5bits[10-6] 6bits[5-0]
Operation codeOperation code OperandsOperands
4
------------------------------------------------------------------------------------------Address Contents------------------------------------------------------------------------------------------
Move N,R1 |Move #NUM1,R2 |
InitializationClear R0 |
+ LOOP Add (R2),R0| Increment R2| Decrement R1+---------------------------- Branch>0 LOOP
Move R0,SUM
Example of Register Indirect Addressing - to access successive numbers in the list
• 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 pointerR2 is used as a pointer
NN nn
absolute
immediate
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
More Addressing Modes
6
Two Ways of Using the Index Mode
7
Student information stored in a computer
Example of using index addressing modes
8
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
Move #LIST,R0 |Move N,R1 | InitializationClear R2 |Clear R3 |
LOOP Add 2(R0),R2Add 3(R0),R3Add #4,R0Decrement R1Branch>0 LOOPMove R2,SUM2Move R3,SUM3
Example of Using Index Mode (cont.)
9
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 3There 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
Example of Using Index Mode (cont.)
Q?
10
Relative Addressing Mode
Branch > 0 LOOP When LOOP designates a relative distance
from the PC, which typically points to the next instruction
11
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
Move N,R1 |Move #NUM1,R2 | InitializationClear R0 |
LOOP Add (R2)+,R0Decrement R1Branch>0 LOOPMove R0,SUM
More Addressing Modes
The autoincrement mode makes The autoincrement mode makes it possible to eliminate the it possible to eliminate the Increment instruction (i.e., Increment instruction (i.e., Increment R2)Increment R2)
NN
12
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)
More Addressing Modes
Q?
13
Translation
High-level Language: C = A + B
Assembly Language: ADD r1,r2,r3
Machine Language: 0x04010203
Linking Loading Executing ?
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
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, SUMADD #5,R3 or ADDI 5,R3MOVE #5,(R2) or MOVEI 5,(R2)
Assembly Language (cont.)
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
Assembler Directives and Symbols
18
Memory Addressing
address Operation or datalabel information
------------------------------------------------------------------------------------Assembler SUM EQU 200Directives ORIGIN 201
N DATAWORD 300NUM1 RESERVE 301
ORIGIN 100Statements that START MOVE N,R1Generate MOVE #NUM1,R2machine CLR R0Instructions LOOP ADD (R2),R0
INC R2DEC R1BGTZ LOOPMOVE R0, SUM
Assembler RETURNDirectives END START
Example: A Complete Code Section
19
Assembler 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
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
Two Pass Assembler
21
Program Development Process
Write program in Write program in high-level languagehigh-level language
Compile program into Compile program into assembly languageassembly language
Assemble program into Assemble program into machine languagemachine language
Link multiple Link multiple machine-language programsmachine-language programs
into one applicationinto one application
Load the application intoLoad the application intocomputer’s memorycomputer’s memory
Execute the applicationExecute the application
Program DevelopmentProgram Development Program ExecutionProgram Execution
CompilerCompiler
AssemblerAssembler
LinkerLinker
LoaderLoader
22
Linker
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.
23
Loader
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
24
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
25
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 andOut 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 andMove R1,DATAOUT
26
Basic I/O Operations w/ an Example
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
27
Block Diagram of the Example
28
Moving a character code from the keyboard to the CPU
Striking a key stores the corresponding character code in an 8-bit 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
Program Controlled I/O
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
Example: Output
30
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; no character
Input from DATAIN to R1
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
The output operation also clears SOUT to 0
Execution Process of the Example
31
Usually SIN and SOUT are part of a single register called IOSTATUS
Assume bit b3 is SIN and bit b4 is SOUT
Read operation
READWAIT Tesbit #3, IOSTATUSBranch=0 READWAITMove DATAIN,R1
Write operation
WRITEWAIT Tesbit #4, IOSTATUS Branch=0 WRITEWAIT Move R1,DATAOUT
The testbit instructions test the state of one bit in IOSTATUS
Using the IOSTATUS register
b7 b6 b5 SOUT SIN b2 b1 b0
32
Move #LOC, R0 ; initialize pointer register R0 to point to the
address of the first location in the memorywhere the characters are to be stored
READ TesBit #3, INSTATUS ; wait for the character to be enteredBranch=0 READ in the keyboard buffer DATAINMoveByte DATIN, (R0) transfer the character from DATIN
into the memory (this clears SIN to 0)ECHO TestBit #3, OUTSTATUS; wait for the display to become ready
Branch=0 ECHOMoveByte (R0), DATOUT ; move the character just read to
the displaybuffer 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 thenext character.
LOC (buffer for a line of characters)
Example: read a line of characters from the keyboard and send them out to the display
device
33
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)
QueueQueue
34
Typical Pushdown Stack
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)
35
Push and Pop
SPSP
36
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 #4, SP Decrement SP
Move NEWITEM, (SP) Pop ITEM
Move (SP), ITEMAdd #4, SP Increment SP
Operations On a Stack
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
Stack Properties
38
Queue
Data are stored and retreived from a queue on a first-in-first-out (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