Discussion 4 Richard Guo SLAB, Buddy, Garbage Collection, and Intro to MIPS 01/30/09

Similar documents
Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2)

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

Numbers: positional notation. CS61C Machine Structures. Faux Midterm Review Jaein Jeong Cheng Tien Ee. www-inst.eecs.berkeley.

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer

Lecture 5: Procedure Calls

ECE 2035 Programming HW/SW Systems Fall problems, 7 pages Exam Two 23 October 2013

Lecture 4: MIPS Instruction Set

Reduced Instruction Set Computer (RISC)

ENCM 369 Winter 2013: Reference Material for Midterm #2 page 1 of 5

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

ECE 30 Introduction to Computer Engineering

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam Two 23 October Your Name (please print clearly) Signed.

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

We will study the MIPS assembly language as an exemplar of the concept.

CS 61c: Great Ideas in Computer Architecture

Reduced Instruction Set Computer (RISC)

Procedure Calling. Procedure Calling. Register Usage. 25 September CSE2021 Computer Organization

Computer Architecture I Midterm I

Run time environment of a MIPS program

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam Two 11 March Your Name (please print) total

COE608: Computer Organization and Architecture

Instructions: Language of the Computer

EE108B Lecture 2 MIPS Assembly Language I. Christos Kozyrakis Stanford University

Lecture 5: Procedure Calls

ECE232: Hardware Organization and Design. Computer Organization - Previously covered

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

Thomas Polzer Institut für Technische Informatik

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam Three 10 April 2013

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Lecture #6 Intro MIPS; Load & Store

CS61C : Machine Structures

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

Computer Architecture. MIPS Instruction Set Architecture

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

COMPUTER ORGANIZATION AND DESIGN

CS3350B Computer Architecture MIPS Introduction

Discussion 3 Richard Guo Advanced C 01/28/09

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types

ECE 331 Hardware Organization and Design. Professor Jay Taneja UMass ECE - Discussion 3 2/8/2018

Brought to you by CalLUG (UC Berkeley GNU/Linux User Group). Tuesday, September 20, 6-8 PM in 100 GPB.

Lecture #6 Intro MIPS; Load & Store Assembly Variables: Registers (1/4) Review. Unlike HLL like C or Java, assembly cannot use variables

Architecture II. Computer Systems Laboratory Sungkyunkwan University

CS3350B Computer Architecture MIPS Procedures and Compilation

EE108B Lecture 3. MIPS Assembly Language II

MIPS Assembly Programming

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam Two 21 October 2016

The MIPS Instruction Set Architecture

Concocting an Instruction Set

Programming the processor

ECE 2035 A Programming Hw/Sw Systems Fall problems, 8 pages Final Exam 9 December 2015

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

Topic Notes: MIPS Instruction Set Architecture

CS 61c: Great Ideas in Computer Architecture

Chapter 3. Instructions:

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

101 Assembly. ENGR 3410 Computer Architecture Mark L. Chang Fall 2009

EN2910A: Advanced Computer Architecture Topic 02: Review of classical concepts

Lecture 7: Procedures

CS61C : Machine Structures

CS 110 Computer Architecture Lecture 5: Intro to Assembly Language, MIPS Intro

Chapter 1. Computer Abstractions and Technology. Lesson 3: Understanding Performance

MIPS History. ISA MIPS Registers

CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Formats

Chapter 2. Instructions:

ECE 2035 A Programming Hw/Sw Systems Fall problems, 8 pages Final Exam 8 December 2014

CS61C : Machine Structures

Computer Architecture. The Language of the Machine

ECE/CS 314 Fall 2003 Homework 2 Solutions. Question 1

Review of instruction set architectures

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

Instruction Set Architecture. "Speaking with the computer"

Inequalities in MIPS (2/4) Inequalities in MIPS (1/4) Inequalities in MIPS (4/4) Inequalities in MIPS (3/4) UCB CS61C : Machine Structures

MIPS Instruction Set Architecture (2)

CSEE W3827 Fundamentals of Computer Systems Homework Assignment 3 Solutions

Instructions: MIPS arithmetic. MIPS arithmetic. Chapter 3 : MIPS Downloaded from:

CS3350B Computer Architecture

Motivation for Dynamic Memory. Dynamic Memory Allocation. Stack Organization. Stack Discussion. Questions answered in this lecture:

ECE 2035 A Programming Hw/Sw Systems Fall problems, 10 pages Final Exam 14 December 2016

MACHINE LANGUAGE. To work with the machine, we need a translator.

ECE 2035 A Programming Hw/Sw Systems Spring problems, 8 pages Final Exam 29 April 2015

Course Administration

Compiling Techniques

ECE 2035 Programming Hw/Sw Systems Fall problems, 10 pages Final Exam 9 December 2013

Chapter 2. Instructions: Language of the Computer. HW#1: 1.3 all, 1.4 all, 1.6.1, , , , , and Due date: one week.

Machine Instructions - II. Hwansoo Han

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

Chapter 3 MIPS Assembly Language. Ó1998 Morgan Kaufmann Publishers 1

ECE 15B Computer Organization Spring 2010

CS222: MIPS Instruction Set

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

ECE 154A Introduction to. Fall 2012

UCB CS61C : Machine Structures

Chapter 2. Instructions: Language of the Computer

A General-Purpose Computer The von Neumann Model. Concocting an Instruction Set. Meaning of an Instruction. Anatomy of an Instruction

Chapter 2. Instruction Set Architecture (ISA)

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Transcription:

Discussion 4 Richard Guo SLAB, Buddy, Garbage Collection, and Intro to MIPS 01/30/09 1. Answers to Last Time's Problems #include <stdio.h> #include <stdlib.h> struct node int value; // or char or whatever storage you want struct node * next; }; typedef struct node nodeptr; int main() int i=0; nodeptr* a = (nodeptr*) malloc (sizeof(nodeptr)); nodeptr* b = (nodeptr*) malloc (sizeof(nodeptr)); nodeptr* walker; a->value = 0; b->value =3; a->next = b; b->next = NULL; for(walker = a; walker!= NULL; walker = walker->next) printf ("%d\n", walker->value); } return 0; 2. SLAB Allocator and Buddy System To avoid external fragmentation due to small chunks of memory lying free between bigger slices of allocated space, the SLAB allocator preallocates memory into small predefined sizes (16kb, 32kb, etc.). The size of the slab is also a tweakable parameter which specifies the largest memory request that uses SLAB; all larger cases use the regular best fit/first fit/next fit method we've seen. Whenever a request comes that is small enough to be handled by SLAB, an entire chunk is marked as used and returned, even if it's bigger than what was requested. This causes internal fragmentation, or waste of space within a chunk, but allows for faster and more efficient use and mallocing of small spaces. The buddy system is similar to SLAB in that small amounts of memory are pre-allocated, except that they are marked so that certain contiguous same-sized blocks can be joined or broken to form larger or smaller chunks depending on demand. If a free chunk just bigger than (to the closest power of 2) the requested amount is found, that chunk is returned. However, if there is nothing available, a larger chunk can be split into smaller ones via the buddy system. This allows even more efficient usage than SLAB but takes longer because checks must be done to join/split buddies.

3. Garbage Collection C does not support garbage collection because it is weakly typed. This means that though variables have types, they can be interpreted bitwise as something different. For example, int, float, and char * are all 4 bytes wide in a 32 bit system. How does the system know (when blindly trekking through memory) whether the 4 bytes it encounters is a pointer? With no way to distinguish, there can be no garbage collection in C. In Java and interpreted languages, there are 3 main methods to do garbage collection: Method Description Problems Reference Counting Mark and Sweep Copying GC Have a few bits attached to each malloc that stores how many things point to it. Increment and decrement those bits accordingly when pointers are changed or set. When the bits reach 0 for an allocated chunk, free it. Have a tag bit for every section of allocated memory. Go through all the pointers and the nodes they connect. Tag/mark everything that's accessible. Free the rest. Use half of memory. When one side gets fully, pick up the reachable stuff and move to other half. Compact to remove fragmentation. Can generate massive waits when removing the head of a linked list. Doesn't deal with doubly linked lists. Very slow! Program stalls to clean tagged memory all at once. Can only use half of memory! Has very bad performance in edge cases (e.g. almost full). Java and C# use a hybrid technique that combines elements of the above with Generational GC. 5. Intro to MIPS MIPS (Microprocessor without Interlocked Pipeline Stages) is a CPU architecture that was pioneered back in the early 80s @ Stanford. It went against the dogma at the time of complex computer architectures (CISC) that had more and more exotic instructions. x86 is a good example of MIPS's nemesis it became bloated over time as more and more extensions were added (SSE, SSE2, MMX, 3D Now!, etc.). From the name, you can tell that interlocked pipelines was the colossal beast of the time, which MIPS desperately wanted to slay. MIPS wanted a clean and simple instruction set architecture (ISA) that was easy to implement in hardware, reasoning that the transistors saved in implementing logic can be used on more execution resource and cache. As testament to the degree of its success, CPUs today are predominantly made with RISC cores. MIPS Registers The MIPS design has 32 registers, each of which can store 32 bites of information. Registers are like hands for the CPU (and are physically located on-die), which can only work with the data stored in registers. Everything else must be stashed in memory and retrieved as required. The reason for this is that instructions are conveniently also 32 bits wide. A summary of all 32 registers is given below:

Name Number Function $zero $0 Defines a constant zero $at $1 Used by system $v0-$v1 $2 $3 Function returns and expression evaluations $a0-$a3 $4 $7 Function arguments $t0-$t7 $8 $15 Temporary storage (can change after system call) $s0-$s7 $t8-$t9 $k0-$k1 $16 $23 Long-term static storage (guaranteed not to change after a system/function call) $24 $25 More temporary storage $26 $27 Reserved $gp $28 Global pointer reserved $sp $29 Stack pointer don't modify $fp $30 Frame pointer don't modify $ra $31 Return address Most of your work will be dealing with $t0-$t9, $s0-$s7, and $sp. MIPS instructions The format of most instructions is: name $destination, $source1, $source2 Some basic instructions you should have covered in lecture: Instruction Type Function Notes add $dest, $source1, $source2 R $dest = $source1 + $source2 addi $dest, $source1, CONST I $dest = $source1 + CONST CONST can be negative sub $dest, $source1, $source2 R $dest = $source1 - $source2 subu $dest, $source1, $source2 R $dest = $source1 - (unsigned int) $source2 addu $dest, $source1, $source2 R $dest = $source1 + (unsigned int) $source2 addiu $dest, $source1, CONST I $dest = $source1 + (unsigned int) CONST $source2 is unsigned $source2 is unsigned CONST is always pos lw $dest, OFFSET($addr) I $dest = (int) *($addr+offset) Must be word aligned sw $source, OFFSET($addr) I *($addr+offset) = (int)$dest Must be word aligned lb $dest, OFFSET($addr) I $dest = (char) *($addr+offset) Sign extended lbu $dest, OFFSET($addr) I $dest = (unsigned char) *($addr+offset) Not sign extended sb $source, OFFSET($addr) I *($addr+offset) = (char)$dest Sign extended

beq $reg1, $reg2, LABEL I if ($reg1 == $reg2) goto LABEL bne $reg1, $reg2, LABEL I if ($reg1!= $reg2) goto LABEL j LABEL J goto LABEL There is no subiu instruction. How can you imitate it by using others? 6. Solved Problems Translate the following C into MIPS: // assume ptr is the address of a0[4] // num $s0, num2 $s1, dest $s2 int num = *(ptr); char num2 = (char) (ptr[8] >> 24); if (num == num2) dest = num + num2; else dest = num - num2; lw $s0, 16($a0) lbu $s1, 48($a0) beq $s0, $s1, equal subu $s2, $s0, $s1 j end equal: addu $s2, $s0, $s1 end: // Nth_Fibonacci(N) // N $s0, fib $s1 // i $t0, j $t1 if(n==0) return 0; else if(n==1) return 1; N =2; int fib=1, i=1, j=1; while(n!=0) fib = i+j; j = i; zero: bne $s0 $0 one addi $s1 $0 0 j done one: addi $s1 $0 1 bne $s0 $s1 init j done init: addi $s0 $s0-2 addi $t0 $0 1 addi $t1 $0 1 loop: beq $s0 $0 done add $s1 $t0 $t1 addi $t1 $t0 0

i = fib; N ; } return fib; done: addi $t0 $s1 0 subi $s0 $s0 1 j loop