1 / 38

Chapter 2-3 Addressing Modes

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

isla
Télécharger la présentation

Chapter 2-3 Addressing Modes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. op rs rt rd shamt funct op rs rt16 bit constant/address op 26 bit address Addressing Mode Field for MIPS ISA • Formats:rs: first source, rt: second source, rd: destinationshamt: shift amount, funct: function 6bits[31-26] 5bits[25-21] 5bits[20-16] 5bits[15-11] 5bits[10-6] 6bits[5-0] R-type I-type J-type Operation code Operands

  4. Example of Register Indirect Addressing - to access successive numbers in the list ------------------------------------------------------------------------------------------ Address Contents ------------------------------------------------------------------------------------------ Move N,R1 | Move #NUM1,R2 | Initialization Clear R0 | + LOOP Add (R2),R0 | Increment R2 | Decrement R1 +---------------------------- Branch>0 LOOP Move R0,SUM absolute immediate • 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 N n

  5. More Addressing Modes • 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

  6. Two Ways of Using the Index Mode

  7. Example of using index addressing modes Student information stored in a computer

  8. 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 Move #LIST,R0 | Move N,R1 | Initialization Clear R2 | Clear R3 | LOOP Add 2(R0),R2 Add 3(R0),R3 Add #4,R0 Decrement R1 Branch>0 LOOP Move R2,SUM2 Move R3,SUM3

  9. 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?

  10. Relative Addressing Mode • Branch > 0 LOOP • When LOOP designates a relative distance from the PC, which typically points to the next instruction

  11. 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 Move N,R1 | Move #NUM1,R2 | Initialization Clear R0 | LOOP Add (R2)+,R0 Decrement R1 Branch>0 LOOP Move R0,SUM The autoincrement mode makes it possible to eliminate the Increment instruction (i.e., Increment R2) N

  12. 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?

  13. Translation • High-level Language: C = A + B • Assembly Language: ADD r1,r2,r3 • Machine Language: 0x04010203 • Linking • Loading • Executing ?

  14. Assembly Language • 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

  15. Assembly Language (cont.) • 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)

  16. Assembler Directives and Symbols • 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

  17. Assembler Directives and Symbols • 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

  18. 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

  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. Two Pass Assembler • 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

  21. Program Development Process Program Development Program Execution Write program in high-level language Load the application into computer’s memory Loader Compile program into assembly language Execute the application Compiler Assemble program into machine language Assembler Link multiple machine-language programs into one application Linker

  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 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

  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. Program Controlled I/O • 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

  29. Example: Output • 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

  30. 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; 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

  31. 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 READWAIT Tesbit #3, IOSTATUS Branch=0 READWAIT Move 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

  32. Example: read a line of characters from the keyboard and send them out to the display device Move #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 READ TesBit #3, INSTATUS ; wait for the character to be entered Branch=0 READ in the keyboard buffer DATAIN MoveByte 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 ECHO MoveByte (R0), DATOUT ; 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. LOC (buffer for a line of characters)

  33. Queue 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)

  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 SP

  36. 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 #4, SP Decrement SP Move NEWITEM, (SP) • Pop ITEM Move (SP), ITEM Add #4, SP Increment SP

  37. Stack Properties • 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

  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 acircular queue

More Related