CS61C : Machine Structures

Similar documents
Review C program: foo.c Compiler Assembly program: foo.s Assembler Object(mach lang module): foo.o

Where Are We Now? Linker (1/3) Linker (2/3) Four Types of Addresses we ll discuss. Linker (3/3)

Assembler. #13 Running a Program II

CS61C : Machine Structures

CS 61C: Great Ideas in Computer Architecture CALL continued ( Linking and Loading)

Review C program: foo.c Compiler Assembly program: foo.s Assembler Object(mach lang module): foo.o

From Code to Program: CALL Con'nued (Linking, and Loading)

Review (1/2) IEEE 754 Floating Point Standard: Kahan pack as much in as could get away with. CS61C - Machine Structures

CS 110 Computer Architecture Running a Program - CALL (Compiling, Assembling, Linking, and Loading)

Lecture 10: Program Development versus Execution Environment

I-Format Instructions (3/4) Define fields of the following number of bits each: = 32 bits

CS 61C: Great Ideas in Computer Architecture Running a Program - CALL (Compiling, Assembling, Linking, and Loading)

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

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

ECE 15B COMPUTER ORGANIZATION

Review. Disassembly is simple and starts by decoding opcode field. Lecture #13 Compiling, Assembly, Linking, Loader I

UCB CS61C : Machine Structures

CS 61c: Great Ideas in Computer Architecture

CS61C : Machine Structures

CS 61C: Great Ideas in Computer Architecture. (Brief) Review Lecture

CS61C : Machine Structures

CS 61c: Great Ideas in Computer Architecture

CS61C : Machine Structures

CS61C : Machine Structures

Administrivia. Translation. Interpretation. Compiler. Steps to Starting a Program (translation)

UCB CS61C : Machine Structures

CS61C : Machine Structures

Review! Lecture 5 C Memory Management !

Orange Coast College. Business Division. Computer Science Department CS 116- Computer Architecture. The Instructions

CS61C : Machine Structures

Administrivia. Midterm Exam - You get to bring. What you don t need to bring. Conflicts? DSP accomodations? Head TA

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

Lecture #6 Intro MIPS; Load & Store

UCB CS61C : Machine Structures

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

CS61C : Machine Structures

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

EEM 486: Computer Architecture. Lecture 2. MIPS Instruction Set Architecture

Lectures 13 & 14. memory management

CS61C L10 MIPS Instruction Representation II, Floating Point I (6)

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

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

CS61C : Machine Structures

COMPUTER ORGANIZATION AND DESIGN

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

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

UCB CS61C : Machine Structures

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

CS61C - Machine Structures. Lecture 6 - Instruction Representation. September 15, 2000 David Patterson.

CS61C : Machine Structures

MIPS Instruction Set Architecture (2)

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

xx.yyyy Lecture #11 Floating Point II Summary (single precision): Precision and Accuracy Fractional Powers of 2 Representation of Fractions

CS61C : Machine Structures

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

CENG3420 Lecture 03 Review

UCB CS61C : Machine Structures

Review of Last Lecture. CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Representation II. Agenda. Dealing With Large Immediates

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

CS 61c: Great Ideas in Computer Architecture

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

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

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

Architecture II. Computer Systems Laboratory Sungkyunkwan University

UCB CS61C : GREAT IDEAS IN COMPUTER ARCHITECTURE

ECE331: Hardware Organization and Design

Reduced Instruction Set Computer (RISC)

CS 110 Computer Architecture MIPS Instruction Formats

Computer Architecture. The Language of the Machine

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output

Agenda. Strings: C vs. Java. Loading, Storing bytes. Support for Characters and Strings 6/29/2011

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

CS61C : Machine Structures

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

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

Thomas Polzer Institut für Technische Informatik

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

UC Berkeley CS61C : Machine Structures

EEM 486: Computer Architecture. Lecture 2. MIPS Instruction Set Architecture

CS61C : Machine Structures

Lecture 7: Procedures and Program Execution Preview

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

Reduced Instruction Set Computer (RISC)

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

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

Review. Lecture 18 Running a Program I aka Compiling, Assembling, Linking, Loading

CS61C : Machine Structures

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

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

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions

MIPS (SPIM) Assembler Syntax

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

CS61C : Machine Structures

Topic Notes: MIPS Instruction Set Architecture

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

CS 61C: Great Ideas in Computer Architecture Running a Program - CALL (Compiling, Assembling, Linking, and Loading)

Review. Lecture #7 MIPS Decisions So Far...

CS61C : Machine Structures

Transcription:

inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #14 CALL II, 1 st Half review 2008-7-15 Script browsers in C? http://tech.slashdot.org/article.pl?sid=08/07/07/1724236 Albert Chae, Instructor CS61C L14 CALL I, 1 st half rev iew (1) Review Interpretation less efficient, easier to program, debug Translation more efficient, harder to read/program, can protect source Translating C Programs Compiler Assembler Linker Loader CS61C L14 CALL I, 1 st half rev iew (2)

Where Are We Now? CS164 CS61C L14 CALL I, 1 st half rev iew (3) Loader (1/3) Input: Executable Code (e.g., a.out for MIPS) Output: (program is run) Executable files are stored on disk. When one is run, loader s job is to load it into memory and start it running. In reality, loader is the operating system (OS) loading is one of the OS tasks CS61C L14 CALL I, 1 st half rev iew (4)

Loader (2/3) So what does a loader do? Reads executable file s header to determine size of text and data segments Creates new address space for program large enough to hold text and data segments, along with a stack segment Copies instructions and data from executable file into the new address space (this may be anywhere in memory as we ll see later) CS61C L14 CALL I, 1 st half rev iew (5) Loader (3/3) Copies arguments passed to the program onto the stack Initializes machine registers Most registers cleared, but stack pointer assigned address of 1st free stack location Jumps to start-up routine that copies program s arguments from stack to registers and sets the PC If main routine returns, start-up routine terminates program with the exit system call CS61C L14 CALL I, 1 st half rev iew (6)

Example: C Asm Obj Exe Run #include <stdio.h> int main (int argc, char *argv[]) { int i, sum = 0; for (i = 0; i<= 100; i++) sum = sum + i * i; printf ("The sum of sq from 0.. 100 is %d\n", sum); } C Program Source Code: prog.c printf lives in libc CS61C L14 CALL I, 1 st half rev iew (7) Compilation: MAL.text.align 2.globl main main: subu $sp,$sp,32 sw $ra, 20($sp) sd $a0, 32($sp) sw $0, 24($sp) sw $0, 28($sp) loop: lw $t6, 28($sp) mul$t7, $t6,$t6 lw $t8, 24($sp) addu $t9,$t8,$t7 sw $t9, 24($sp) addu $t0, $t6, 1 sw $t0, 28($sp) ble $t0,100, loop la $a0, str lw $a1, 24($sp) jalprintf move $v0, $0 lw $ra, 20($sp) addiu $sp,$sp,32 jr $ra.data Where are.align 0 7 pseudoinstructions? str:.asciiz"the sum of sq from 0.. 100 is %d\n" CS61C L14 CALL I, 1 st half rev iew (8)

Compilation: MAL.text.align 2.globl main main: subu $sp,$sp,32 sw $ra, 20($sp) sd $a0, 32($sp) sw $0, 24($sp) sw $0, 28($sp) loop: lw $t6, 28($sp) mul$t7, $t6,$t6 lw $t8, 24($sp) addu $t9,$t8,$t7 sw $t9, 24($sp) addu $t0, $t6, 1 sw $t0, 28($sp) ble $t0,100, loop la $a0, str lw $a1, 24($sp) jalprintf move $v0, $0 lw $ra, 20($sp) addiu $sp,$sp,32 jr $ra.data 7 pseudoinstructions.align 0 str: underlined.asciiz "The sum of sq from 0.. 100 is %d\n" CS61C L14 CALL I, 1 st half rev iew (9) Assembly step 1: Remove pseudoinstructions, assign addresses 00 addiu $29,$29,-32 04 sw $31,20($29) 08 sw $4, 32($29) 0c sw $5, 36($29) 10 sw $0, 24($29) 14 sw $0, 28($29) 18 lw $14, 28($29) 1c multu $14, $14 20 mflo $15 24 lw $24, 24($29) 28 addu $25,$24,$15 2c sw $25, 24($29) 30 addiu $8,$14, 1 34 sw $8,28($29) 38 slti $1,$8, 101 3c bne $1,$0, loop 40 lui $4, l.str 44 ori $4,$4,r.str 48 lw $5,24($29) 4c jal printf 50 add $2, $0, $0 54 lw $31,20($29) 58 addiu $29,$29,32 5c jr $31 CS61C L14 CALL I, 1 st half rev iew (10)

Assembly step 2 Create relocation table and symbol table Symbol Table Label address (in module) type main: 0x00000000 global text loop: 0x00000018 local text str: 0x00000060 local data Relocation Information Address Dependency CS61C L14 CALL I, 1 st half rev iew (11) Instr. type 0x00000040 lui l.str 0x00000044 ori r.str 0x0000004c jal printf Assembly step 3 Resolve local PC-relative labels 00 addiu$29,$29,-32 04 sw$31,20($29) 08 sw$4, 32($29) 0c sw$5, 36($29) 10 sw$0, 24($29) 14 sw$0, 28($29) 18 lw$14, 28($29) 1c multu$14, $14 20 mflo$15 24 lw$24, 24($29) 28 addu$25,$24,$15 2c sw$25, 24($29) 30 addiu $8,$14, 1 34 sw$8,28($29) 38 slti$1,$8, 101 3c bne$1,$0, -10 40 lui$4, l.str 44 ori$4,$4,r.str 48 lw$5,24($29) 4c jalprintf 50 add$2, $0, $0 54 lw$31,20($29) 58 addiu $29,$29,32 5c jr$31 CS61C L14 CALL I, 1 st half rev iew (12)

Assembly step 4 Generate object (.o) file: Output binary representation for - ext segment (instructions), - data segment (data), - symbol and relocation tables. Using dummy placeholders for unresolved absolute and external references. CS61C L14 CALL I, 1 st half rev iew (13) Text segment in object file 0x000000 00100111101111011111111111100000 0x000004 10101111101111110000000000010100 0x000008 10101111101001000000000000100000 0x00000c 10101111101001010000000000100100 0x000010 10101111101000000000000000011000 0x000014 10101111101000000000000000011100 0x000018 10001111101011100000000000011100 0x00001c 10001111101110000000000000011000 0x000020 00000001110011100000000000011001 0x000024 00100101110010000000000000000001 0x000028 00101001000000010000000001100101 0x00002c 10101111101010000000000000011100 0x000030 00000000000000000111100000010010 0x000034 00000011000011111100100000100001 0x000038 00010100001000001111111111110111 0x00003c 10101111101110010000000000011000 0x000040 00111100000001000000000000000000 0x000044 10001111101001010000000000000000 0x000048 00001100000100000000000011101100 0x00004c 00100100000000000000000000000000 0x000050 10001111101111110000000000010100 0x000054 00100111101111010000000000100000 0x000058 00000011111000000000000000001000 0x00005c 00000000000000000001000000100001 CS61C L14 CALL I, 1 st half rev iew (14)

Link step 1: combine prog.o, libc.o Merge text/data segments Create absolute memory addresses Modify & merge symbol and relocation tables Symbol Table Label Address main: 0x00000000 loop: 0x00000018 str: 0x10000430 printf: 0x000003b0 Relocation Information Address Instr. Type Dependency 0x00000040 luil.str 0x00000044 orir.str 0x0000004c jalprintf CS61C L14 CALL I, 1 st half rev iew (15) Link step 2: Edit Addresses in relocation table (shown in TAL for clarity, but done in binary ) 00 addiu $29,$29,-32 04 sw $31,20($29) 08 sw $4, 32($29) 0c sw $5, 36($29) 10 sw $0, 24($29) 14 sw $0, 28($29) 18 lw $14, 28($29) 1c multu $14, $14 20 mflo $15 24 lw $24, 24($29) 28 addu $25,$24,$15 2c sw $25, 24($29) 30 addiu $8,$14, 1 34 sw $8,28($29) 38 slti $1,$8, 101 3c bne $1,$0, -10 40 lui $4, 4096 44 ori $4,$4,1072 48 lw $5,24($29) 4c jal812 50 add $2, $0, $0 54 lw $31,20($29) 58 addiu $29,$29,32 5c jr $31 CS61C L14 CALL I, 1 st half rev iew (16)

Link step 3: Output executable of merged modules. Single text (instruction) segment Single data segment Header detailing size of each segment NOTE: The preceeding example was a much simplified version of how ELF and other standard formats work, meant only to demonstrate the basic principles. CS61C L14 CALL I, 1 st half rev iew (17) Peer Instruction Which of the following instr. may need to be edited during link phase? Loop: lui $at, 0xABCD }# A ori $a0,$at, 0xFEDC jal add_link # B bne $a0,$v0, Loop # C CS61C L14 CALL I, 1 st half rev iew (18) ABC 1: FFF 2: FFT 3: FTF 4: FTT 5: TFF 6: TFT 7: TTF 8: TTT

Peer Instruction Answer Which of the following instr. may need to be edited during link phase? CS61C L14 CALL I, 1 st half rev iew (19) $a0 just holds a number; OK Loop: lui $at, 0xABCD ori $a0,$at, 0xFEDC jal add_link bne $a0,$v0, Loop subroutine; relocate }# A # B # C PC-relative branch; OK ABC 1: FFF 2: FFT 3: FTF 4: FTT 5: TFF 6: TFT 7: TTF 8: TTT Administrivia Assignments HW3 due 7/16 @ 11:59pm Proj2 due 7/18 @ 11:59pm Grades HW1,2, labs1-6 are up. - If not, contact reader (HWs) or TA (labs) right away. - We will have a grade freeze for these grades TBA. HW0, quizzes1-6 will be posted by Thursday CS61C L14 CALL I, 1 st half rev iew (20)

Administrivia Midterm Midterm Mon 2008-07-21@7-10pm, 155 Dwinelle Bring pencils and eraser! You can bring green sheet and one handwritten double sided note sheet No calculator, laptop, etc. faux midterm: 7/16 @ 6-9pm 10 Evans review session: 7/17 in lecture (will go over faux exam) CS61C L14 CALL I, 1 st half rev iew (21) Things to Remember (1/3) CS61C L14 CALL I, 1 st half rev iew (22)

Things to Remember (2/3) Compiler converts a single HLL file into a single assembly language file. Assembler removes pseudoinstructions, converts what it can to machine language, and creates a checklist for the linker (relocation table). This changes each.s file into a.o file. Does 2 passes to resolve addresses, handling internal forward references Linker combines several.o files and resolves absolute addresses. Enables separate compilation, libraries that need not be compiled, and resolves remaining addresses Loader loads executable into memory and begins execution. CS61C L14 CALL I, 1 st half rev iew (23) Things to Remember 3/3 Stored Program concept mean instructions just like data, so can take data from storage, and keep transforming it until load registers and jump to routine to begin execution Compiler Assembler Linker ( Loader) CS61C L14 CALL I, 1 st half rev iew (24)

1 st Half Review This is your chance to ask questions about anything from the past 4 weeks CS61C L14 CALL I, 1 st half rev iew (25) Anatomy: 5 components of any Computer Computer Processor Control ( brain ) Datapath ( brawn ) Memory (where programs, data live when running) Devices Input Output Keyboard, Mouse Disk (where programs, data live when not running) Display, Printer CS61C L14 CALL I, 1 st half rev iew (26)

Numbers: positional notation A digit s position determines how much value it adds to the whole number. Number Base B B symbols per digit: Base 10 (Decimal): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Base 2 (Binary): 0, 1 Number representation: d 31 d 30... d 1 d 0 is a 32 digit number value = d 31 B 31 + d 30 B 30 +... + d 1 B 1 + d 0 B 0 Binary: 0,1 (In binary, digits called bits ) 0b11010 = 1 2 4 + 1 2 3 + 0 2 2 + 1 2 1 + 0 2 0 = 26 Here 5 digit binary # turns into a 2 digit decimal # Can we find a base that converts to binary easily? CS61C L14 CALL I, 1 st half rev iew (27) Hexadecimal Numbers: Base 16 Hexadecimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F Normal digits + 6 more from the alphabet In C, written as 0x (e.g., 0xFAB5) Conversion: Binary Hex 1 hex digit represents 16 decimal values 4 binary digits represent 16 decimal values Þ1 hex digit replaces 4 binary digits One hex digit is a nibble. Two is a byte CS61C L14 CALL I, 1 st half rev iew (28)

Decimal vs. Hexadecimal vs. Binary Examples: 1010 1100 0011 (binary) = 0xAC3 10111 (binary) = 0001 0111 (binary) = 0x17 0x3F9 = 11 1111 1001 (binary) How do we convert between hex and Decimal? MEMORIZE! CS61C L14 CALL I, 1 st half rev iew (29) 00 0 0000 01 1 0001 02 2 0010 03 3 0011 04 4 0100 05 5 0101 06 6 0110 07 7 0111 08 8 1000 09 9 1001 10 A 1010 11 B 1011 12 C 1100 13 D 1101 14 E 1110 15 F 1111 kibi, mebi, gibi, tebi, pebi, exbi, zebi, yobi en.wikipedia.org/wiki/binary_prefix New IEC Standard Prefixes [only to exbi officially] Name Abbr Factor As of this kibi Ki 2 10 = 1,024 writing, this mebi Mi 2 20 = 1,048,576 proposal has gibi Gi 2 30 = 1,073,741,824 yet to gain tebi Ti 2 40 = 1,099,511,627,776 widespread pebi Pi 2 50 = 1,125,899,906,842,624 use exbi zebi yobi Ei Zi Yi 2 60 = 1,152,921,504,606,846,976 2 70 = 1,180,591,620,717,411,303,424 2 80 = 1,208,925,819,614,629,174,706,176 MEMORIZE! CS61C L14 CALL I, 1 st half rev iew (30)

The way to remember #s What is 2 34? How many bits addresses (i.e., what s ceil log 2 = lg of) 2.5 TiB? Answer! 2 XY means X=0 --- Y=0 1 X=1 kibi ~10 3 Y=1 2 X=2 mebi ~10 6 Y=2 4 X=3 gibi ~10 9 Y=3 8 X=4 tebi ~10 12 Y=4 16 X=5 pebi ~10 15 Y=5 32 X=6 exbi ~10 18 Y=6 64 X=7 zebi ~10 21 Y=7 128 X=8 yobi ~10 24 Y=8 256 Y=9 512 MEMORIZE! CS61C L14 CALL I, 1 st half rev iew (31) What to do with representations of numbers? Just what we do with numbers! Arithmetic, comparisons Use them to represent ANYTHING Characters ASCII, UNICODE Boolean True/False Colors, memory addresses, MIPS instructions With N bits represent/index at most 2 N things With Y things to represent need at least ceil(lg(y)) bits to represent CS61C L14 CALL I, 1 st half rev iew (32)

How to Represent Negative Numbers? Define leftmost bit to be sign! 0 +, 1 sign and magnitude left most bit is sign, rest of number is unsigned value Bad: Arithmetic circuit complicated because adding 1 doesn t always result in bigger number Also, two zeros - 0x00000000 = +0 ten - 0x80000000 = 0 ten CS61C L14 CALL I, 1 st half rev iew (33) Another try: complement the bits Example: 7 10 = 00111 2-7 10 = 11000 2 Called One s Complement Note: positive numbers have leading 0s, negative numbers have leadings 1s. So leftmost bit is still a sign bit 10000... 11110 11111 Bad: there are still 2 zeros - 0x00000000 = +0 ten 00000 00001... 01111-0xFFFFFFFF = -0 ten HW still more complicated than it needs to be CS61C L14 CALL I, 1 st half rev iew (34)

2 s Complement Number wheel : N = 5 00000 11111 00001 2 N-1 nonnegatives 11110-1 0 1 11101-2 2 11100-3 -4... 10001-15 -16 15 00010... 2 N-1 negatives one zero 10000 01111 00000 00001... 01111 10000... 11110 11111 CS61C L14 CALL I, 1 st half rev iew (35) Standard Negative Number Representation Two s Complement Shift one s complement over one to get rid of 2 zeros and make hardware simpler Features of two s complement: To negate: invert all bits and add 1 Sign extension: - Easy to convert n bit number to m bit number, when m > n - Just copy sign bit over to the left If not enough bits to hold number, we get overflow CS61C L14 CALL I, 1 st half rev iew (36)

C Syntax: Variables Declare variables with type and name type varname; e.g. int x; Initialize variables before using them! Can combine initialization with declaration: int x = 5; Declarations can go anywhere (C99) CS61C L14 CALL I, 1 st half rev iew (37) C Syntax: Assignment Use = sign for assignment set! in Scheme The value of an assignment expression is the RHS, while the type is the LHS. e.g. int x, y; x = y = 5; Same as y = 5; x = 5; (not x = y) CS61C L14 CALL I, 1 st half rev iew (38)

C Syntax: True or False? Booleans exist in C99, but it is very common to test any type for its truthiness What evaluates to FALSE in C? 0 (integer) NULL (pointer) What evaluates to TRUE in C? everything else CS61C L14 CALL I, 1 st half rev iew (39) C syntax : flow control Within a function, remarkably close to Java constructs in methods (shows its legacy) in terms of flow control if-else switch while and for do-while Can also use conditional expressions Expressions return VALUES (test)? then : else; CS61C L14 CALL I, 1 st half rev iew (40)

Functions Specify return type If no return type, use void Formal parameters declared after function name Function body goes between { } e.g. int subone(int x) { } return x - 1; CS61C L14 CALL I, 1 st half rev iew (41) C Syntax: main To get the main function to accept arguments, use this: int main (int argc, char *argv[]) What does this mean? argc will contain the number of strings on the command line (the executable counts as one, plus one for each argument). - Example: unix% sort myfile argv is a pointer to an array containing the arguments as strings (more on pointers later). CS61C L14 CALL I, 1 st half rev iew (42)

Pointers Pointer: A variable that contains the address of another variable. Location (address) name... 101 102 103 104 105... 23 42 104... y p x A pointer s type includes the number of * s int *p, **h; - p is type int *, h is type int** CS61C L14 CALL I, 1 st half rev iew (43) Pointers Using pointers & operator: get address of a variable - returns value whose type has one more * * dereference operator : two uses - RHS: get value from memory pointed at - LHS: store value to memory pointed at - * also has one more use, in pointer declarations Pointers let us change nonlocal variables and keep results Gets around C s pass by copy Don t ever return the address of a local variable! CS61C L14 CALL I, 1 st half rev iew (44)

Pointers & Allocation After declaring a pointer: int *ptr; ptr doesn t actually point to anything yet. We can either: make it point to something that already exists using &, or allocate room in memory for something new that it will point to with malloc Don t dereference an uninitialized pointer! CS61C L14 CALL I, 1 st half rev iew (45) Arrays Declaration: int ar[size]; Accessing elements: ar[num]; Arrays are (almost) identical to pointers ar[0] is the same as *ar ar[2] is the same as *(ar+2) They differ in very subtle ways: - Can t increment array variables - Can declare filled arrays - Using sizeof Key Concept: An array variable is like a pointer to the first element. CS61C L14 CALL I, 1 st half rev iew (46)

Pointer Arithmetic Can do arithmetic on memory address to get a new memory address p+1 returns a ptr to the next array elt. Adds 1*sizeof(arrayelt). *p++ vs (*p)++? x = *p++ x = *p ; p = p + 1; x = (*p)++ x = *p ; *p = *p + 1; CS61C L14 CALL I, 1 st half rev iew (47) Pointer Arithmetic So what s valid pointer arithmetic? Add an integer to a pointer. Subtract 2 pointers (in the same array). Compare pointers (<, <=, ==,!=, >, >=) Compare pointer to NULL (indicates that the pointer points to nothing). Everything else is illegal since it makes no sense: adding two pointers multiplying pointers subtract pointer from integer CS61C L14 CALL I, 1 st half rev iew (48)

Improper memory accesses Bus Error Usually from misaligned address - Maybe a freak accident with pointer arithmetic - Maybe dereferencing something that wasn t meant to be a pointer Segmentation Fault When you try to access memory that doesn t belong to you - Going out of bounds in an array - Invalid pointer values Forgot to initialize Malloc ing, freeing, then trying to dereference CS61C L14 CALL I, 1 st half rev iew (49) C Strings A string in C is just an array of characters. char string[] = "abc"; How do you tell how long a string is? Last character is followed by a 0 byte (null terminator, \0 ) strlen counts everything up to and excluding the null byte Make sure to allocate enough space for the null byte! CS61C L14 CALL I, 1 st half rev iew (50)

Pointers to pointers You might want an array of pointers. int **int_p_array; You might want to change a pointer value from within a function void IncrementPtr(int **h) { *h = *h + 1; } CS61C L14 CALL I, 1 st half rev iew (51) Dynamic Memory Allocation To allocate room for something new to point to, use malloc() (with the help of a typecast and sizeof): ptr = (int *) malloc (n*sizeof(int)); Type on LHS should have one more * than type malloc ed. malloc returns pointer to memory which contains garbage Use free on pointers to unmalloc. Do this or you ll get memory leaks. CS61C L14 CALL I, 1 st half rev iew (52)

C structures A struct is a data structure composed of existing data types. Commonly used with typedef struct point { int x; int y; }; typedef struct point point_t; CS61C L14 CALL I, 1 st half rev iew (53) C structures The C arrow operator (->) dereferences and extracts a structure field with a single operator. The following are equivalent: struct point *p; printf( x is %d\n, (*p).x); printf( x is %d\n, p->x); Struct size: usually the size of everything inside added up, then word aligned CS61C L14 CALL I, 1 st half rev iew (54)

Normal C Memory Management A program s address space contains 4 regions: stack: local variables, grows downward heap: space requested for pointers via malloc() ; resizes dynamically, grows upward static data: variables declared outside main, does not grow or shrink code: loaded when program starts, does not change CS61C L14 CALL I, 1 st half rev iew (55) ~ FFFF FFFF hex ~ 0 hex stack heap static data code For now, OS somehow prevents accesses between stack and heap (gray hash lines). Wait for virtual memory The Stack Stack frame includes: Return instruction address Parameters Space for other local variables Stack frames contiguous blocks of memory; stack pointer tells where top stack frame is When procedure ends, stack frame is tossed off the stack; frees memory for future stack frames SO SP frame frame frame frame CS61C L14 CALL I, 1 st half rev iew (56)

The Heap (Dynamic memory) Large pool of memory, not allocated in contiguous order back-to-back requests for heap memory could result blocks very far apart In C, tell malloc number of bytes of memory explicitly to allocate item CS61C L14 CALL I, 1 st half rev iew (57) Memory Management How do we manage memory? Code, Static storage are easy: they never grow or shrink Stack space is also easy: stack frames are created and destroyed in last-in, first-out (LIFO) order Managing the heap is tricky: memory can be allocated / deallocated at any time CS61C L14 CALL I, 1 st half rev iew (58)

Heap Management Issues Want malloc() and free() to run quickly. Want minimal memory overhead Want to avoid fragmentation* when most of our free memory is in many small chunks In this case, we might have many free bytes but not be able to satisfy a large request since the free bytes are not contiguous in memory. * This is technically called external fragmention CS61C L14 CALL I, 1 st half rev iew (59) Choosing a free block in malloc() Best-fit: Tries to limit fragmentation but at the cost of time (must examine all free blocks for each malloc). Leaves lots of small blocks (why?) First-fit: Quicker than best-fit (why?) but potentially more fragmentation. Tends to concentrate small blocks at the beginning of the free list (why?) Next-fit: Does not concentrate small blocks at front like first-fit, should be faster as a result. CS61C L14 CALL I, 1 st half rev iew (60)

Slab Allocator A different way to divide memory Divide blocks into large and small by picking an arbitrary threshold size. Blocks larger than this threshold are managed with a freelist (as before). Use a bitmap for each range of blocks of the same size. Finding free block = finding 0 bit Freeing block = clearing 1 bit Fast for small blocks, no external fragmentation But we have internal fragmentation now CS61C L14 CALL I, 1 st half rev iew (61) Buddy System If no free block of size n is available, find a block of size 2n and split it in to two blocks of size n When a block of size n is freed, if its neighbor (who is its buddy) of size n is also free, combine the blocks in to a single block of size 2n Buddy is block in other half larger block buddies NOT buddies Helps reduce some of the internal fragmentation in slab allocator CS61C L14 CALL I, 1 st half rev iew (62)

Garbage collection Can t do with a weakly typed language because we have no information about object sizes. Start with all pointers in global variables and local variables (root set). Recursively examine dynamically allocated objects we see a pointer to. These are the only things we need to keep. CS61C L14 CALL I, 1 st half rev iew (63) Scheme 1: Reference Counting Counting pointers. For every chunk of dynamically allocated memory, keep a count of number of pointers that point to it. When the count reaches 0, reclaim. Simple assignment statements can result in a lot of work, since may update reference counts of many items Breaks on cyclical structures. CS61C L14 CALL I, 1 st half rev iew (64)

Scheme 2: Mark and Sweep Garbage Col. Keep allocating new memory until memory is exhausted, then try to find unused memory. Consider objects in heap a graph, chunks of memory (objects) are graph nodes, pointers to memory are graph edges. Edge from A to B A stores pointer to B Can start with the root set, perform a graph traversal, find all usable memory! 2 Phases: 1. Mark used nodes 2. Sweep free ones, returning list of free nodes CS61C L14 CALL I, 1 st half rev iew (65) Pretty slow because of graph traversal Scheme 3: Copying Garbage Collection Divide memory into two spaces, only one in use at any time. When active space is exhausted, traverse the active space, copying all objects to the other space, then make the new space active and continue. Only reachable objects are copied! Use forwarding pointers to keep consistency Bad: Only get to use half the memory. CS61C L14 CALL I, 1 st half rev iew (66)

Assembly Language MIPS is an ISA. An ISA is the set of an instructions a CPU can execute. MIPS philosophy: RISC. Keep hardware simple and fast. As few instructions as possible Fast common case. CS61C L14 CALL I, 1 st half rev iew (67) MIPS intro In MIPS Assembly Language: Registers replace C variables - Registers don t have type, operations do One Instruction (simple operation) per line Simpler is Better Smaller is Faster Basic instruction syntax op dest, src1, src2 CS61C L14 CALL I, 1 st half rev iew (68)

Data Transfer: Memory and Registers Load FROM memory, store TO memory lw $t0,0($s0) # t0 = *s0; sw $t0,0($s0) # *s0 = t0; Notes: base register MUST be word aligned if using lw/sw - Base register is equivalent to C pointer Base register + offset must be word aligned if using lw/sw CS61C L14 CALL I, 1 st half rev iew (69) Inequalities in MIPS Set on Less Than Syntax: slt reg1,reg2,reg3 - reg1 = (reg2 < reg3)? 1 : 0; - With bne, get if( < )goto - With beq, get if( >= )goto Set greater than? - reg1 = (reg2 > reg3)? 1 : 0; - slt reg1, reg3, reg2 - With bne, get if( > )goto - With beq, get if( <= )goto Can also just add 1 to operand to do and equal CS61C L14 CALL I, 1 st half rev iew (70)

MIPS Control flow conditional branches beq register1, register2, L1 bne register1, register2, L1 unconditional branch j label # Just like C s goto Use these to write loops CS61C L14 CALL I, 1 st half rev iew (71) MIPS Signed vs. Unsigned diff meanings! MIPS Signed v. Unsigned is an overloaded term Do/Don't sign extend (lb, lbu) Don't overflow (addu, addiu, subu, multu, divu) Do signed/unsigned compare (slt, slti/sltu, sltiu) CS61C L14 CALL I, 1 st half rev iew (72)

Calling functions jal saves next instruction address in $ra and jumps to label jal label Follow register conventions! CalleE must restore $s0-$s7 and $sp before returning. CalleR must save $ra, $v s, $a s, $t s CS61C L14 CALL I, 1 st half rev iew (73) MIPS Registers The constant 0 $0 $zero Reserved for Assembler $1 $at Return Values $2-$3 $v0-$v1 Arguments $4-$7 $a0-$a3 Temporary $8-$15 $t0-$t7 Saved $16-$23 $s0-$s7 More Temporary $24-$25 $t8-$t9 Used by Kernel $26-27 $k0-$k1 Global Pointer $28 $gp Stack Pointer $29 $sp Frame Pointer $30 $fp Return Address $31 $ra (From COD 3 rd Ed. green insert) Use names for registers -- code is clearer! CS61C L14 CALL I, 1 st half rev iew (74)

R I J MIPS instruction formats MIPS Machine Language Instruction: 32 bits representing a single instruction opcode rs rt rd shamt funct opcode rs rt immediate opcode target address Branches use PC-relative addressing, Jumps use absolute addressing. Learn to read green sheet! CS61C L14 CALL I, 1 st half rev iew (75) IEEE 754 Floating Point Standard Normal format: +1.xxxx xxx two *2 yyy y two Single Precision (DP similar): 31 30 23 22 S Exponent Significand 1 bit 8 bits 23 bits Sign bit: Significand: CS61C L14 CALL I, 1 st half rev iew (76) 1 means negative 0 means positive To pack more bits, leading 1 implicit for normalized numbers Exponent: Get unsigned value, then subtract bias 0

FP special cases Reserve exponents, significands: Exponent Significand Object 0 0 0 0 nonzero Denorm 1-254 anything +/- fl. pt. # 255 0 +/- 255 nonzero NaN CS61C L14 CALL I, 1 st half rev iew (77) What are Machine Structures? Software Hardware Application (ex: browser) Compiler Assembler Processor Memory Digital Design Circuit Design transistors Operating System (Mac OSX) Datapath & Control I/O system 61C Instruction Set Architecture * Coordination of many levels (layers) of abstraction CS61C L14 CALL I, 1 st half rev iew (78)

61C Levels of Representation High Level Language Program (e.g., C) Compiler Assembly Language Program (e.g.,mips) Assembler Machine Language Program (MIPS) Machine Interpretation Hardware Architecture Description (Logic, Logisim, etc.) Architecture Implementation Logic Circuit Description (Logisim, etc.) temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; lw $t0, 0($2) lw $t1, 4($2) sw $t1, 0($2) sw $t0, 4($2) 0000 1001 1100 0110 1010 1111 0101 1000 1010 1111 0101 1000 0000 1001 1100 0110 1100 0110 1010 1111 0101 1000 0000 1001 0101 1000 0000 1001 1100 0110 1010 1111 CS61C L14 CALL I, 1 st half rev iew (79)