1 / 35

Tech Support Cheat Sheet

Tech Support Cheat Sheet. Carnegie Mellon. Carnegie Mellon. Assembly: Arithmetic & Control CSCi 2021: Computer Architecture and Organization. Chap. 3.4 - 3.5. Last Time. Assembly Basics: Registers, operands, move. Carnegie Mellon. Today. Complete addressing mode, address computation

rayya
Download Presentation

Tech Support Cheat Sheet

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. Tech Support Cheat Sheet

  2. Carnegie Mellon Carnegie Mellon Assembly: Arithmetic & ControlCSCi 2021: Computer Architecture and Organization Chap. 3.4 - 3.5

  3. Last Time • Assembly Basics: Registers, operands, move

  4. Carnegie Mellon Today • Complete addressing mode, address computation • Arithmetic operations • Control: Condition codes • Conditional branches • Loops We will not cover every mode or operation! Project #2 is out Exam #1 in one week Will talk about on Monday/Tuesday

  5. Carnegie Mellon Complete Memory Addressing Modes • Most General Form • D (Rb,Ri,S) => Mem[Reg[Rb]+S*Reg[Ri]+ D] • D: Constant “displacement” 1, 2, or 4 bytes • Rb: Base register • Ri: Index register • S: Scale: 1, 2, 4, or 8 • Special Cases (no S, S=>1; no D, D=>0)

  6. Carnegie Mellon Address Computation Examples

  7. Carnegie Mellon Address Computation Instruction • lealSrc,Dest • Src is address mode expression • Set Dest to address denoted by expression • Uses • Computes addresses with a single instruction => register • leal (x, y, k)=> x + k*y • k = 1, 2, 4, or 8 • Example (also used for optimizing arithmetic) int mul12(int x) { return x*12; } leal (%eax,%eax,2), %eax ;t <- x+x*2 sall $2, %eax ;return t<<2

  8. Carnegie Mellon Today • Complete addressing mode, address computation (leal) • Arithmetic operations • Control: Condition codes • Conditional branches • Loops

  9. Carnegie Mellon Some “BINARY” Arithmetic Operations • Two Operand Instructions: FormatComputation addlSrc,DestDest = Dest + Src sublSrc,DestDest = DestSrc imullSrc,DestDest = Dest * Src sallSrc,DestDest = Dest << SrcAlso called shll sarlSrc,DestDest = Dest >> SrcArithmetic shrlSrc,DestDest = Dest >> SrcLogical xorlSrc,DestDest = Dest ^ Src andlSrc,DestDest = Dest & Src orlSrc,DestDest = Dest | Src • Watch out for argument order! • No distinction between signed and unsigned int (why?) • See book for more why 2?

  10. Carnegie Mellon Arithmetic Expression Example arith: pushl %ebp movl %esp, %ebp movl 8(%ebp), %ecx movl 12(%ebp), %edx leal (%edx,%edx,2), %eax sall $4, %eax leal 4(%ecx,%eax), %eax addl %ecx, %edx addl 16(%ebp), %edx imull %edx, %eax popl %ebp ret Set Up intarith(int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; intrval = t2 * t5; return rval; } Body Finish

  11. Carnegie Mellon Understanding arith intarith(int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; intrval = t2 * t5; return rval; } Offset %ebp movl 8(%ebp), %ecx movl 12(%ebp), %edx leal (%edx,%edx,2), %eax sall $4, %eax leal 4(%ecx,%eax), %eax addl %ecx, %edx addl 16(%ebp), %edx imull %edx, %eax

  12. Carnegie Mellon Observations about arith intarith(int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; intrval = t2 * t5; return rval; } • Instructions in different order from C code • Some expressions require multiple instructions • No memory store! movl 8(%ebp), %ecx # ecx = x movl 12(%ebp), %edx # edx = y leal (%edx,%edx,2), %eax # eax = y*3 sall $4, %eax # eax*= 16 (t4) leal 4(%ecx,%eax), %eax # eax = t4 +x+4 (t5) addl %ecx, %edx # edx = x+y (t1) addl 16(%ebp), %edx # edx += z (t2) imull %edx, %eax # eax = t2 * t5 (rval)

  13. Carnegie Mellon Today • Complete addressing mode, address computation • Arithmetic operations • Control: Condition codes • Conditional branches • Loops

  14. Carnegie Mellon Condition Codes (Implicit Setting) • Single bit registers • CF Carry Flag – recent op carried (detect unsigned overflow) • SF Sign Flag – recent op value was negative • ZF Zero Flag – recent op value was 0 • OF Overflow Flag (detect TC over/under) • Implicitly set (think of it as side effect) by arithmetic operations Example: addlSrc, Dest↔ t = a+b CF set if carry from most significant bit ZF set if t == 0 SF set if t < 0 (as signed) Software can use these flags to conditionally jump, transfer data, set bytes. ALL arith operators set CC (except leal).

  15. Carnegie Mellon Condition Codes (Explicit Setting: Compare) • Explicit Setting Condition Codes by Compare Instruction • cmplSrc2, Src1 • cmplb,a like computing a-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(signed)

  16. Carnegie Mellon Reading Condition Codes • SetXInstructions • setx<single_byte_register> • Set single byte based on combinations of condition codes

  17. Carnegie Mellon Single byte Reading Condition Codes • SetX Instructions: • Set single byte based on combination of condition codes • One of 8 addressable byte registers • Does not alter remaining 3 bytes • Typically use movzbl to finish job intgt (int x, int y) { return x > y; } Body 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

  18. On Exams …

  19. Carnegie Mellon Today • Complete addressing mode, address computation (leal) • Arithmetic operations • Control: Condition codes • Conditional branches • Loops

  20. Carnegie Mellon Jumping • jX Instructions • Jump to different part of code depending on condition codes cmp%eax, %ebx je .L1 addl %ecx, %edx … .L1: movl %ecx, %edx in C: if (a==b) goto L1; x += y; L1: x = y; …

  21. Conditional branch • If (test-expr) then-stmt else else-stmt t = test-expr; if (!t) gotoFalse; then-stmt; goto Done; False: else-stmt; Done:

  22. CarneeMellon Conditional Branch Example intabsdiff(int x, int y) { int result; if (x > y) { result = x-y; } else { result = y-x; } return result; } absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle .L6 subl %eax, %edx movl %edx, %eax jmp .L7 .L6: subl %edx, %eax .L7: popl %ebp ret Setup Body1 Body2a Body2b %eax holds return result Finish

  23. Carnegie Mellon Today • Complete addressing mode, address computation (leal) • Arithmetic operations • Control: Condition codes • Conditional branches • Loops

  24. Loops do-while: do body-stmt while (test-expr); Loop: body-stmt; t = test-expr; if (t) goto Loop; while: while (test-expr) body-stmt; t=test-expr; If (!t) gotoDone; Loop: body-stmt; t = test-expr; if (t) gotoLoop; Done:

  25. Carnegie Mellon “Do-While” Loop Example C Code intpcount_do(unsigned x) { int result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result; } write goto version

  26. Exercise • “goto” versions intpcount_do(unsigned x) { int result = 0; loop: result += x & 0x1; x >>= 1; if (x) goto loop; return result; }

  27. Carnegie Mellon “Do-While” Loop Compilation Goto Version • Registers: %edxx %ecxresult intpcount_do(unsigned x){ int result = 0; loop: result += x & 0x1; x >>= 1; if (x) goto loop; return result; } What does this do? movl $0, %ecx # result = 0 .L2: # loop: movl %edx, %eax andl $1, %eax # t = x & 1 addl %eax, %ecx # result += t shrl %edx # x >>= 1 jne .L2 # If !0, goto loop

  28. Carnegie Mellon “While” Loop Example C Code GotoVersion intpcount_while(unsigned x) { int result = 0; while (x) { result += x & 0x1; x >>= 1; } return result; } intpcount_do(unsigned x){ int result = 0; if (!x) goto done; loop: result += x & 0x1; x >>= 1; if (x) goto loop; done: return result; }

  29. For loop for (init-expr; test-expr; update-expr) body-stmt; init-expr; if (!test-expr) gotodone; do { body-stmt; update-expr; } while (test-expr); done:

  30. Carnegie Mellon “For” Loop Example C Code intpcount_for(unsigned x) { inti; int result = 0; for (i = 0; i < WSIZE; i++) { result += 4; } return result; }

  31. Exercise: transformed C code • On your own …

  32. Carnegie Mellon Summary • Today • Complete addressing mode, address computation (leal) • Arithmetic operations • Control: Condition codes • Conditional branches • Loops

  33. Next Time • Next Time • Procedures • Stack • Call / return • Chap. 3.7 • Have a great weekend!

More Related