180 likes | 214 Views
Learn about setting and reading condition codes in computer systems programming, including examples and explanations.
E N D
CS 3214Computer Systems Godmar Back Lecture 3
Announcements • Exercise 2 handed out, due Monday • Project 1 due Wed, Feb 10 • Posted later today • Please read instructions first • Must be done on McB 124 machines or on rlogin cluster • Auto-fail rule 1: • Need at least phase_4 defused to pass class. • Should have access to systems lab (McB 124) via keycard • Has Unix workstations for you to use; use SLO account CS 3214 Spring 2010
Exercise 1 • Question 2: Why does password-less login require that Unix permissions be set up in a certain way? Your Computer Private key (in ~/.ssh/identity, ~/.ssh/id_dsa, or ~/.ssh/id_rsa) (processed by ssh) Lab Machine Allowed public keys ~/.ssh/authorized_keys (processed by sshd) Since presence of public key in ~you/.ssh/authorized_keys presents the holder of the matching private key with the right to log on as ‘you’, nobody else can be allowed to change or replace this file. Note that the latter is controlled by the permissions assigned to the directory ~/.ssh CS 3214 Spring 2010
The following slides are taken with permission from Complete Powerpoint Lecture Notes forComputer Systems: A Programmer's Perspective (CS:APP) Randal E. Bryant and David R. O'Hallaron http://csapp.cs.cmu.edu/public/lectures.html Part 2 Programs and Data CS 3214 Spring 2010
Condition Codes • Single Bit Registers CF Carry Flag SF Sign Flag ZF Zero Flag OF Overflow Flag • Implicitly Set By Arithmetic Operations addlSrc,Dest C analog:t = a + b • CF set if carry out from most significant bit • Used to detect unsigned overflow • ZF set if t == 0 • SF set if t < 0 • OF set if two’s complement overflow (a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0) • Not Set by lealinstruction CS 3214 Spring 2010
Setting Condition Codes (cont.) Explicit Setting by Compare Instruction cmplSrc2,Src1 • cmplb,alike computinga-b without setting destination • CF set if carry out from most significant bit • Used for unsigned comparisons • ZF set if a == b • SF set if (a-b) < 0 • OF set if two’s complement overflow (a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0) CS 3214 Spring 2010
Setting Condition Codes (cont.) • Explicit Setting by Test instruction testlSrc2,Src1 • Sets condition codes based on value of Src1&Src2 • Useful to have one of the operands be a mask testl b,a like computinga&b without setting destination • ZF set when a&b == 0 • SF set when a&b < 0 CS 3214 Spring 2010
Reading Condition Codes • setXInstructions • Set single byte based on combinations of condition codes CS 3214 Spring 2010
Reading Condition Codes (Cont.) • setXInstructions • 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! CS 3214 Spring 2010
Jumping • jX Instructions • Jump to different part of code depending on condition codes CS 3214 Spring 2010
Conditional Branch Example max: pushl %ebp movl %esp,%ebp movl 8(%ebp),%edx movl 12(%ebp),%eax cmpl %eax,%edx jle L9 movl %edx,%eax L9: movl %ebp,%esp popl %ebp ret Set Up int max(int x, int y) { if (x > y) return x; else return y; } Body Finish CS 3214 Spring 2010
Conditional Branch Example (Cont.) int goto_max(int x, int y) { int rval = y; int ok = (x <= y); if (ok) goto done; rval = x; done: return rval; } • Compiler treats code on previous slide as if it had been written as shown left • Aside: • C allows “goto” as means of transferring control • Closer to machine-level programming style • Generally considered bad coding style • But exceptions exist, particularly when using systems programming idioms movl 8(%ebp),%edx # edx = x movl 12(%ebp),%eax # eax = y cmpl %eax,%edx # x : y jle L9 # if <= goto L9 movl %edx,%eax # eax = x L9: # Done: Skipped when x y CS 3214 Spring 2010
“Do-While” Loop Example C Code Goto Version • Use backward branch to continue looping • Only take branch when “while” condition holds int fact_do (int x) { int result = 1; do { result *= x; x = x-1; } while (x > 1); return result; } int fact_goto(int x) { int result = 1; loop: result *= x; x = x-1; if (x > 1) goto loop; return result; } CS 3214 Spring 2010
“Do-While” Loop Compilation Goto Version Assembly • Registers %edx x %eax result int fact_goto (int x) { int result = 1; loop: result *= x; x = x-1; if (x > 1) goto loop; return result; } fact_goto: pushl %ebp # Setup movl %esp,%ebp # Setup movl $1,%eax # eax = 1 movl 8(%ebp),%edx # edx = x L11: imull %edx,%eax # result *= x decl %edx # x-- cmpl $1,%edx # Compare x : 1 jg L11 # if > goto loop movl %ebp,%esp # Finish popl %ebp # Finish ret # Finish CS 3214 Spring 2010
General “Do-While” Translation C Code Goto Version do Body while (Test); loop: Body if (Test) goto loop • Body can be any C statement • Typically compound statement: • Test is expression returning integer = 0 interpreted as false 0 interpreted as true { Statement1; Statement2; … Statementn; } CS 3214 Spring 2010
“While” Loop Example #1 C Code First Goto Version int fact_while (int x) { int result = 1; while (x > 1) { result *= x; x = x-1; }; return result; } int fact_while_goto (int x) { int result = 1; loop: if (!(x > 1)) goto done; result *= x; x = x-1; goto loop; done: return result; } CS 3214 Spring 2010
Actual “While” Loop Translation C Code Second Goto Version • Uses same inner loop as do-while version • Guards loop entry with extra test int fact_while(int x) { int result = 1; while (x > 1) { result *= x; x = x-1; }; return result; } int fact_while_goto2 (int x) { int result = 1; if (!(x > 1)) goto done; loop: result *= x; x = x-1; if (x > 1) goto loop; done: return result; } CS 3214 Spring 2010
General “While” Translation C Code while (Test) Body Goto Version Do-While Version if (!Test) goto done; loop: Body if (Test) goto loop; done: if (!Test) goto done; do Body while(Test); done: CS 3214 Spring 2010