Computer Organization and Design Addressing Modes

Slides:



Advertisements
Similar presentations
Henk Corporaal TUEindhoven 2011
Advertisements

1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
Branches Two branch instructions:
The University of Adelaide, School of Computer Science
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 5 MIPS ISA & Assembly Language Programming.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson slides3.ppt Modification date: March 16, Addressing Modes The methods used in machine instructions.
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
The University of Adelaide, School of Computer Science
L5 – Addressing Modes 1 Comp 411 – Spring /29/08 Operands and Addressing Modes Where is the data? Addresses as data Names and Values Indirection.
CPSC CPSC 3615 Computer Architecture Chapter 3 Instructions and Addressing (Text Book: Chapter 5)
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
1 Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Lecture 4: MIPS Instruction Set
Chapter 2 Decision-Making Instructions (Instructions: Language of the Computer Part V)
 1998 Morgan Kaufmann Publishers MIPS arithmetic All instructions have 3 operands Operand order is fixed (destination first) Example: C code: A = B +
Computer Organization and Design Addressing Modes Montek Singh Wed, Sep 19, 2012 Lecture 6.
Computer Organization and Design Addressing Modes Montek Singh Sep 30, 2015 Lecture 7.
Computer Organization Instructions Language of The Computer (MIPS) 2.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Computer Organization Rabie A. Ramadan Lecture 3.
Computer Organization Instructions Language of The Computer (MIPS) 2.
Control Structures Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Conditional Control Structure if ( i < j ) goto A; else.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Computer Organization and Design Pointers, Arrays and Strings in C
CS/COE 0447 (term 2181) Jarrett Billingsley
MIPS Instruction Set Advantages
CSCI206 - Computer Organization & Programming
CS2100 Computer Organisation
MIPS Coding Continued.
Lecture 4: MIPS Instruction Set
These are slides from Comp411
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) {
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
CSCI206 - Computer Organization & Programming
Henk Corporaal TUEindhoven 2010
These are slides from Comp411
ECE232: Hardware Organization and Design
Operands and Addressing Modes
October 1 Programming Questions?
September 5 Fang-Yi’s Office Hours Friday 10am  12pm (and another TBD) Programming Language Issues (JAVA vs. C, Pointers vs. Arrays) Representations Instructions.
The University of Adelaide, School of Computer Science
Instruction encoding The ISA defines Format = Encoding
MIPS coding.
Computer Organization and Design Assembly & Compilation
Part II Instruction-Set Architecture
Other ISAs Next, we’ll first we look at a longer example program, starting with some C code and translating it into our assembly language. Then we discuss.
Flow of Control -- Conditional branch instructions
3.
Instruction encoding The ISA defines Format = Encoding
COMS 361 Computer Organization
COMS 361 Computer Organization
Instruction encoding The ISA defines Format = Encoding
Flow of Control -- Conditional branch instructions
Instruction encoding The ISA defines Format = Encoding
MIPS Coding Continued.
Addressing Modes Don Porter.
Computer Organization and Design Addressing Modes
Operands and Addressing Modes
9/27: Lecture Topics Memory Data transfer instructions
7/6/
August 31 addresses Drop box Questions? 31 August 2004
Conditional Branching (beq)
Presentation transcript:

Computer Organization and Design Addressing Modes Montek Singh Mon, Sep 25, 2013 Lecture 6

Operands and Addressing Modes Where is the data? Addresses as data Names and Values Indirection Reading: Ch. 2.3, 2.14

Pointers and Arrays in C Operands in memory and their addresses

C vs. Java For our purposes C is almost identical to Java except: C has “functions”, JAVA has “methods” function == method without “class” i.e., a global method C has “pointers” explicitly Java has them (called “references”) but hides them under the covers JVM takes care of handling pointers, so the programmer doesn’t have to C++ is sort of in-between C and Java In this class, we will see how pointers/references are implemented at the assembly language level

What is a “pointer” in C? A pointer is an explicit memory address Example int i i is an integer variable located at, say, address 1056 int *p p is a variable that “points to” an integer p is located at, say, address 2004 p = &i the value in p is now equal to the address of variable i i.e., the value stored in Mem[2004] is 1056 (the location of i) Memory address i 1056 1060 1064 1068 2000 p 1056 2004

Referencing and Dereferencing Referencing an object means … taking its address and assigning it to a pointer variable (e.g., &i) Dereferencing a pointer means … going to the memory address pointed to by the pointer, and accessing the value there (e.g., *p) Example int i; // i is an int variable int *p; // p is a pointer to int p = &i; // referencing i // p is assigned 1056 *p = 5; // dereference p // i assigned 5 Memory address i 5 1056 1060 1064 1068 2000 p 1056 2004

Pointer expressions and arrays Dereferencing could be done to an expression So, not just *p, but can also write *(p+400) accesses memory location that is 400th int after i Arrays in C are really pointers underneath! int a[10]; // array of integers a itself simply refers to the address of the start of the array a is the same as &a[0] a is a constant of type “int *” a[0] is the same as *a a[1] is the same as *(a+1) a[k] is the same as *(a+k) a[j] = a[k]; is the same as *(a+j) = *(a+k);

Pointer arithmetic and object size IMPORTANT: Pointer expressions automatically account for the size of object pointed to Example 1 if p is of type “int *” and an int is 4 bytes long if p points to address 1056, (p=p+2) will point to address 1064 C compiler automatically does the multiply-by-4 BUT… in assembly, the programmer will have to explicitly do the multiply-by-4 Example 2 char *q; q++; // really does add 1 Memory address i 1056 1060 1064 1068 2000 p 1056 1064 2004

Pointer examples int i; // simple integer variable int a[10]; // array of integers int *p; // pointer to integer p = &i; // & means address of p = a; // a means &a[0] p = &a[5]; // address of 6th element of a *p // value at location pointed by p *p = 1; // change value at that location *(p+1) = 1; // change value at next location p[1] = 1; // exactly the same as above p++; // step pointer to the next element

Pointer pitfalls int i; // simple integer variable int a[10]; // array of integers int *p; // pointer to integer(s) So what happens when p = &i; What is value of p[0]? What is value of p[1]?  Very easy to exceed bounds (C has no bounds checking)

Iterating through an array 2 ways to iterate through an array using array indices void clear1(int array[], int size) { for(int i=0; i<size; i++) array[i] = 0; } using pointers void clear2(int *array, int size) { for(int *p = &array[0]; p < &array[size]; p++) *p = 0; or, also using pointers, but more concise (more cryptic!) void clear3(int *array, int size) { int *arrayend = array + size; while(array < arrayend) *array++ = 0;

Pointer summary In the “C” world and in the “machine” world: Examples: a pointer is just the address of an object in memory size of pointer itself is fixed regardless of size of object to get to the next object: in machine code: increment pointer by the object’s size in bytes in C: increment pointer by 1 to get the ith object: in machine code: add i*sizeof(object) to pointer in C: add i to pointer Examples: int R[5]; // 20 bytes storage R[i] is same as *(R+i) int *p = &R[3] is same as p = (R+3) (p points 12 bytes after start of R)

What are all the different ways to specify an operand? Addressing Modes What are all the different ways to specify an operand?

Revisiting Operands Operands = the variables needed to perform an instruction’s operation Three types in the MIPS ISA: Register: add $2, $3, $4 # operands are the “contents” of a register Immediate: addi $2,$2,1 # 2nd source operand is part of the instruction Register-Indirect: lw $2, 12($28) # source operand is in memory sw $2, 12($28) # destination operand is memory Simple enough, but is it enough?

Common “Addressing Modes” MIPS can do these with appropriate choices for Ra and const Absolute (Direct): lw $8, 0x1000($0) Value = Mem[constant] Use: accessing static data Indirect: lw $8, 0($9) Value = Mem[Reg[x]] Use: pointer accesses Displacement: lw $8, 16($9) Value = Mem[Reg[x] + constant] Use: access to local variables Indexed: Value = Mem[Reg[x] + Reg[y]] Use: array accesses (base+index) Memory indirect: Value = Mem[Mem[Reg[x]]] Use: access thru pointer in mem Autoincrement: Value = Mem[Reg[x]]; Reg[x]++ Use: sequential pointer accesses Autodecrement: Value = Reg[X]--; Mem[Reg[x]] Use: stack operations Scaled: Value = Mem[Reg[x] + c + d*Reg[y]] Use: array accesses (base+index) Is the complexity worth the cost? Need a cost/benefit analysis!

Relative popularity of address modes From Hennessy & Patterson

Absolute (Direct) Addressing What we want: Contents of a specific memory location, i.e. at a given address Example: Caveat: Sometimes generates a two instruction sequence If the address for x chosen by the programmer/compiler/assembler is too large to fit within the 16-bit immediate field “MIPS Assembly” .data x: .word 10 .text main: lw $2,x($0) addi $2,$2,1 sw $2,x($0) Allocates space for a single integer (4-bytes) and initializes its value to 10 “C” int x = 10; main() { x = x + 1; } lui $1,xhighbits lw $2,xlowbits($1)

Absolute (Direct) Addressing: More detail “MIPS Assembly” .data x: .word 10 .text main: lw $2,x($0) addi $2,$2,1 sw $2,x($0) “After Compilation” .data 0x0100 x: .word 10 .text main: lw $2,0x100($0) addi $2,$2,1 sw $2,0x100($0) “C” int x = 10; main() { x = x + 1; } Assembler replaces “x” by its address e.g., here the data part of the code (.data) starts at 0x100 x is the first variable, so starts at 0x100 this mode works in MIPS only if the address fits in 16 bits

Indirect Addressing What we want: Examples: Caveats The contents at a memory address held in a register Examples: Caveats You must make sure that the register contains a valid address (double, word, or short aligned as required) “la” is not a real instruction, It’s a convenient pseudoinstruction that constructs a constant via either a 1 instruction or 2 instruction sequence “MIPS Assembly” .data x: .word 10 .text main: la $2,x addi $3,$0,2 sw $3,0($2) ori $2,$0,x “C” int x = 10; main() { int *y = &x; *y = 2; } lui $2,xhighbits ori $2,$2,xlowbits $2 performs the role of y, i.e., it is a pointer to x

Note on la pseudoinstruction la is a pseudoinstruction: la $r, x stands for “load the address of” variable x into register r not an actual MIPS instruction but broken down by the assembler into actual instructions if address of x is small (fits within 16 bits), then a single ori one could also use a single addiu if address of x is larger, use the lui + ori combo “MIPS Assembly” .data x: .word 10 .text .global main main: la $2,x addi $3,$0,2 sw $3,0($2) ori $2,$0,0x100 0x0100 0x80000010 lui $2,0x8000 ori $2,$2,0x0010

Displacement Addressing What we want: contents of a memory location at an offset relative to a register the address that is the sum of a constant and a register value Examples: Remember: Must multiply (shift) the “index” to be properly aligned “MIPS Assembly” .data 0x0100 a: .space 20 .text main: addi $2,$0,3 // i in $2 addi $3,$0,2 sll $1,$2,2 // i*4 in $1 sw $3,a($1) “C” int a[5]; main() { int i = 3; a[i] = 2; } Allocates space for a 5 uninitialized integers (20-bytes)

Displacement Addressing: 2nd example What we want: The contents of a memory location at an offset relative to a register Examples: Note: offsets to the various fields within a structure are constants known to the assembler/compiler “MIPS Assembly” .data p: .space 8 .text main: la $1,p addi $2,$0,13 sw $2,0($1) addi $2,$0,12 sw $2,4($1) “C” struct p { int x, y; } main() { p.x = 13; p.y = 12; } Allocates space for 2 uninitialized integers (8-bytes)

Assembly Coding Templates For common C program fragments

Conditionals: if-else There are little tricks that come into play when compiling conditional code blocks. For instance, the statement: if (y > 32) { x = x + 1; } compiles to: lw $24, y ori $15, $0, 32 slt $1, $15, $24 beq $1, $0, Lendif lw $24, x addi $24, $24, 1 sw $24, x Lendif: C code: if (expr) { STUFF } MIPS assembly: (compute expr in $rx) beq $rx, $0, Lendif (compile STUFF) Lendif: C code: if (expr) { STUFF1 } else { STUFF2 } MIPS assembly: (compute expr in $rx) beq $rx, $0, Lelse (compile STUFF1) beq $0, $0, Lendif Lelse: (compile STUFF2) Lendif:

Loops: while MIPS assembly: Lwhile: Lendw: Alternate MIPS assembly: C code: while (expr) { STUFF } MIPS assembly: Lwhile: (compute expr in $rx) beq $rX,$0,Lendw (compile STUFF) beq $0,$0,Lwhile Lendw: Alternate MIPS assembly: beq $0,$0,Ltest Lwhile: (compile STUFF) Ltest: (compute expr in $rx) bne $rX,$0,Lwhile Lendw: Compilers spend a lot of time optimizing in and around loops - moving all possible computations outside of loops - unrolling loops to reduce branching overhead - simplifying expressions that depend on “loop variables”

Loops: for Most high-level languages provide loop constructs that establish and update an iteration variable, which is used to control the loop’s behavior MIPS assembly: sum: .word 0x0 data: .word 0x1, 0x2, 0x3, 0x4, 0x5 .word 0x6, 0x7, 0x8, 0x9, 0xa add $30,$0,$0 Lfor: lw $24,sum($0) sll $15,$30,2 lw $15,data($15) addu $24,$24,$15 sw $24,sum addi $30,$30,1 slti $24,$30,10 bne $24,$0,Lfor Lendfor: C code: int sum = 0; int data[10] = {1,2,3,4,5,6,7,8,9,10}; int i; for (i=0; i<10; i++) { sum += data[i] }

This week’s lab On Friday: We’ll write some real assembly code Play with a simulator Bring your laptop computers!