Condition Codes Single Bit Registers

Slides:



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

Machine-Level Programming III: Procedures Feb. 8, 2000 Topics IA32 stack Stack-based languages Stack frames Register saving conventions Creating pointers.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
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.
Machine-Level Programming III: Procedures Sept. 17, 2007 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
Machine-Level Programming III: Procedures Jan 30, 2003
– 1 – Homework 1 Count 25 Mean 73.8 Std Min 22 Max 96 Mode 66 Median80.5.
Machine-Level Programming III: Procedures Sept. 15, 2006 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
University of Washington x86 Programming III The Hardware/Software Interface CSE351 Winter 2013.
Machine-Level Programming III: Switch Statements and IA32 Procedures Seoul National University.
Machine-Level Programming V: Switch Statements Comp 21000: Introduction to Computer Systems & Assembly Lang Systems book chapter 3* * Modified slides from.
University of Washington Today More on procedures, stack etc. Lab 2 due today!  We hope it was fun! What is a stack?  And how about a stack frame? 1.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
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.
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 III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
Machine-Level Programming II Control Flow Sept. 10, 1998 Topics Control Flow –Varieties of Loops –Switch Statements class06.ppt “The course that.
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
C function call conventions and the stack
Machine-Level Programming II: Arithmetic & Control
Machine-Level Programming II Control Flow Sept. 9, 1999
Carnegie Mellon Machine-Level Programming III: Switch Statements and IA32 Procedures / : Introduction to Computer Systems 7th Lecture, Sep.
Machine-Level Programming II: Control
Chapter 3 Machine-Level Representation of Programs
Machine-Level Representation of Programs II
Ch. 2 Two’s Complement Boolean vs. Logical Floating Point
Machine-Level Programming II: Control Flow
Machine-Level Programming III: Switch Statements and IA32 Procedures
Y86 Processor State Program Registers
Instructors: Majd Sakr and Khaled Harras
Machine-Level Programming 4 Procedures
Instructor: David Ferry
Machine-Level Programming: Control Flow
Machine-Level Programming 2 Control Flow
Machine-Level Programming II: Control Flow
Machine-Level Programming 2 Control Flow
Machine-Level Programming III: Procedures Sept 18, 2001
Machine-Level Representation of Programs III
Today Control flow if/while/do while/for/switch
Machine-Level Programming 2 Control Flow
Machine-Level Representation of Programs II
Chapter 3 Machine-Level Representation of Programs
Machine-Level Programming II: Control Flow
X86 Assembly - Control.
Machine-Level Programming II: Control Flow Sept. 12, 2007
“Way easier than when we were students”
CS201- Lecture 8 IA32 Flow Control
Presentation transcript:

Condition Codes Single Bit Registers CF Carry Flag SF Sign Flag ZF Zero Flag OF Overflow Flag Implicitly Set By Arithmetic Operations, except leal cmpl and testl jX

Set your own CC setX Instructions Set single byte based on combinations of condition codes One of 8 addressable byte registers Embedded within first 4 integer registers Does not alter remaining 3 bytes Typically use movzbl to finish job %eax %ah %al %edx %dh %dl %ecx %ch %cl %ebx %bh %bl %esi int gt (int x, int y) { return x > y; } %edi %esp Body %ebp movl 12(%ebp),%eax # eax = y cmpl %eax,8(%ebp) # Compare x : y setg %al # al = x > y movzbl %al,%eax # Zero rest of %eax Note inverted ordering!

t = test_expr; if (t) if (!t) then-statement; goto false; else do then-statement; goto done; false: do else-statement; done: if (t) then-statement; else else-statement; if (!Test) goto done; loop: Body if (Test) goto loop done: while (Test) Body

Body for (Init; Test; Update ) Init; if (!Test) goto done; loop: Body goto loop; done:

3.6.7.Switch Statements Implementation Options Series of conditionals 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 '?'; } 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

Jump Table Structure Jump Targets Switch Form Jump Table Targ0: Targ1: Code Block Targ0: 1 Targ1: 2 Targ2: n–1 Targn-1: • switch(op) { case val_0: Block 0 case val_1: Block 1 • • • case val_n-1: Block n–1 } Targ0 Targ1 Targ2 Targn-1 • jtab: Approx. Translation target = JTab[op]; goto *target;

Switch Statement Example Branching Possibilities Enumerated Values ADD 0 MULT 1 MINUS 2 DIV 3 MOD 4 BAD 5 typedef enum {ADD, MULT, MINUS, DIV, MOD, BAD} op_type; char unparse_symbol(op_type op){ switch (op) { • • • }

Approx. Translation target = JTab[op]; goto *target; unparse_symbol: pushl %ebp # Setup movl %esp,%ebp # Setup movl 8(%ebp),%eax # eax = op cmpl $5,%eax # Compare op : 5 ja .L49 # If > 5 goto done jmp *.L57(,%eax,4)# goto Table[op]

Assembly Setup Explanation Symbolic Labels Labels of form .LXX translated into addresses by assembler Table Structure Each target requires 4 bytes Base address at .L57 Jumping jmp .L49 Jump target is denoted by label .L49 jmp *.L57(,%eax,4) Start of jump table denoted by label .L57 Register %eax holds op Must scale by factor of 4 to get offset into table Fetch target from effective Address .L57 + op*4

Jump Table Targets & Completion Table Contents Enumerated Values ADD 0 movl $43,%eax # ’+’ jmp .L49 .L52: movl $42,%eax # ’*’ .L53: movl $45,%eax # ’-’ .L54: movl $47,%eax # ’/’ .L55: movl $37,%eax # ’%’ .L56: movl $63,%eax # ’?’ # Fall Through to .L49 .section .rodata .align 4 .L57: .long .L51 #Op = 0 .long .L52 #Op = 1 .long .L53 #Op = 2 .long .L54 #Op = 3 .long .L55 #Op = 4 .long .L56 #Op = 5 Enumerated Values ADD 0 MULT 1 MINUS 2 DIV 3 MOD 4 BAD 5

Switch Statement Completion .L49: # Done: movl %ebp,%esp # Finish popl %ebp # Finish ret # Finish Puzzle What value returned when op is invalid? Answer Register %eax set to op at beginning of procedure This becomes the returned value Advantage of Jump Table Can do k-way branch in O(1) operations

Object Code Setup Label .L49 becomes address 0x804875c Label .L57 becomes address 0x8048bc0 08048718 <unparse_symbol>: 8048718: 55 pushl %ebp 8048719: 89 e5 movl %esp,%ebp 804871b: 8b 45 08 movl 0x8(%ebp),%eax 804871e: 83 f8 05 cmpl $0x5,%eax 8048721: 77 39 ja 804875c <unparse_symbol+0x44> 8048723: ff 24 85 c0 8b jmp *0x8048bc0(,%eax,4)

Sparse Switch Example Not practical to use jump table /* Return x/111 if x is multiple && <= 999. -1 otherwise */ int div111(int x) { switch(x) { case 0: return 0; case 111: return 1; case 222: return 2; case 333: return 3; case 444: return 4; case 555: return 5; case 666: return 6; case 777: return 7; case 888: return 8; case 999: return 9; default: return -1; } Not practical to use jump table Would require 1000 entries Obvious translation into if-then-else would have max. of 9 tests

IA32/Linux Stack Frame Current Stack Frame (“Top” to Bottom) Caller Parameters for function about to call “Argument build” Local variables If can’t keep in registers Saved register context Old frame pointer Caller Stack Frame Return address Pushed by call instruction Arguments for this call Caller Frame Arguments Frame Pointer (%ebp) Return Addr Old %ebp Saved Registers + Local Variables Argument Build Stack Pointer (%esp)

IA32/Linux Register Usage Integer Registers Two have special uses %ebp, %esp Three managed as callee-save %ebx, %esi, %edi Old values saved on stack prior to using Three managed as caller-save %eax, %edx, %ecx Do what you please, but expect any callee to do so, as well Register %eax also stores returned value %eax Caller-Save Temporaries %edx %ecx %ebx Callee-Save Temporaries %esi %edi %esp Special %ebp

Recursive Factorial Registers %eax used without first saving .globl rfact .type rfact,@function rfact: pushl %ebp movl %esp,%ebp pushl %ebx movl 8(%ebp),%ebx cmpl $1,%ebx jle .L78 leal -1(%ebx),%eax pushl %eax call rfact imull %ebx,%eax jmp .L79 .align 4 .L78: movl $1,%eax .L79: movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Recursive Factorial int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1); return rval * x; } Registers %eax used without first saving %ebx used, but save at beginning & restore at end

Summary The Stack Makes Recursion Work Private storage for each instance of procedure call Instantiations don’t clobber each other Addressing of locals + arguments can be relative to stack positions Can be managed by stack discipline Procedures return in inverse order of calls IA32 Procedures Combination of Instructions + Conventions Call / Ret instructions Register usage conventions Caller / Callee save %ebp and %esp Stack frame organization conventions