Machine-Level Programming II Control Flow Sept. 10, 1998 Topics Control Flow –Varieties of Loops –Switch Statements class06.ppt 15-213 “The course that.

Slides:



Advertisements
Similar presentations
University of Amsterdam Computer Systems – Control in C Arnoud Visser 1 Computer Systems New to C?
Advertisements

Fabián E. Bustamante, Spring 2007 Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
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.
The University of Adelaide, School of Computer Science
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.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
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.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
Machine-Level Programming II: Control Flow Sept. 12, 2002 Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements.
Machine-Level Programming II Control Flow Sept. 13, 2001 Topics Condition Codes –Setting –Testing Control Flow –If-then-else –Varieties of Loops –Switch.
– 1 – Homework 1 Count 25 Mean 73.8 Std Min 22 Max 96 Mode 66 Median80.5.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
Machine-Level Programming II: Control Flow Topics Condition codes Conditional branches Loops Switch statements CS 105 “Tour of the Black Holes of Computing”
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Machine-Level Programming II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of loops Switch statements CS 105.
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
Lecture 4: MIPS Instruction Set
Machine-Level Programming II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of loops Switch statements CS 105.
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
1 Machine-Level Programming IV: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2015 Systems book chapter 3* * Modified.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
CDA 3101 Spring 2016 Introduction to Computer Organization
1 Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2016 Systems book chapter 3* * Modified.
IA32: Control Flow Topics –Condition Codes Setting Testing –Control Flow If-then-else Varieties of Loops Switch Statements.
Machine-Level Programming II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of loops Switch statements CS 105.
Machine-Level Programming II Control Flow Sept. 14, 2000 Topics Condition Codes –Setting –Testing Control Flow –If-then-else –Varieties of Loops –Switch.
Machine-Level Programming 2 Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements.
Machine-Level Programming II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements class06.ppt.
Reading Condition Codes (Cont.)
Machine-Level Programming 2 Control Flow
Lecture 5: Procedure Calls
CS2100 Computer Organisation
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Machine-Level Programming II Control Flow Sept. 9, 1999
Machine-Level Programming II: Control
Assembly Programming using MIPS R3000 CPU
Machine-Level Programming II: Control Flow
Instructions - Type and Format
Condition Codes Single Bit Registers
Machine-Level Programming: Control Flow
Machine-Level Programming 2 Control Flow
Machine-Level Programming II: Control Flow
ECE232: Hardware Organization and Design
Machine-Level Programming 2 Control Flow
The University of Adelaide, School of Computer Science
Today Control flow if/while/do while/for/switch
Machine-Level Programming 2 Control Flow
Lecture 5: Procedure Calls
Machine-Level Programming II: Control Flow
Assembly Programming using MIPS R3000 CPU
Computer Architecture
Introduction Lab1 A crash course in assembler programming
CS201- Lecture 8 IA32 Flow Control
9/27: Lecture Topics Memory Data transfer instructions
Presentation transcript:

Machine-Level Programming II Control Flow Sept. 10, 1998 Topics Control Flow –Varieties of Loops –Switch Statements class06.ppt “The course that gives CMU its Zip!”

CS 213 F’98– 2 – class06.ppt Alpha Register Convention General Purpose Registers 32 total Store integers and pointers Fast access: 2 reads, 1 write in single cycle Usage Conventions Established as part of architecture Used by all compilers, programs, and libraries Assures object code compatibility –e.g., can mix Fortran and C $0 $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15 v0 t0 t1 t2 t3 t4 t5 t6 t7 s0 s1 s2 s3 s4 s5 s6,fp Return value from integer functions Temporaries (not preserved across procedure calls) Callee saved Frame pointer, or callee saved

CS 213 F’98– 3 – class06.ppt Registers (cont.) Important Ones for Now $0 Return Value $1…$8 Temporaries $16 First argument $17 Second argument $26 Return address $31 Constant 0 $16 $17 $18 $19 $20 $21 $22 $23 $24 $25 $26 $27 $28 $29 $30 $31 a0 a1 a2 a3 a4 a5 t8 t9 t10 t11 ra pv,t12 AT gp sp zero Integer arguments Temporaries Current proc addr or Temp Always zero Global pointer Stack pointer Reserved for assembler Return address

CS 213 F’98– 4 – class06.ppt C Code long int fact_do (long int x) { long int result = 1; do { result *= x; x = x-1; } while (x > 1); return result; Goto Version long int fact_goto (long int x) { long int result = 1; loop: result *= x; x = x-1; if (x > 1) goto loop; return result; } “Do-While” Loop Example C allows “goto” as means of transferring control –Closer to machine-level programming style Generally considered bad coding style

CS 213 F’98– 5 – class06.ppt Goto Version long int fact_goto (long int x) { long int result = 1; loop: result *= x; x = x-1; if (x > 1) goto loop; return result; } “Do-While” Loop Compilation Registers $16x $0result bis $31,1,$0# result = 1 $37:# loop: mulq $0,$16,$0# result *= x subq $16,1,$16# x = x-1 cmple $16,1,$1# if !(x<=1) beq $1,$37# goto loop ret $31,($26),1# return Assembly New Instructions bis a, b, cc = a | b cmple a, b, cc = a <= b

CS 213 F’98– 6 – class06.ppt C Code do Body while ( Test ); Goto Version loop: Body if ( Test ) goto loop General “Do-While” Translation Body can be any C statement –Typically compound statement: Test is expression returning integer = 0 interpreted as false≠ 0 interpreted as true { Statement 1 ; Statement 2 ; … Statement n ; }

CS 213 F’98– 7 – class06.ppt C Code long int fact_while (long int x) { long int result = 1; while (x > 1) { result *= x; x = x-1; }; return result; First Goto Version long int fact_while_goto (long int x) { long int result = 1; loop: if (!(x > 1)) goto done; result *= x; x = x-1; goto loop; done: return result; } “While” Loop Example Is this code equivalent to the do-while version? Must jump out of loop if test fails

CS 213 F’98– 8 – class06.ppt C Code long int fact_while (long int x) { long int result = 1; while (x > 1) { result *= x; x = x-1; }; return result; Second Goto Version long int fact_while_goto2 (long int x) { long int result = 1; if (!(x > 1)) goto done; loop: result *= x; x = x-1; if (x > 1) goto loop; done: return result; } Actual “While” Loop Translation Uses same inner loop as do-while version Guards loop entry with extra test

CS 213 F’98– 9 – class06.ppt C Code while ( Test ) Body Do-While Version if (! Test ) goto done; do Body while( Test ); done: General “While” Translation Goto Version if (! Test ) goto done; loop: Body if ( Test ) goto loop; done:

CS 213 F’98– 10 – class06.ppt “While” Loop Example #2 Algorithm Exploit property that p = p 0 + 2p 1 + 4p 2 + … 2 n–1 p n–1 Gives: x p = z 0 · z 1 2 · (z 2 2 ) 2 · … · (…((z n –1 2 ) 2 )…) 2 z i = 1 when p I = 0 z i = x when p I = 1 Complexity O(log p) /* Compute x raised to nonnegative power p */ long int ipwr_while(long int x, long unsigned p) { long int result = 1; while (p) { if (p & 0x1) result *= x; x = x*x; p = p>>1; } return result; } n times

CS 213 F’98– 11 – class06.ppt “While”  “Do-While ”  “Goto ” long int result = 1; while (p) { if (p & 0x1) result *= x; x = x*x; p = p>>1; } long int result = 1; if (!p) goto done; do { if (p & 0x1) result *= x; x = x*x; p = p>>1; } while (p); done: long int result = 1; if (!p) goto done; loop: if (!(p & 0x1)) goto skip; result *= x; skip: x = x*x; p = p>>1; if (p) goto loop; done: Also converted conditional update into test and branch around update code

CS 213 F’98– 12 – class06.ppt Example #2 Compilation bis $31,1,$0# result = 1 beq $17,$52# if p=0 # goto done $53:# loop: blbc $17,$54# if (p&0x1) # goto skip mulq $0,$16,$0# result *= x $54:# skip: mulq $16,$16,$16# x *= x srl $17,1,$17# p = p>>1 bne $17,$53# if p != 0 # goto loop $52:# done ret $31,($26),1# return AssemblyGoto Version long int result = 1; if (!p) goto done; loop: if (!(p & 0x1)) goto skip; result *= x; skip: x = x*x; p = p>>1; if (p) goto loop; done:

CS 213 F’98– 13 – class06.ppt “For” Loop Example for (Init; Test; Update ) Body long int result; for (result = 1; p != 0; p = p>>1) { if (p & 0x1) result *= x; x = x*x; } General Form Init result = 1 Test p != 0 Update p = p >> 1 Body { if (p & 0x1) result *= x; x = x*x; }

CS 213 F’98– 14 – class06.ppt “For”  “While” for (Init; Test; Update ) Body Init; while (Test ) { Body Update ; } Goto Version Init; if (! Test ) goto done; loop: Body Update ; if ( Test ) goto loop; done: While VersionFor Version Do-While Version Init; if (! Test ) goto done; do { Body Update ; } while (Test) done:

CS 213 F’98– 15 – class06.ppt “For” Loop Compilation Init result = 1 Test p != 0 Update p = p >> 1 Body { if (p & 0x1) result *= x; x = x*x; } Goto Version Init; if (! Test ) goto done; loop: Body Update ; if ( Test ) goto loop; done: result = 1 ; if (p == 0) goto done; loop: if (p & 0x1) result *= x; x = x*x; p = p >> 1; if (p != 0) goto loop; done:

CS 213 F’98– 16 – class06.ppt Compiling Switch Statements Implementation Options Series of conditionals –Good if few cases –Slow if many Jump Table –Lookup branch target –Avoids conditionals –Possible when cases are small integer constants GCC –Picks one based on case structure Bug in example code –No default given typedef enum {ADD, MULT, MINUS, DIV, MOD, BAD} op_type; char unparse_symbol(op_type op) { switch (op) { case ADD : return '+'; case MULT: return '*'; case MINUS: return '-'; case DIV: return '/'; case MOD: return '%'; case BAD: return '?'; }

CS 213 F’98– 17 – class06.ppt Jump Table Structure Code Block 0 Targ0: Code Block 1 Targ1: Code Block 2 Targ2: Code Block n–1 Targn-1: Targ0 Targ1 Targ2 Targn-1 jtab: target = JTab[op]; goto *target; switch(op) { case 0: Block 0 case 1: Block 1 case n-1: Block n–1 } Switch Form Approx. Translation Jump Table Jump Targets

CS 213 F’98– 18 – class06.ppt Switch Statement Example Branching Possibilities Setup: # op in $16 zapnot $16,15,$16# zero upper 32 bits cmpule $16,5,$1# if (op > 5) then beq $1,$66# branch to return lda $1,$74# $1 = &jtab[0] - $gp s4addq $16,$1,$1# $1 = &jtab[op] - $gp ldl $1,0($1)# $1 = jtab[op] - $gp addq $1,$29,$2# $2 = jtab[op] jmp $31,($2),$68# jump to *jtab[op] Enumerated Values ADD0 MULT1 MINUS2 DIV3 MOD4 BAD5 typedef enum {ADD, MULT, MINUS, DIV, MOD, BAD} op_type; char unparse_symbol(op_type op) { switch (op) { } Check for out-of-range cases Set up jump

CS 213 F’98– 19 – class06.ppt Assembly Setup Explanation Instructions zapnot a, b, c Use low order byte of b as mask m byte(c,i) = m[i] ? byte(a,i) : 0 cmpule a, b, c c = ((unsigned long) a <= (unsigned long) b) Symbolic Labels Labels of form $XX translated into addresses by assembler Table Structure Each target requires 4 bytes Base address of jtab at $gp + $74

CS 213 F’98– 20 – class06.ppt Jump Table Enumerated Values ADD0 MULT1 MINUS2 DIV3 MOD4 BAD5 $74:.gprel32 $68.gprel32 $69.gprel32 $70.gprel32 $71.gprel32 $72.gprel32 $73 Table Contents $68: bis $31,43,$0# return ‘+’ ret $31,($26),1 $69: bis $31,42,$0 # return ‘*’ ret $31,($26),1 $70: bis $31,45,$0 # return ‘-’ ret $31,($26),1 $71: bis $31,47,$0 # return ‘/’ ret $31,($26),1 $72: bis $31,37,$0 # return ‘%’ ret $31,($26),1 $73: bis $31,63,$0# return ‘?’ $66: ret $31,($26),1 Targets & Completion