360 likes | 562 Views
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
E N D
Carnegie Mellon Carnegie Mellon Assembly: Arithmetic & ControlCSCi 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 • 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
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)
Carnegie Mellon Address Computation Examples
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
Carnegie Mellon Today • Complete addressing mode, address computation (leal) • Arithmetic operations • Control: Condition codes • Conditional branches • Loops
Carnegie Mellon Some “BINARY” Arithmetic Operations • Two Operand Instructions: FormatComputation addlSrc,DestDest = Dest + Src sublSrc,DestDest = DestSrc 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?
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
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
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)
Carnegie Mellon Today • Complete addressing mode, address computation • Arithmetic operations • Control: Condition codes • Conditional branches • Loops
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).
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)
Carnegie Mellon Reading Condition Codes • SetXInstructions • setx<single_byte_register> • Set single byte based on combinations of condition codes
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
Carnegie Mellon Today • Complete addressing mode, address computation (leal) • Arithmetic operations • Control: Condition codes • Conditional branches • Loops
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; …
Conditional branch • If (test-expr) then-stmt else else-stmt t = test-expr; if (!t) gotoFalse; then-stmt; goto Done; False: else-stmt; Done:
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
Carnegie Mellon Today • Complete addressing mode, address computation (leal) • Arithmetic operations • Control: Condition codes • Conditional branches • Loops
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:
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
Exercise • “goto” versions intpcount_do(unsigned x) { int result = 0; loop: result += x & 0x1; x >>= 1; if (x) goto loop; return result; }
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
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; }
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:
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; }
Exercise: transformed C code • On your own …
Carnegie Mellon Summary • Today • Complete addressing mode, address computation (leal) • Arithmetic operations • Control: Condition codes • Conditional branches • Loops
Next Time • Next Time • Procedures • Stack • Call / return • Chap. 3.7 • Have a great weekend!