procedure call and array

79
1 Procedure Call and Array

Upload: art

Post on 05-Jan-2016

29 views

Category:

Documents


1 download

DESCRIPTION

Procedure Call and Array. Outline. Data manipulation Control structure Suggested reading Chap 3.7, 3.8. Procedure. In high level programming languages Parameter passing (actual vs. formal) Return value Control transfer Allocate space for local variables on entry - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Procedure Call and Array

1

Procedure Calland Array

Page 2: Procedure Call and Array

2

Outline

• Data manipulation

• Control structure

• Suggested reading

– Chap 3.7, 3.8

Page 3: Procedure Call and Array

3

Procedure

• In high level programming languages

– Parameter passing (actual vs. formal)

– Return value

– Control transfer

– Allocate space for local variables on entry

– Deallocate space for local variables on exit

Page 4: Procedure Call and Array

4

Procedure Implementation

• Stack frame

• A fixed register is used to hold the return

value

• Simple instructions for control transferring

• Register usage

• Calling convention

Convention:约定Frame: 帧,结构

Page 5: Procedure Call and Array

5

Call Chain & Stack Frames

Main

{

C();

A() ;

}

A()

{

B() ;

}

B()

{

C() ;

}

Page 6: Procedure Call and Array

6

Call Chain & Stack Frames

Main

{

C();

A() ;

}

A()

{

B() ;

}

B()

{

C() ;

}

Stack frame for main()

Page 7: Procedure Call and Array

7

Call Chain & Stack Frames

Main

{

C();

A() ;

}

A()

{

B() ;

}

B()

{

C() ;

}

Stack frame for main()

Stack frame for C()

Page 8: Procedure Call and Array

8

Call Chain & Stack Frames

Main

{

C();

A() ;

}

A()

{

B() ;

}

B()

{

C() ;

}

Stack frame for main()

Stack frame for A()

Page 9: Procedure Call and Array

9

Call Chain & Stack Frames

Main

{

C();

A() ;

}

A()

{

B() ;

}

B()

{

C() ;

}

Stack frame for main()

Stack frame for A()

Stack frame for B()

Page 10: Procedure Call and Array

10

Call Chain & Stack Frames

Main

{

C();

A() ;

}

A()

{

B() ;

}

B()

{

C() ;

}

Stack frame for main()

Stack frame for A()

Stack frame for B()

Stack frame for C()

Page 11: Procedure Call and Array

11

Frame Structure

• A stack frame is delimited by

– The frame pointer %ebp

– The stack pointer %esp

• The stack pointer can move when

the procedure is executing (%esp )

• Frame pointer relative accessing

(%ebp )

%ebp

%esp

Page 12: Procedure Call and Array

12

Frame Structure

• Frames are chained

%ebp

%esp

Old ebp

Old ebp

Old ebp

Page 13: Procedure Call and Array

13

Caller and Callee

Caller(){

call callee (argument1, … , argumentn);

}

Page 14: Procedure Call and Array

14

Register Usage

• Caller saved registers– %eax, %edx, %ecx

– Saved by caller

– Callee can use these registers freely

– The contents in these registers may be changed after return

– Caller must restore them if it tries to use them after calling

Page 15: Procedure Call and Array

15

Register Usage

• Callee saved registers

– %ebx, %esi, %edi

– Callee must save them before using

– Callee must restore them before return

Page 16: Procedure Call and Array

16

Calling Convention

• Save caller save registers (%eax, %edx,

%ecx)

• Push actual arguments from right to left

• Call instruction

– Save return address

– Transfer control to callee

Page 17: Procedure Call and Array

17

Passing Parameters

pushl argument n

………Argument n

%esp

Page 18: Procedure Call and Array

18

Passing Parameters

pushl argument n

……………

pushl argument 1

………Argument n

………Argument 1

%esp

Page 19: Procedure Call and Array

19

Passing Parameters

pushl argument n

……………

pushl argument 1

call callee

………Argument n

………Argument 1Return address

%esp

Page 20: Procedure Call and Array

20

Call Instruction P172

• call label (direct)

• call *operand (indirect)

• Save return address in the stack

• Jump to the entry to callee

Page 21: Procedure Call and Array

21

Initialize Stack frame

pushl %ebpmovl %esp, %ebp

save callee saved register

adjust %esp to allocate space for local variables and temporaries

Page 22: Procedure Call and Array

22

Destruct a Frame

restore callee saved registers movl %ebp, %esp popl %ebp retIntegeral return value is in the %eax

restore caller saved registers leave ret

Page 23: Procedure Call and Array

23

Example P175 Figure 3.18

1 int swap_add(int *xp, int *yp)

2 {

3 int x = *xp;

4 int y = *yp;

5

6 *xp = y;

7 *yp = x;

8 return x + y;

9 }

10

Page 24: Procedure Call and Array

24

Example P175 Figure 3.18

11 int caller()12 {13 int arg1 = 534;14 int arg2 = 1057;15 int sum = swap_add(&arg1, &arg2);16 int diff = arg1 - arg2;1718 return sum * diff;19 }

Page 25: Procedure Call and Array

25

Example

0 Saved %ebp

-4 arg2(1057)

-8 arg1(534)

-12

Stack frame for callerBefore

int sum = swap_add(&arg1, &arg2);%ebp

%esp

Page 26: Procedure Call and Array

26

Parameter Passing P174

0 Saved %ebp

-4 arg2(1057)

-8 arg1(534)

-12 &arg2

Stack frame for caller1 leal -4(%ebp),%eax

Compute &arg2

2 pushl %eax

Push &arg2

%ebp

%esp

Page 27: Procedure Call and Array

27

Parameter Passing P174

0 Saved %ebp

-4 arg2(1057)

-8 arg1(534)

-12 &arg2

-16 &arg1

Stack frame for caller1 leal -4(%ebp),%eax

Compute &arg2

2 pushl %eax

Push &arg2

3 leal -8(%ebp),%eax

Compute &arg1

4 pushl %eax

Push &arg1

%ebp

%esp

Page 28: Procedure Call and Array

28

Call Instruction P174

0 Saved %ebp

-4 arg2(1057)

-8 arg1(534)

-12 &arg2

-16 &arg1

-20 Return Addr

Stack frame for caller1 leal -4(%ebp),%eax

Compute &arg2

2 pushl %eax

Push &arg2

3 leal -8(%ebp),%eax

Compute &arg1

4 pushl %eax

Push &arg1

5 call swap_add

Call the swap_add function

%ebp

%esp

Page 29: Procedure Call and Array

29

Call Instruction P174

0 Saved %ebp

-4 arg2(1057)

-8 arg1(534)

-12 &arg2

-16 &arg1

-20 Return Addr

Stack frame for caller1 leal -4(%ebp),%eax

Compute &arg2

2 pushl %eax

Push &arg2

3 leal -8(%ebp),%eax

Compute &arg1

4 pushl %eax

Push &arg1

5 call swap_add

Call the swap_add function

%ebp

%esp

Page 30: Procedure Call and Array

30

Setup code in swap_add P176

24 Saved %ebp

20 arg2(1057)

16 arg1(534)

12 yp(&arg2)

8 xp(&arg1)

4 Return Addr

0 Saved %ebp

Stack frame for callerswap_add:

1 pushl %ebp Save old %ebp

%ebp

%esp

Page 31: Procedure Call and Array

31

Setup code in swap_add

24 Saved %ebp

20 arg2(1057)

16 arg1(534)

12 yp(&arg2)

8 xp(&arg1)

4 Return Addr

0 Saved %ebp

Stack frame for callerswap_add:

1 pushl %ebp Save old %ebp

2 movl %esp,%ebp

Set %ebp as frame pointer

%ebp

%esp

Stack frame for swap_add

Page 32: Procedure Call and Array

32

Setup code in swap_add

24 Saved %ebp

20 arg2(1057)

16 arg1(534)

12 yp(&arg2)

8 xp(&arg1)

4 Return Addr

0 Saved %ebp

-4 Saved %ebx

Stack frame for callerswap_add:

1 pushl %ebp Save old %ebp

2 movl %esp,%ebp

Set %ebp as frame pointer

3 pushl %ebx

Save %ebx

%esp

%ebp

Stack frame for swap_add

Page 33: Procedure Call and Array

33

Body code in swap_add

24 Saved %ebp

20 arg2(1057)

16 arg1(534)

12 yp(&arg2)

8 xp(&arg1)

4 Return Addr

0 Saved %ebp

-4 Saved %ebx

Stack frame for caller5 movl 8(%ebp),%edx Get xp

%esp

%ebp

Stack frame for swap_add

xp(=&arg1=%ebp+16)%edx

Page 34: Procedure Call and Array

34

Body code in swap_add

24 Saved %ebp

20 arg2(1057)

16 arg1(534)

12 yp(&arg2)

8 xp(&arg1)

4 Return Addr

0 Saved %ebp

-4 Saved %ebx

Stack frame for caller5 movl 8(%ebp),%edx Get xp

6 movl 12(%ebp),%ecx Get yp

%esp

%ebp

Stack frame for swap_add

xp(=&arg1=%ebp+16)%edx

yp(=&arg2=%ebp+20)%ecx

Page 35: Procedure Call and Array

35

Body code in swap_add

24 Saved %ebp

20 arg2(1057)

16 arg1(534)

12 yp(&arg2)

8 xp(&arg1)

4 Return Addr

0 Saved %ebp

-4 Saved %ebx

Stack frame for caller5 movl 8(%ebp),%edx Get xp

6 movl 12(%ebp),%ecx Get yp

7 movl (%edx),%ebx Get x

8 movl (%ecx),%eax Get y

%esp

%ebp

Stack frame for swap_add

xp(=&arg1=%ebp+16)%edx

yp(=&arg2=%ebp+20)%ecx

534 %ebx

1057 %eax

Page 36: Procedure Call and Array

36

Body code in swap_add

24 Saved %ebp

20 arg2(1057)

16 arg1(1057)

12 yp(&arg2)

8 xp(&arg1)

4 Return Addr

0 Saved %ebp

-4 Saved %ebx

Stack frame for caller9 movl %eax, (%edx) Store y at *xp

%esp

%ebp

Stack frame for swap_add

xp(=&arg1=%ebp+16)%edx

yp(=&arg2=%ebp+20)%ecx

534 %ebx

1057 %eax

Page 37: Procedure Call and Array

37

Body code in swap_add

24 Saved %ebp

20 arg2(534)

16 arg1(1057)

12 yp(&arg2)

8 xp(&arg1)

4 Return Addr

0 Saved %ebp

-4 Saved %ebx

Stack frame for caller9 movl %eax, (%edx) Store y at *xp

10 movl %ebx, (%ecx) Store x at

*yp

%esp

%ebp

Stack frame for swap_add

xp(=&arg1=%ebp+16)%edx

yp(=&arg2=%ebp+20)%ecx

534 %ebx

1057 %eax

Page 38: Procedure Call and Array

38

Body code in swap_add

24 Saved %ebp

20 arg2(534)

16 arg1(1057)

12 yp(&arg2)

8 xp(&arg1)

4 Return Addr

0 Saved %ebp

-4 Saved %ebx

Stack frame for caller9 movl %eax, (%edx) Store y at *xp

10 movl %ebx, (%ecx) Store x at

*yp

11 addl %ebx,%eax Set return value = x+y

%esp

%ebp

Stack frame for swap_add

xp(=&arg1=%ebp+16)%edx

yp(=&arg2=%ebp+20)%ecx

534 %ebx

1591 %eax

Page 39: Procedure Call and Array

39

Finishing code in swap_add

24 Saved %ebp

20 arg2(534)

16 arg1(1057)

12 yp(&arg2)

8 xp(&arg1)

4 Return Addr

0 Saved %ebp

-4 Saved %ebx

Stack frame for caller12 popl %ebx Restore %ebx

%esp

%ebp

Stack frame for swap_add

xp(=&arg1=%ebp+16)%edx

yp(=&arg2=%ebp+20)%ecx

original value %ebx

1591 %eax

Page 40: Procedure Call and Array

40

Finishing code in swap_add

24 Saved %ebp

20 arg2(534)

16 arg1(1057)

12 yp(&arg2)

8 xp(&arg1)

4 Return Addr

0 Saved %ebp

-4 Saved %ebx

Stack frame for caller12 popl %ebx Restore %ebx

13 movl %ebp, %esp Restore %esp

%esp

%ebp

Stack frame for swap_add

xp(=&arg1=%ebp+16)%edx

yp(=&arg2=%ebp+20)%ecx

original value %ebx

1591 %eax

Page 41: Procedure Call and Array

41

Finishing code in swap_add

0 Saved %ebp

-4 arg2(534)

-8 arg1(1057)

-12 yp(&arg2)

-16 xp(&arg1)

-20 Return Addr

Saved %ebp

Saved %ebx

Stack frame for caller12 popl %ebx Restore %ebx

13 movl %ebp, %esp Restore %esp

14 popl %ebp Restore %ebp

%ebp

xp(=&arg1=%ebp+16)%edx

yp(=&arg2=%ebp+20)%ecx

original value %ebx

1591 %eax

%esp

Page 42: Procedure Call and Array

42

Finishing code in swap_add

0 Saved %ebp

-4 arg2(534)

-8 arg1(1057)

-12 yp(&arg2)

-16 xp(&arg1)

-20 Return Addr

Saved %ebp

Saved %ebx

Stack frame for caller12 popl %ebx Restore %ebx

13 movl %ebp, %esp Restore %esp

14 popl %ebp Restore %ebp

15 ret Return to caller

• Call by value

%ebp

xp(=&arg1=%ebp+16)%edx

yp(=&arg2=%ebp+20)%ecx

original value %ebx

1591 %eax

%esp

Page 43: Procedure Call and Array

43

Recursion P178 Figure 3.20

1 int fib_rec(int n)

2 {

3 int prev_val, val;

4

5 if (n <= 2)

6 return 1;

7 prev_val = fib_rec(n-2);

8 val = fib_rec(n-1);

9 return prev_val + val;

10 }

Page 44: Procedure Call and Array

44

Setup code P179 Figure 3.21

1 fib_rec:

2 pushl %ebp Save old %ebp

3 movl %esp,%ebp Set %ebp as frame pointer

4 subl $16,%esp Allocate 16 bytes on stack

5 pushl %esi Save %esi (offset -20)

6 pushl %ebx Save %ebx (offset -24)

Page 45: Procedure Call and Array

45

Recursion

.

.

.

.

+8 n

+4 Return Addr

0 Saved %ebp

Unused-20 Saved %esi

-24 Saved %ebx

Stack frame

%ebp

%esp

Page 46: Procedure Call and Array

46

Body code

7 movl 8(%ebp),%ebx Get n

8 cmpl $2,%ebx Compare n:2

9 jle .L24 if <=, goto terminate

10 addl $-12,%esp Allocate 12 bytes on

stack

11 leal -2(%ebx),%eax Compute n-2

12pushl %eax Push as argument

13call fib_rec Call fib_rec(n-2)

Page 47: Procedure Call and Array

47

Recursion

.

.

.

.

+8 n

+4 Return Addr

0 Saved %ebp

Unused-20 Saved %esi

-24 Saved %ebx

Unused -40 n-2

Stack frame

%ebp

%esp

Page 48: Procedure Call and Array

48

Body code

14 movl %eax,%esi Store result in %esi

15 addl $-12,%esp Allocate 12 bytes to

stack

16 leal -1(%ebx),%eax Compute n-1

17 pushl %eax Push as argument

18 call fib_rec Call fib_rec(n-1)

19 addl %esi,%eax Compute val+nval

20 jmp .L25 Go to done

Page 49: Procedure Call and Array

49

Terminal condition

21 .L24: terminate:

22 movl $1,%eax Return value 1

Page 50: Procedure Call and Array

50

Finishing code

23 .L25: done:

24 leal -24(%ebp),%esp Set stack to offset -24

25 popl %ebx Restore %ebx

26 popl %esi Restore %esi

27 movl %ebp,%esp Restore stack pointer

28 popl %ebp Restore %ebp

29 ret Return

Page 51: Procedure Call and Array

51

ArraySuggested reading

Chap 3.8

Page 52: Procedure Call and Array

52

Outline

• Aggregate scalar data into larger data

• Pointers vs. Array

• Array subscript and pointer arithmetic

• Memory layout for array

– Static vs. dynamic

• Compiler optimization for array operations

Page 53: Procedure Call and Array

53

Array declaration

• T A[N] ;

• Allocate a contiguous region in memory

– The size of the region is sizeof(T) * N bytes

* Contiguous: 临近的

Page 54: Procedure Call and Array

54

Starting Address of an Array

• The starting address of an array A

– is denoted as XA

• Identifier A can be used as

– A pointer to the beginning of the array

– Its value is XA

Page 55: Procedure Call and Array

55

Accessing Array

• Array elements can be accessed

– Using an integer index ranging between 0 and

N-1

• Array element i is stored at address

– XA + sizeof(T) * i

Page 56: Procedure Call and Array

56

Array

0 1 2 3 4 5 6 7 8 9 10 11

0 4 8 12 16

char a[12] ;

xa xa+4 xa+8

char *b[5] ;

xb xb+4 xb+8 xb+12 xb+16

指针数组

Page 57: Procedure Call and Array

57

Array

0 4 8 12 16

double c[2] ;

xc xc+8

double *d[5] ;

xd xd+4 xd+8 xd+12 xd+16

指针数组

Page 58: Procedure Call and Array

58

Pointer Arithmetic

• Addition and subtraction– p+i , p-i (result is a pointer)

– p-q (result is an int)

• Referencing & dereferencing– *p (value), &E (address)

• Subscription– A[i], *(A+i)

Page 59: Procedure Call and Array

59

Memory referencing instruction

• E is an array of int’s

– Address of E is stored in register %edx

– Index i is stored in register %ecx

• The array element E[i] is translated into

– movl (%edx, %ecx, 4), %eax

Page 60: Procedure Call and Array

60

Pointer Arithmetic P182

Expression Type Value Assembly code

E int * xE movl %edx, %eax

E[0] int M[xE] movl (%edx), %eax

E[i] int M[xE+4i] movl (%edx, %ecx, 4), %eax

&E[2] int * xE+8 leal 8(%edx,) %eax

E+i-1 int * xE+4i-4 leal -4(%edx, %ecx, 4), %eax

*(&E[i]+i) int M[xE+4i+4i]

movl (%edx, %ecx, 8), %eax

&E[i]-E int i movl %ecx, %eax

Page 61: Procedure Call and Array

61

Optimization P184 Figure 3.23(a)

1 int decimal5(int *x)2 {3 int i;4 int val = 0;56 for (i = 0; i < 5; i++)7 val = (10 * val) + x[i];89 return val;10 }

Page 62: Procedure Call and Array

62

Optimization P184 Figure 3.23(b)

1 int decimal5_opt(int *x)2 {3 int val = 0;4 int *xend = x + 4;56 do {7 val = (10 * val) + *x;8 x++;9 } while (x <= xend);1011 return val;12 }

Page 63: Procedure Call and Array

63

Optimization P184 Figure 3.23(c)

1 movl 8(%ebp),%ecx Get base addr of array x

2 xorl %eax,%eax val = 0;

3 leal 16(%ecx),%ebx xend = x+4 (16 bytes = 4 double words)

4 .L12: loop:

5 leal (%eax,%eax,4),%edx Compute 5*val

6 movl (%ecx),%eax Compute *x

7 leal (%eax,%edx,2),%eax Compute *x + 2*(5*val)

8 addl $4,%ecx x++

9 cmpl %ebx,%ecx Compare x:xend

10 jbe .L12 if <=, goto loop:

避免乘法

Page 64: Procedure Call and Array

64

Nested Array P183

• int A[4][3] ;

• Array of array– typedef int row3_t[3] ;

– row3_t A[4] ;

– Array A contains 4 elements, each requiring 12 bytes to store 3 integers

– The whole size of array A is 48 bytes

Nested: 嵌套

Page 65: Procedure Call and Array

65

Nested Array

• int A[4][3] ;

– Array A is a two-dimensional array

• with four rows and three columns

– It is referenced as A[0][0] through A[3][2]

– Row major ordered in memory

Page 66: Procedure Call and Array

66

Nested Array P185

Element Address

A[0][0] xA

A[0][1] xA+4

A[0][2] xA+8

A[1][0] xA+12

A[1][1] xA+16

A[1][2] xA+20

A[2][0] xA+24

A[2][1] xA+28

A[2][2] xA+32

A[3][0] xA+36

A[3][1] xA+40

A[3][2] xA+44

Page 67: Procedure Call and Array

67

Nested Array

• T D[R][C] ;

• D[i][j] is at memory address

– xD + L* ( C * i + j )

– L is sizeof(T)

Page 68: Procedure Call and Array

68

Access A[i,j] P185

• It is in memory M [ xA +j*4+ i * 12 ]

• %eax contains xA

• %edx holds i, %ecx holds j

• sall $2, %ecx j*4• leal (%edx, %edx, 2), %edx i*3• leal (%ecx, %edx, 4) j*4+

i*12• movl (%eax, %ecx), %eax

Page 69: Procedure Call and Array

69

Optimization P186-P187

1 #define N 162 typedef int fix_matrix[N][N];34 /* Compute i,k of fixed matrix product */5 int fix_prod_ele (fix_matrix A, fix_matrix B, int

i, int k)6 {7 int j;8 int result = 0;910 for (j = 0; j < N; j++)11 result += A[i][j] * B[j][k];1213 return result;14 }

Page 70: Procedure Call and Array

70

Observations

• The loop will access the elements of array A– as A[i][0], A[i][1], …, A[i][15] in sequence

• These elements occupy adjacent positions in memory – starting with the address of array A[i][0]

• Use a pointer variable Aptr – to access these successive locations.

Page 71: Procedure Call and Array

71

Observations

• The loop will access the elements of array B– as B[0][k], B[1][k], …, B[15][k] in sequence

• These elements occupy positions in memory– starting with the address of array B[0][i]– and space 64 bytes apart.

• Use a pointer variable Bptr – to access these locations.

• Use a simple counter – to keep track of the number of iterations required

Page 72: Procedure Call and Array

72

Optimization P187

1 /* Compute i,k of fixed matrix product */2 int fix_prod_ele_opt (fix_matrix A, fix_matrix B,

int i, int k)3 {4 int *Aptr = &A[i][0], *Bptr = &B[0][k];5 int cnt = N – 1, result = 0;67 do {8 result += (*Aptr) * (*Bptr);9 Aptr += 1;10 Bptr += N;11 cnt--;12 } while (cnt >= 0);1314 return result;15 }

Page 73: Procedure Call and Array

73

Optimization

Aptr is in %edx, Bptr in %ecx, result in %esi, cnt in %ebx

1 .L23: loop:

2 movl (%edx),%eax Compute t = *Aptr

3 imull (%ecx),%eax Compute v = *Bptr * t

4 addl %eax,%esi Add v result

5 addl $64,%ecx Add 64 to Bptr

6 addl $4,%edx Add 4 to Aptr

7 decl %ebx Decrement cnt

8 jns .L23 if >=, goto loop

Page 74: Procedure Call and Array

74

Dynamic Array P188

• Code required to

– Work for arbitrary size arrays

– Dynamically allocated

– Explicitly encode the mapping of multidimensional

arrays into one-dimensional ones

– Define a data type var_matrix as simply an int *:

•typedef int *var_matrix;

Page 75: Procedure Call and Array

75

Allocate and Initialize

• Allocate and initialize storage for an nn array of integers

1 var_matrix new_var_matrix (int n)

2 {

3 return (var_matrix) calloc

( sizeof(int), n * n );

4 }

Page 76: Procedure Call and Array

76

Indexing P189

• Row major computation1 int var_ele (var_matrix A, int i, int j, int n)

2 {3 return A[(i*n) + j];4 }

1 movl 8(%ebp),%edx Get A2 movl 12(%ebp),%eax Get i3 imull 20(%ebp),%eax Compute n*i4 addl 16(%ebp),%eax Compute n*i + j5 movl (%edx,%eax,4),%eax Get A[i*n

+ j]

Page 77: Procedure Call and Array

77

Optimization P190 Figure 3.25(a)

1 typedef int *var_matrix;23 /* Compute i,k of variable matrix product */4 int var_prod_ele (var_matrix A, var_matrix B, int i,

int k, int n)5 {6 int j;7 int result = 0;89 for (j = 0; j < n; j++)10 result += A[i*n + j] * B[j*n + k];1112 return result;13 }

Page 78: Procedure Call and Array

78

Optimization P190 Figure 3.25(b)

1 /* Compute i,k of variable matrix product */2 int var_prod_ele_opt (var_matrix A, var_matrix B, int i,

int k, int n)3 {4 int *Aptr = &A[i*n], nTjPk = k;5 int cnt = n, result = 0;67 if (n <= 0)8 return result;910 do {11 result += (*Aptr) * B[nTjPk];12 Aptr += 1;13 nTjPk += n;14 cnt--;15 } while (cnt);1617 return result;18 }

Page 79: Procedure Call and Array

79

Optimization P191

1 .L37: loop:

2 movl 12(%ebp), %eax Get B

3 movl (%ebx), %edi Get *Aptr

4 addl $4, %ebx Increment Aptr

5 imull (%eax,%ecx,4), %edi Multiply by B[nTjPk]

6 addl %edi, %esi Add to result

7 addl 24(%ebp), %ecx Add n to nTjPk

8 decl %edx Decrement cnt

9 jnz .L37 If cnt <> 0, goto loop

Register spilling