CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.

Slides:



Advertisements
Similar presentations
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.
Advertisements

4.
Lecture 5: MIPS Instruction Set
MIPS assembly. Review  Lat lecture, we learnt  addi,  and, andi, or, ori, xor, xori, nor,  beq, j, bne  An array is stored sequentially in the memory.
CS/COE0447 Computer Organization & Assembly Language
The University of Adelaide, School of Computer Science
Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Lecture 9: MIPS Instruction Set
The University of Adelaide, School of Computer Science
Computer Organization CS224
Systems Architecture Lecture 5: MIPS Instruction Set
Chapter 2 Instructions: Language of the Computer
Chapter 2.
The University of Adelaide, School of Computer Science
COMP3221 lec9-logical-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 9: C/Assembler Logical and Shift - I
Lecture 8: MIPS Instruction Set
1 CS61C L10 Fl. Pt. © UC Regents CS61C - Machine Structures Lecture 10 - Floating Point, Part II and Miscellaneous September 29, 2000 David Patterson
Lecturer PSOE Dan Garcia
Lecture 4: Loads, Logic, Loops. Review Memory is byte-addressable, but lw and sw access one word at a time. These instructions transfer the contents of.
inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 12 – Introduction to MIPS Procedures II & Logical Ops As of mid-2007,
Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Computer Architecture CPSC 321 E. J. Kim. Overview Logical Instructions Shifts.
CPSC CPSC 3615 Computer Architecture Chapter 3 Instructions and Addressing (Text Book: Chapter 5)
Logical & shift ops (1) Fall 2007 Lecture 05: Logical Operations.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
CS61C L9 MIPS Logical & Shift Ops, and Instruction Representation I (1) Beamer, Summer 2007 © UCB Scott Beamer, Instructor inst.eecs.berkeley.edu/~cs61c.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 6: Logic/Shift Instructions Partially adapted from Computer Organization and Design, 4.
CS61C L12 Introduction to MIPS : Procedures II & Logical Ops (1) Garcia, Fall 2006 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Lecture 6 Sept 16 Chapter 2 continue MIPS translating c into MIPS – examples MIPS simulator (MARS and SPIM)
Feb 18, 2009 Lecture 4-1 instruction set architecture (Part II of [Parhami]) MIPS assembly language instructions MIPS assembly programming.
More decisions and logic (1) Fall 2010 Lecture 4: Loads, Logic, Loops.
CHAPTER 2 ISA Instructions (logical + procedure call)
Lecture 4. Miscellaneous Addressing Mode, Memory Map, Pointer, and ASCII Prof. Taeweon Suh Computer Science Education Korea University ECM534 Advanced.
1 Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Lecture 4: MIPS Instruction Set
CHAPTER 6 Instruction Set Architecture 12/7/
 1998 Morgan Kaufmann Publishers MIPS arithmetic All instructions have 3 operands Operand order is fixed (destination first) Example: C code: A = B +
Computer Organization Instructions Language of The Computer (MIPS) 2.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
EET 4250 Instruction Representation & Formats Acknowledgements: Some slides and lecture notes for this course adapted from Prof. Mary Jane Penn.
Lec 6Systems Architecture1 Systems Architecture Lecture 6: Branching and Procedures in MIPS Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some.
Pointers and Arrays in C and Assembly Language. Arrays Provide useful abstraction Match up to machine memory organization Array index computation –time.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 14, 15 Addressing Mode.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Arithmetic Operations Add and subtract, three operands Two sources and one destination add a, b, c # a gets b + c All arithmetic operations have this form.
MIPS Procedures II, Logical & Shift Ops. Review Functions called with jal, return with jr $ra. The stack is your friend: Use it to save anything you need.
CSCI206 - Computer Organization & Programming
CS2100 Computer Organisation
COMPUTER ARCHITECTURE & OPERATIONS I
The University of Adelaide, School of Computer Science
MIPS Coding Continued.
Lecture 4: MIPS Instruction Set
Instructions - Type and Format
Single-Cycle CPU DataPath.
MISP Assembly.
Systems Architecture Lecture 5: MIPS Instruction Set
Solutions Chapter 2.
Logical and Decision Operations
Logical and Decision Operations
Part II Instruction-Set Architecture
CS61C : Machine Structures Lecture 2. 2
3.
MIPS Assembly.
MIPS Coding Continued.
MIPS assembly.
9/27: Lecture Topics Memory Data transfer instructions
MIPS instructions.
Conditional Branching (beq)
Presentation transcript:

CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson & Hennessy, ©2005 Some slides and/or pictures in the following are adapted from: slides ©2008 UCB 6

Arrays and Pointers Index: Use a register that holds the index i and increment the register in each step to effect moving from element i of the list to element i + 1 Pointer: Use a register that points to (holds the address of) the list element being examined and update it in each step to point to the next element Stepping through the elements of an array using the indexing method and the pointer updating method.

Arrays vs. Pointers Array indexing involves Multiplying index by element size Adding to array base address Pointers correspond directly to memory addresses Can avoid indexing complexity §2.14 Arrays versus Pointers

Selection Sort Example One iteration of selection sort. To sort a list of numbers, repeatedly perform the following: Find the max element, swap it with the last item, move up the “last” pointer

Selection Sort Using the Procedure max Example (continued) sort: beq $a0,$a1,done # single-element list is sorted jal max # call the max procedure lw $t0,0($a1) # load last element into $t0 sw $t0,0($v0) # copy the last element to max loc sw $v1,0($a1) # copy max value to last element addi $a1,$a1,-4 # decrement pointer to last element j sort # repeat sort for smaller list done:... # continue with rest of program Inputs to proc max Outputs from proc max In $a0 In $a1 In $v0 In $v1

Example Show a sequence of MIPS instructions corresponding to: clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } Show a sequence of MIPS instructions corresponding to: clear2(int *array, int size) { int *p; for (p = &array[0]; p < &array[size]; p = p + 1) *p = 0; }

Example: Pointer and Array clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) { int *p; for (p = &array[0]; p < &array[size]; p = p + 1) *p = 0; } addi $t0,$zero 0 # i = 0 loop1: sll $t1,$t0,2 # $t1 = i * 4 add $t2,$a0,$t1 # $t2 = # &array[i] sw $zero, 0($t2) # array[i] = 0 addi $t0,$t0,1 # i = i + 1 slt $t3,$t0,$a1 # $t3 = # (i < size) bne $t3,$zero,loop1 # if (…) # goto loop1 addi $t0,$a0 0 # p = & array[0] sll $t1,$a1,2 # $t1 = size * 4 add $t2,$a0,$t1 # $t2 = # &array[size] loop2: sw $zero,0($t0) # Memory[p] = 0 addi $t0,$t0,4 # p = p + 4 slt $t3,$t0,$t2 # $t3 = #(p<&array[size]) bne $t3,$zero,loop2 # if (…) # goto loop2

Example Show MIPS instructions corresponding to: void swap(int v[], int k, int j) { int temp; temp = v [k]; v[k] = v [j]; v[j] = temp; } Show MIPS instructions corresponding to: void swap(int *p) { int temp; temp = *p; *p = *(p+1); *(p+1) = temp; }

Bitwise Operations Up until now, we’ve done arithmetic ( add, sub, addi ), memory access ( lw and sw ), and branches and jumps All of these instructions view contents of register as a single quantity (such as an integer) New Perspective: View register as 32 raw bits rather than as a single 32-bit number Since registers are composed of 32 bits, we may want to access individual bits (or groups of bits) rather than the whole Introduce two new classes of instructions –Logical & Shift Ops

Logical Operators Two basic logical operators –AND: outputs 1 only if both inputs are 1 –OR: outputs 1 if at least one input is 1 Truth Table: standard table listing all possible combinations of inputs and resultant output for each. E.g., ABA AND BA OR B

Logical Operators Instruction Names: – and, or : Both of these expect the third argument to be a register – andi, ori : Both of these expect the third argument to be an immediate MIPS Logical Operators are all bitwise, meaning that bit 0 of the output is produced by the respective bit 0’s of the inputs, bit 1 by the bit 1’s, etc. –C: Bitwise AND is & (e.g., z = x & y; ) –C: Bitwise OR is | (e.g., z = x | y; )

Uses for Logical Operators Note that and ing a bit with 0 produces a 0 at the output while and ing a bit with 1 produces the original bit This can be used to create a mask –Example: –The result of and ing these: mask: mask last 12 bits

Uses for Logical Operators The second bitstring in the example is called a mask. It is used to isolate the rightmost 12 bits of the first bitstring by masking out the rest of the string (e.g. setting it to all 0s) Thus, the and operator can be used to set certain portions of a bitstring to 0s, while leaving the rest alone –In particular, if the first bitstring in the above example were in $t0, then the following instruction would mask it: andi $t0,$t0,0xFFF

Uses for Logical Operators Similarly, note that or ing a bit with 1 produces a 1 at the output while or ing a bit with 0 produces the original bit This can be used to force certain bits of a string to 1s –For example, if $t0 contains 0x , then after this instruction: ori$t0, $t0, 0xFFFF –… $t0 contains 0x1234FFFF (e.g. the high-order 16 bits are untouched, while the low-order 16 bits are forced to 1s)

Shift Instructions (review) Move (shift) all the bits in a word to the left or right by a number of bits –Example: shift right by 8 bits –Example: shift left by 8 bits

Shift Instructions Since shifting may be faster than multiplication, a good compiler usually notices when C code multiplies by a power of 2 and compiles it to a shift instruction: a *= 8; (in C) would compile to: sll $s0,$s0,3 (in MIPS) Likewise, shift right to divide by powers of 2

Uses for Shift Instructions Suppose we want to isolate byte 0 (rightmost 8 bits) of a word in $t0. Simply use: andi $t0,$t0,0xFF Suppose we want to isolate byte 1 (bit 15 to bit 8) of a word in $t0. We can use: andi $t0,$t0,0xFF00 but then we still need to shift to the right by 8 bits...

Uses for Shift Instructions Could use instead: sll $t0,$t0,16 srl $t0,$t0,