1 / 23

CS 3214 Computer Systems

CS 3214 Computer Systems. Godmar Back. Lecture 3. Announcements. Office hours today: Back: 1:30pm – 3pm Puran: 6-8pm Exercise 1 due tomorrow Exercise 2 & Project 1 will be posted later today Please read instructions first Must be done on McB 124 machines or on rlogin cluster

ayame
Download Presentation

CS 3214 Computer Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 3214Computer Systems Godmar Back Lecture 3

  2. Announcements • Office hours today: • Back: 1:30pm – 3pm • Puran: 6-8pm • Exercise 1 due tomorrow • Exercise 2 & Project 1 will be posted later today • Please read instructions first • Must be done on McB 124 machines or on rlogin cluster • Should have access to systems lab (McB 124) via keycard • Has Unix workstations for you to use; use SLO account

  3. Setting Up ssh • Question 2: Password-less login requires that /.ssh/authorized keys is not writable by anyone • but the user to whom it belongs. Why? 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

  4. The following slides are taken with permission from Complete Powerpoint Lecture Notes forComputer Systems: A Programmer's Perspective 2nd Edition (CS:APP 2e) Randal E. Bryant and David R. O'Hallaron http://csapp.cs.cmu.edu/public/instructors.html Part 2 Programs and Data

  5. 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

  6. 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)

  7. 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

  8. Reading Condition Codes • setXInstructions • Set single byte based on combinations of condition codes

  9. 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!

  10. Jumping • jX Instructions • Jump to different part of code depending on condition codes

  11. 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

  12. 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

  13. “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; }

  14. “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

  15. 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; }

  16. “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; }

  17. 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; }

  18. 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:

  19. n–1 times “For” Loop Example /* Compute x raised to nonnegative power p */ int ipwr_for(int x, unsigned p) { int result; for (result = 1; p != 0; p = p>>1) { if (p & 0x1) result *= x; x = x*x; } return result; } • Algorithm • Exploit property that p = p0 + 2p1 + 4p2 + … 2n–1pn–1 • Gives: xp = z0 · z12 · (z22) 2 · … · (…((zn–12) 2 )…) 2 zi = 1 when pi = 0 zi = xwhen pi = 1 • Complexity O(log p) Example 310 = 32 * 38 = 32 * ((32) 2) 2

  20. ipwr Computation /* Compute x raised to nonnegative power p */ int ipwr_for(int x, unsigned p) { int result; for (result = 1; p != 0; p = p>>1) { if (p & 0x1) result *= x; x = x*x; } return result; }

  21. “For” Loop Example General Form int result; for (result = 1; p != 0; p = p>>1) { if (p & 0x1) result *= x; x = x*x; } for (Init; Test; Update ) Body Init Test Update result = 1 p != 0 p = p >> 1 { if (p & 0x1) result *= x; x = x*x; } Body

  22. “For” “While” For Version While Version for (Init; Test; Update ) Body Init; while (Test ) { Body Update ; } Do-While Version Goto Version Init; if (!Test) goto done; do { Body Update ; } while (Test) done: Init; if (!Test) goto done; loop: Body Update ; if (Test) goto loop; done:

  23. “For” Loop Compilation 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: Goto Version Init; if (!Test) goto done; loop: Body Update ; if (Test) goto loop; done: Body Init Test { if (p & 0x1) result *= x; x = x*x; } result = 1 p != 0 Update p = p >> 1

More Related