Algorithmen und Datenstrukturen in Chome Algorithmen und Datenstrukturen in C: Parameterübergabe von Strukturen Prof. Dr. Uwe Schmidt FH Wedel

Parameterübergabe von Strukturen

weiter

weiter

call by value <--> call by reference
Beispiel: param.c

   1typedef struct
   2{
   3  double re;
   4  double im;
   5}
   6Complex;
   7
   8Complex
   9times (Complex xComplex y)
  10{
  11  Complex res;
  12
  13  res.re = x.re * y.re + x.im * y.im;
  14  res.im = x.re * y.im + x.im * y.re;
  15
  16  return res;
  17}
  18
  19void
  20mult (Complex * xComplex * yComplex * res)
  21{
  22  res->re = x->re * y->re - x->im * y->im;
  23  res->im = x->re * y->im + x->im * y->re;
  24}
  25
  26Complex a = { 1.0, 1.0 };
  27Complex b = { 2.0, 0.0 };
  28
  29void
  30test1 (void)
  31{
  32  Complex r;
  33  
  34  r = times (ab);
  35}
  36
  37void
  38test2 (void)
  39{
  40  Complex r;
  41
  42  mult (&a&b&r);
  43}
  44
  45void
  46test3 (void)
  47{
  48  /* no comment */
  49  mult (&a&a&a);
  50}
weiter

weiter

Übersetzen

cc -Wall -O -S param.c

weiter

weiter

Der Assembler-Code: param.s

1 times:
2 pushl %ebp
3 movl %esp, %ebp
4 subl $24, %esp
5 movl 8(%ebp), %eax
6 fldl 12(%ebp)
7 fld %st(0)
8 fmull 28(%ebp)
9 fldl 20(%ebp)
10 fld %st(0)
11 fmull 36(%ebp)
12 faddp %st, %st(2)
13 fxch %st(1)
14 fstpl -24(%ebp)
15 fxch %st(1)
16 fmull 36(%ebp)
17 fxch %st(1)
18 fmull 28(%ebp)
19 faddp %st, %st(1)
20 fstpl -16(%ebp)
21 movl -24(%ebp), %edx
22 movl %edx, (%eax)
23 movl -20(%ebp), %edx
24 movl %edx, 4(%eax)
25 movl -16(%ebp), %edx
26 movl %edx, 8(%eax)
27 movl -12(%ebp), %edx
28 movl %edx, 12(%eax)
29 leave
30 ret $4
31 mult:
32 pushl %ebp
33 movl %esp, %ebp
34 movl 8(%ebp), %edx
35 movl 12(%ebp), %eax
36 movl 16(%ebp), %ecx
37 fldl (%edx)
38 fmull (%eax)
39 fldl 8(%edx)
40 fld %st(0)
41 fmull 8(%eax)
42 fsubrp %st, %st(2)
43 fxch %st(1)
44 fstpl (%ecx)
45 fldl (%edx)
46 fmull 8(%eax)
47 fxch %st(1)
48 fmull (%eax)
49 faddp %st, %st(1)
50 fstpl 8(%ecx)
51 popl %ebp
52 ret
53 a:
54 b:
55 test1:
56 pushl %ebp
57 movl %esp, %ebp
58 subl $36, %esp
59 leal -24(%ebp), %eax
60 pushl b+12
61 pushl b+8
62 pushl b+4
63 pushl b
64 pushl a+12
65 pushl a+8
66 pushl a+4
67 pushl a
68 pushl %eax
69 call times
70 leave
71 ret
72 test2:
73 pushl %ebp
74 movl %esp, %ebp
75 subl $28, %esp
76 leal -24(%ebp), %eax
77 pushl %eax
78 pushl $b
79 pushl $a
80 call mult
81 leave
82 ret
83 test3:
84 pushl %ebp
85 movl %esp, %ebp
86 subl $12, %esp
87 pushl $a
88 pushl $a
89 pushl $a
90 call mult
91 leave
92 ret
weiter

Letzte Änderung: 11.01.2007
© Prof. Dr. Uwe Schmidt
Prof. Dr. Uwe Schmidt FH Wedel