CDA 3101 Spring 2016 Introduction to Computer Organization

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

The University of Adelaide, School of Computer Science
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
COMP3221 lec18-pointers.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 18 : Pointers & Arrays in C/ Assembly
Lecture 8 Sept 23 Completion of Ch 2 translating procedure into MIPS role of compilers, assemblers, linking, loading etc. pitfalls, conclusions Chapter.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
Computer Architecture CSCE 350
MIPS Function Continued
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
CS3350B Computer Architecture Winter 2015 Lecture 4
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
Cs 61C L8 Proc II., Arrays and Pointers in C 1 Patterson Fall 00 ©UCB CS61C - Machine Structures Lecture 8 - Procedure Conventions part II, plus, Arrays.
Cs 61C L6 Instruction, Proc II.1 Patterson Spring 99 ©UCB CS61C Instruction Representation and Functions, Part II Lecture 6 February 5, 1999 Dave Patterson.
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Intro to Computer Architecture
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
RISC Concepts, MIPS ISA and the Mini–MIPS project
ELEC2041 lec18-pointers.1 Saeid Nooshabadi COMP3221/9221 Microprocessors and Interfacing Lectures 7 : Pointers & Arrays in C/ Assembly
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
CDA 3101 Fall 2013 Introduction to Computer Organization Pointers & Arrays MIPS Programs 16 September 2013.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Character Data and 32-bit Constants (Lecture #20) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Lecture 4: MIPS Instruction Set
Chapter 2 CSF 2009 The MIPS Assembly Language. Stored Program Computers Instructions represented in binary, just like data Instructions and data stored.
1 CS61C L6 Machine Rep CENG 311 Procedure Conventions & The Stack.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
Chapter 2 Instructions: Language of the Computer.
Function Calls in Assembly MIPS R3000 Language (extensive use of stack) Updated 7/11/2013.
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
Lecture 5: Procedure Calls
Computer Architecture & Operations I
Prof. Hsien-Hsin Sean Lee
Lecture 6: Assembly Programs
Computer Architecture Instruction Set Architecture
Lecture 4: MIPS Instruction Set
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Procedures (Functions)
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
MIPS Instructions.
ECE232: Hardware Organization and Design
The University of Adelaide, School of Computer Science
Instruction encoding The ISA defines Format = Encoding
Lecture 5: Procedure Calls
Computer Organization and Design Assembly & Compilation
Computer Instructions
Lecture 6: Assembly Programs
April 2006 Saeid Nooshabadi
Computer Architecture
9/27: Lecture Topics Memory Data transfer instructions
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

CDA 3101 Spring 2016 Introduction to Computer Organization Data Types and Addressing 28 January 2016

Topics to Review Memory layout Procedure conventions Text, data (static and heap), and the stack Procedure conventions Procedure call bookkeeping Caller Saved Registers: Return address $ra Arguments $a0, $a1, $a2, $a3 Return value $v0, $v1 $t Registers $t0 - $t9 Callee Saved Registers: $s Registers $s0 - $s7 Procedure structure Prologue: allocate frame, save registers, assign locals Body: procedure code Epilogue: restore registers, free frame

Overview Data types MIPS data types Support for bytes and strings Application / HLL requirements Hardware support (data and instructions) MIPS data types Support for bytes and strings Addressing Modes Data Instructions Large constants and far target addresses SPIM code

Data Types Applications / HLL Hardware support Integer Floating point Character String Date Currency Text, Objects (ADT) Blob double precision Signed, unsigned Hardware support Numeric data types Integers 8 / 16 / 32 / 64 bits Signed or unsigned Binary coded decimal (COBOL, Y2K!) Floating point 32 / 64 /128 bits Nonnumeric data types Characters Strings Boolean (bit maps) Pointers

MIPS Data Types (1/2) Basic machine data type: 32-bit word (4 bytes) 0100 0011 0100 1001 0101 0011 0100 0101 Integers (signed or unsigned) 1,128,878,917 Floating point numbers 201.32421875 4 ASCII characters C I S E Memory addresses (pointers) 0x43495345 Instructions

MIPS Data Types (2/2) 16-bit constants (immediates) addi $s0, $s1, 0x8020 lw $t0, 20($s0) Half word (16 bits) lh (lhu): load half word lh $t0, 20($s0) sh: save half word sh $t0, 20($s0) Byte (8 bits) lb (lbu): load byte lb $t0, 20($s0) sb: save byte sb $t0, 20($s0)

Byte Instructions lb $s1, 4($s0) 0x10000000 0xFFFFFFAA lbu $s1, 2($s0) Address Memory Bytes 0x10000000 $s0: 0x10000000 $s1: 0xFFFFFFAA lbu $s1, 2($s0) 10101010 $s0: 0x10000000 $s1: 0x000000 AA

String Manipulation C convention: Void strcpy (char[], char y[]) { int i; i = 0; while ((x[i]=y[i]) != 0) i = i + 1; } strcpy: subi $sp, $sp, 4 sw $s0, 0($sp) add $s0, $zero, $zero L1: add $t1, $a1, $s0 lb $t2, 0($t1) add $t3, $a0, $s0 sb $t2, 0($t3) beq $t2, $zero, L2 addi $s0, $s0, 1 j L1 L2: lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra C convention: Null byte (00000000) represents end of the string Importance of comments in MIPS!

Constants Small constants are used frequently (50% of operands) e.g., A = A + 5; Solutions Put 'typical constants' in memory and load them. Create hard-wired registers (like $zero) for constants like 1. MIPS Instructions: slti $8, $18, 10 andi $29, $29, 6 ori $29, $29, 0x4a addi $29, $29, 4 8 4 29 101011 0000 0000 0011 0100 10011 01000

Large Constants To load a 32 bit constant into a register: Load (16) higher order bits lui $t0, 1010101010101010 Then must get the lower order bits right, i.e., ori $t0, $t0, 1010101010101010 1010 1010 1010 1010 0000 0000 0000 0000 $t0: 1010 1010 1010 1010 0000 0000 0000 0000 0000 0000 0000 0000 1010 1010 1010 1010 ori 1010 1010 1010 1010 1010 1010 1010 1010

Addressing Modes Addresses for data and instructions Data (operands and results) Registers Memory locations Constants Efficient encoding of addresses (space: 32 bits) Registers (32) => 5 bits to encode address Destructive instructions: reg2 = reg2 + reg1 Accumulator Stack Orthogonality of opcodes and addressing modes

Data Addressing Modes Register addressing Base addressing The most common (fastest and shortest) add $3, $2, $1 Base addressing Operand is at a memory location with offset lw $t0, 20 ($t1) Immediate addressing Operand is a small constant within the instruction addi $t0, $t1, 4 (signed 16-bit integer)

MIPS Addressing Modes 1 . I m m e d i a t e a d d r e s s i n g Hint: This will be on a Homework and at least one exam… o p r s r t I m m e d i a t e 2 . R e g i s t e r a d d r e s s i n g o p r s r t r d . . . f u n c t R e g i s t e r s R e g i s t e r 3 . B a s e a d d r e s s i n g o p r s r t A d d r e s s M e m o r y R e g i s t e r + B y t e H a l f w o r d W o r d 4 . P C - r e l a t i v e a d d r e s s i n g o p r s r t A d d r e s s * 4 M e m o r y P C + W o r d 5 . P s e u d o d i r e c t a d d r e s s i n g o p A d d r e s s * 4 M e m o r y P C W o r d

Instruction Addressing Modes Addresses are 32 bits long Special purpose register PC (program counter) stores the address of the current instruction PC-relative addressing (branches) Address: PC + (constant in the instruction) * 4 beq $t0, $t1, 20 (0x15090005) Pseudodirect addressing (jumps) Address: PC[31:28] : (constant in the instruction) * 4

SPIM Code PC MIPS machine code Pseudo MIPS add $9, $10, $11 (0x014b4820) main: add $t1, $t2, $t3 j 0x00400048 [exit] (0x08100012) j exit addi $9, $10, -50 (0x2149ffce) addi $t1, $t2, -50 lw $8, 5($9) (0x8d280005) lw $t0, 5($t1) lw $8, -5($9) (0x8d28fffb) lw $t0, -5($t1) bne $8, $9, 20 [exit-PC] (0x15090005) bne $t0, $t1, exit addi $9, $10, 50 (0x21490032) addi $t1, $t2, 50 bne $8, $9, -28 [main-PC] (0x1509fff9) bne $t0, $t1, main lb $8, -5($9) (0x8128fffb) lb $t0, -5($t1) j 0x00400020 [main] (0x08100008) j main add $9, $10, $11 (0x014b4820) exit: add $t1, $t2, $t3 main [0x00400020] [0x00400024] [0x00400028] [0x0040002c] [0x00400030] [0x00400034] [0x00400038] [0x0040003c] [0x00400040] [0x00400044] [0x00400048] exit

Far Target Address Text Segment (252MB) PC beq $s0, $s1, L1 (0x07fe0000) -217 PC (0x08000000) beq $s0, $s1, L1 +217 (0x08020000) bne $s0, $s1, L2 j L1 L2: (0x08200000) L1: 0x10000000

Overview Pointers (addresses) and values Argument passing Storage lifetime and scope Pointer arithmetic Pointers and arrays Pointers in MIPS

Pointers Pointer: a variable that contains the address of another variable HLL version of machine language memory address Why use Pointers? Sometimes only way to express computation Often more compact and efficient code Why not? Huge source of bugs in real software, perhaps the largest single source 1) Dangling reference (premature free) 2) Memory leaks (tardy free): can't have long-running jobs without periodic restart of them

C Pointer Operators Suppose c has value 100, it is located in memory at address 0x10000000 Unary operator & gives address: p = &c; gives address of c to p; p “points to” c (p == 0x10000000) (Referencing) Unary operator * gives value that pointer points to if p = &c => * p == 100 (Dereferencing a pointer) Deferencing  data transfer in assembler ... = ... *p ...;  load (get value from location pointed to by p) *p = ...;  store (put value into location pointed to by p)

Pointer Arithmetic int x = 1, y = 2; /* x and y are integer variables */ int z[10]; /* an array of 10 ints, z points to start */ int *p; /* p is a pointer to an int */ x = 21; /* assigns x the new value 21 */ z[0] = 2; z[1] = 3 /* assigns 2 to the first, 3 to the next */ p = &z[0]; /* p refers to the first element of z */ p = z; /* same thing; p[ i ] == z[ i ]*/ p = p+1; /* now it points to the next element, z[1] */ p++; /* now it points to the one after that, z[2] */ *p = 4; /* assigns 4 to there, z[2] == 4*/ p = 3; /* bad idea! Absolute address!!! */ p = &x; /* p points to x, *p == 21 */ z = &y illegal!!!!! array name is not a variable p: 4 z[1] 2 3 z[0] y: 2 x: 2 1

Assembly Code c is int, has value 100, in memory at address 0x10000000, p in $a0, x in $s0 p = &c; /* p gets 0x10000000*/ lui $a0,0x1000 # p = 0x10000000 x = *p; /* x gets 100 */ lw $s0, 0($a0) # dereferencing p *p = 200; /* c gets 200 */ addi $t0,$0,200 sw $t0, 0($a0) # dereferencing p

Example int strlen(char *s) { char *p = s; /* p points to chars */ while (*p != ’\0’) p++; /* points to next char */ return p - s; /* end - start */ } mov $t0,$a0 lbu $t1,0($t0) /* derefence p */ beq $t1,$zero, Exit Loop:addi $t0,$t0,1 /* p++ */ lbu $t1,0($t0) /* derefence p */ bne $t1,$zero, Loop Exit: sub $v0,$t0,$a0 jr $ra

Argument Passing Options 2 choices “Call by Value”: pass a copy of the item to the function/procedure “Call by Reference”: pass a pointer to the item to the function/procedure Single word variables passed by value Passing an array? e.g., a[100] Pascal (call by value) copies 100 words of a[] onto the stack C (call by reference) passes a pointer (1 word) to the array a[] in a register

Lifetime of Storage and Scope Automatic (stack allocated) Typical local variables of a function Created upon call, released upon return Scope is the function Heap allocated Created upon malloc, released upon free Referenced via pointers External / static Exist for entire program Code Static Heap Stack

Arrays, Pointers, and Functions 4 versions of array function that adds two arrays and puts sum in a third array (sumarray) Third array is passed to function Using a local array (on stack) for result and passing a pointer to it Third array is allocated on heap Third array is declared static Purpose of example is to show interaction of C statements, pointers, and memory allocation

Version 1 C calling convention means: int x[100], y[100], z[100]; sumarray(x, y, z); C calling convention means: sumarray(&x[0], &y[0], &z[0]); Really passing pointers to arrays addi $a0,$gp,0 # x[0] starts at $gp addi $a1,$gp,400 # y[0] above x[100] addi $a2,$gp,800 # z[0] above y[100] jal sumarray

Version 1: Compiled Code void sumarray(int a[], int b[], int c[]) { int i; for(i = 0; i < 100; i = i + 1) c[i] = a[i] + b[i]; } addi $t0,$a0,400 # beyond end of a[] Loop: beq $a0,$t0,Exit lw $t1, 0($a0) # $t1=a[i] lw $t2, 0($a1) # $t2=b[i] add $t1,$t1,$t2 # $t1=a[i] + b[i] sw $t1, 0($a2) # c[i]=a[i] + b[i] addi $a0,$a0,4 # $a0++ addi $a1,$a1,4 # $a1++ addi $a2,$a2,4 # $a2++ j Loop Exit: jr $ra

Version 2 int *sumarray(int a[],int b[]) { int i, c[100]; for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c; } addi $t0,$a0,400 # beyond end of a[] addi $sp,$sp,-400 # space for c addi $t3,$sp,0 # ptr for c addi $v0,$t3,0 # $v0 = &c[0] Loop: beq $a0,$t0,Exit lw $t1, 0($a0) # $t1=a[i] lw $t2, 0($a1) # $t2=b[i] add $t1,$t1,$t2 # $t1=a[i] + b[i] sw $t1, 0($t3) # c[i]=a[i] + b[i] addi $a0,$a0,4 # $a0++ addi $a1,$a1,4 # $a1++ addi $t3,$t3,4 # $t3++ j Loop Exit: addi $sp,$sp, 400 # pop stack jr $ra $sp c[100] a[100] B[100]

Version 3 Code Static Heap Not reused unless freed Stack c[100] int * sumarray(int a[],int b[]) { int i; int *c; c = (int *) malloc(100); for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c; } Code Static c[100] Heap Not reused unless freed Can lead to memory leaks Java, Scheme have garbage collectors to reclaim free space Stack

Version 3: Compiled Code addi $t0,$a0,400 # beyond end of a[] addi $sp,$sp,-12 # space for regs sw $ra, 0($sp) # save $ra sw $a0, 4($sp) # save 1st arg. sw $a1, 8($sp) # save 2nd arg. addi $a0,$zero,400 jal malloc addi $t3,$v0,0 # ptr for c lw $a0, 4($sp) # restore 1st arg. lw $a1, 8($sp) # restore 2nd arg. Loop: beq $a0,$t0,Exit ... (loop as before on prior slide ) j Loop Exit:lw $ra, 0($sp) # restore $ra addi $sp, $sp, 12 # pop stack jr $ra

Version 4 Code Static Heap int * sumarray(int a[],int b[]) { int i; static int c[100]; for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c; } Code Static c[100] Heap Compiler allocates once for function, space is reused Will be changed next time sumarray invoked Why describe? used in C libraries Stack

Review

Review

Conclusions Data can be anything Datatyping restricts data representations Applications restrict datatyping MIPS Datatypes: Number, String, Boolean Addressing: Pointers, Values Many addressing modes (direct, indirect,…) Memory-based address storage (jr instruction) Arrays: big chunks of memory Pointers versus stack storage Be careful of memory leaks!

THINK Weekend!!