System Programming and Computer Architecture (Fall 2009) Recitation 2 October 8 th, 2009 Zaheer Chothia Email: zchothia@student.ethz.ch Web: http://n.ethz.ch/~zchothia/
Topics for Today Classroom Exercise 1 Review: Data Lab (Homework 2) Classroom Exercise & Homework 3 Parameter passing Control structures in assembly - If statements - Switch statements - For loops Reading condition codes with C Due in one week: October 15 2
Assignment 1
Review: Assignment 1 Question 3: Unsigned Two s complement x -x Hex Decimal Decimal Hex 2 2-2 E 0 0 0 0 3 3-3 D 8-8 -8 8 A -6 6 6 F -1 1 1 4
Review: Assignment 1 Question 5: float sum = 255.0; 0 1000 0110 1111 1110 0000 0000 0000 000 E2 = e 127 10 = 134 10 127 10 = 7 10 M2 = 1.f = 1.11111110000000000000000 float delta = 1.0e-5; 0 0110 1110 0100 1111 1000 1011 0101 100 E1 = e 127 10 = 110 10-127 10 = -17 10 M1 = 1.f = 1.01001111100010110101100 5
Review: Assignment 1 Question 5: E = max{e1, E2} = 710 M: 111111110000000000000000 + 101001111100010110101100 111111110000000000000000101001111100010110101100 111111110000000000000001 sum': rounding 0 1000 0110 1111 1110 0000 0000 0000 001 What happens after adding delta 65535 times? 6
Classroom Exercise 1
Question 5: Conditional branches What is the value of %eax, when the last label (.L3 and.l17 respectively) is reached? 5.1 %eax := a, %edx := d 1 cmpl %eax, %edx 2 jle.l2 3 subl %eax, %edx 4 movl %edx, %eax 5 jmp.l3 6.L2: 7 subl %edx, %eax 8.L3: 9... 5.2 %eax := 1, %edx := N 1 testl %ecx, %ecx 2 jle.l17 3 xorl %edx, %edx 4.L18: 5 incl %edx 6 addl %eax, %eax 7 cmpl %edx, %ecx 8 jne.l18 9.L17: 10... 8
Question 5: Conditional branches Solution 5.1 %eax := (a - d) if (d > a) { t = d - a; } else { t = a - d; } 5.2 %eax := 2 N t = 1; for (i = 0; i < N; i++) { t = t * 2; } 9
Review: Data Lab (Homework 2)
Surprising Results Almost half of the students beat the professor Really? Actually, there was a bug in the data lab Written in good old 32 bit times Use of LONG_MIN from limits.h to get T min But: int and long have different size on 64 bit machines Overflow in test code Accepted incorrect solutions To check if your solution is really correct: Pass -m32 to gcc in the Makefile 11
Example: islessorequal int islessorequal(int x, int y) { int x_neg = x>>31; int y_neg = y>>31; return!((!x_neg & y_neg) (!(x_neg ^ y_neg) & (y+~x+1)>>31)); } 12
Example: islessorequal int islessorequal(int x, int y) { int x_neg = x>>31; int y_neg = y>>31; return!((!x_neg & y_neg) (!(x_neg ^ y_neg) & (y+~x+1)>>31)); } Are x and y negative? 13
Example: islessorequal int islessorequal(int x, int y) { int x_neg = x>>31; int y_neg = y>>31; return!((!x_neg & y_neg) (!(x_neg ^ y_neg) & (y+~x+1)>>31)); } Are x and y negative? one is negative and the other is positive 14
Example: islessorequal int islessorequal(int x, int y) { int x_neg = x>>31; int y_neg = y>>31; return!((!x_neg & y_neg) (!(x_neg ^ y_neg) & (y+~x+1)>>31)); } Are x and y negative? both have the same sign recall: invert + increment = negate 15
Homework 3
Assembler: Warm-up exercise 1 movl $0x40, %eax 2 movl (,%eax,4), %ebx 3 leal 0x88(,%eax,2), %ecx 4 movl (%ecx), %ecx 5 cmpl %ecx, %ebx 6 jg L1 7 <Fragment A> 8 jmp L2 9 L1: 10 <Fragment B> 11 L2: 12... Memory Address Value 0x100 0xff 0x104 0xab 0x108 0x01 Which code fragment would be executed? 17
Assembler: Warm-up exercise 1 movl $0x40, %eax 2 movl (,%eax,4), %ebx 3 leal 0x88(,%eax,2), %ecx 4 movl (%ecx), %ecx 5 cmpl %ecx, %ebx 6 jg L1 7 <Fragment A> 8 jmp L2 9 L1: 10 <Fragment B> 11 L2: 12... Memory Address Value 0x100 0xff 0x104 0xab 0x108 0x01 Jump Condition %ebx - %ecx > 0 %ebx > %ecx Which code fragment would be executed? 18
IA32 Assembler Cheat Sheet Condition Codes Jump Instruction 1 (unconditional) jmp label ZF je label ~ZF jne label SF js label ~SF jns label ~(SF^OF) & ~ZF jg label ~(SF^OF) jge label SF^OF jl label (SF^OF) ZF jle label ~CF & ~ZF ja label CF jb label 19
IA32 Assembler Cheat Sheet Condition Codes CF addl Src,Dest cmpl Src2,Src1 testl Src2,Src1 addl a, b t = a + b cmpl a, b Set CCs based on b a Carry or borrow generated out of MSB (unsigned equivalent of overflow flag) testl a, b Set CCs based on a & b ZF t == 0 a == b a & b == 0 SF t < 0 (b-a) < 0 a & b < 0 OF 0 = b (a > 0 && b > 0 && t < 0) (a < 0 && b < 0 && t > 0) 0 20
Stack Layout Revisited 21
Stack vs. Heap Stack is the place for function parameters and local variables Recursion possible No need for memory management Heap is the place for global variables and dynamically managed memory Explicit memory management Globally accessible 22
IA32 Stack Frame Structure Caller Stack Frame Arguments for callee -Pushed from right-to-left ( instruction Return address (call Caller's Frame Arguments Return Addr 23
IA32 Stack Frame Structure Caller Stack Frame Arguments for callee -Pushed from right-to-left ( instruction Return address (call Current Stack Frame Caller's Frame Pointer Frame Arguments Return Addr Old %ebp +8 +4 Old frame pointer Saved register context -Callee safe registers ( spilling ) Local variables Current Stack Frame Saved Registers + Local Variables Parameters for next function call Stack Pointer Arguments 24
Parameter passing Parameters and return value in assembly int sub(int arg1, int arg2) { return arg1-arg2; } sub: pushl %ebp movl %esp, %ebp movl 12(%ebp), %edx movl 8(%ebp), %eax subl %edx, %eax leave ret 25
Control Structures in Assembly - Revisited 26
For loops int forloop(int x) { int a, s = x; (++ a for (a=0; a<=8; s += s; return s; } 27
For loops int forloop(int x) { int a, s = x; (++ a for (a=0; a<=8; s += s; return s; } int forloop(int x) { int a = 0, s = x; ( 8 > (a if goto done; do { s += s; a++; } while (a <= 8); done: return s; } 28
For loops int forloop(int x) { int a, s = x; (++ a for (a=0; a<=8; s += s; return s; } int forloop(int x) { int a = 0, s = x; ( 8 > (a if goto done; do { s += s; a++; } while (a <= 8); done: return s; } forloop: pushl movl subl movl movl movl.test: cmpl jle jmp.body: movl addl leal incl jmp.done: movl leave ret %ebp %esp, %ebp $8, %esp ( 8(%ebp -,$0 8(%ebp), %eax ( 4(%ebp - %eax, ( 8(%ebp -,$8.body.done -4(%ebp), %eax ( 4(%ebp - %eax, -8(%ebp), %eax ( eax %).test -4(%ebp), %eax 29
Switch statements Two implementation possibilities: Jump tables vs. nested if-else Question: Advantages and disadvantages? 30
Switch statements Two implementation possibilities: Jump tables vs. nested if-else Question: Advantages and disadvantages? Jump tables: constant time If-else: time complexity depends on nb. of cases GCC: Use jump tables when > 4 cases and reasonable range of values 31
Example switch (a) { case 100: a+=1; break; case 102: a+=2; case 103: a+=3; break; case 104: case 106: a+=6; default: a+=7; } Jump table: Fall through! Array with addresses of code segments Index = switch index (here: a) Normalize switch index (here: 0...6 instead of 100...106) 32
Example switch (a) { case 100: a+=1; break; case 102: a+=2; case 103: a+=3; break; case 104: case 106: a+=6; default: a+=7; }.L0: leal -100(%eax), %edx cmpl $6, %edx ja.l8 jmp ( L9(,%edx,4.* normalize and jump to right case jump table.section.rodata.align 4.L9:.long.L3 # a==100.long.l8 # a==101.long.l4 # a==102.long.l5 # a==103.long.l7 # a==104.long.l8 # a==105.text.l3:.l4:.l5:.l7:.l8:.l2:.long incl %eax jmp.l2.l7 # a==106 addl $2, %eax # fall through addl $3, %eax jmp.l2 addl $6, %eax # fall through addl $7, %eax 33
setx instructions int lessthan(x, y) { return x < y; } movl 12(%ebp), %eax cmpl ( 8(%ebp %eax, setl %al # %al = x < y movzbl %al, %eax # fill rest with 0 ret Integer registers: %eax %ah %ax %al Other setx instructions: sete (ZF), setne (~ZF), sets (SF), setns (~SF), 34
Reading condition codes with C EFLAGS register (32 bits): 31 11 7 6 0??? See Intel Architecture Software Developer's Manual (link on course website) 35
Quiz 1. Which of the following C expressions has a result value of 0x01? (a)!0x2a (b)!!0x00 (c)!(~0) (d) 0x0102 & (0xff << 8) (e) 0x0102 && (0xff << 8) 36
Quiz 1. Which of the following C expressions has a result value of 0x01? (a)!0x2a (b)!!0x00 (c)!(~0) (d) 0x0102 & (0xff << 8) (e) 0x0102 && (0xff << 8) 37
Quiz 2. Assuming: 16-bit two s complement integers, Which has the greatest absolute value? (a) 0xffff (b) 0x00ff (c) 0xc000 (d) 0xe000 38
Quiz 2. Assuming: 16-bit two s complement integers, Which has the greatest absolute value? (a) 0xffff (b) 0x00ff (c) 0xc000 (d) 0xe000 39
Quiz 3. Tiny floating-point: sign bit, 2 bit exponent, 2 bit fraction Which bit pattern corresponds to +1 10? (a) 0 00 00 (b) 0 00 01 (c) 0 01 00 (d) 0 01 10 (e) 1 01 00 40
Quiz 3. Tiny floating-point: sign bit, 2 bit exponent, 2 bit fraction Which bit pattern corresponds to +1 10? (a) 0 00 00 (b) 0 00 01 (c) 0 01 00 (d) 0 01 10 (e) 1 01 00 41
Quiz 4. Single-precision floating-point: sign bit, 8 bit exponent, 23 bit fraction Which has the greatest value? (a) 0x00000000 (b) 0x00000001 (c) 0x50000000 (d) 0x60000000 (e) 0xc0000000 42
Quiz 4. Single-precision floating-point: sign bit, 8 bit exponent, 23 bit fraction Which has the greatest value? (a) 0x00000000 (b) 0x00000001 (c) 0x50000000 (d) 0x60000000 (e) 0xc0000000 43
Quiz 5. In assembly code corresponding to a C function, a reference to an operand at memory location -8(%ebp) most likely corresponds to: (a) a local variable declared within that function (b) a global variable declared within that function (c) an incoming parameter value for that function (d) a dynamically allocated variable from the heap 44
Quiz 5. In assembly code corresponding to a C function, a reference to an operand at memory location -8(%ebp) most likely corresponds to: (a) a local variable declared within that function (b) a global variable declared within that function (c) an incoming parameter value for that function (d) a dynamically allocated variable from the heap 45
That's all for today! Questions? 46