TP4 : Structures de données

Listes

irmovl Stack, %esp call main halt # sum(struct cell *l) { # struct cell *p = l; # long sum = 0; # while (p != NULL) { # sum += p->value; # p = p->next; # } # } sum: mrmovl 4(%esp),%edx # edx: p = l irmovl 0,%eax # eax: sum = 0 loop: andl %edx,%edx # p == NULL ? je fin mrmovl 0(%edx),%ecx # sum += p->value addl %ecx,%eax mrmovl 4(%edx),%edx # p = p->next jmp loop fin: # sum est déjà dans eax ret # main() { sum(&l1); } main: irmovl l1,%eax # adresse début liste pushl %eax call sum # appel de la fonction popl %ebx # nettoyage paramètre ret .pos 0x100 l1: .long 1 # champ value .long l2 # champ next l2: .long 2 .long l3 l3: .long 3 .long l4 l4: .long 4 .long 0 # terminaison avec NULL .pos 0x200 Stack:

Arbres

irmovl Stack, %esp call main halt # long sum(struct node *n) { # long s; # if (n == NULL) # return 0; # s = n->value; # s += sum(n->left); # return s + sum(n->right); # } sum: # Sauvegarder deux registres pour s et n pushl %ebx # ebx est callee-saved pushl %esi # esi aussi mrmovl 12(%esp), %esi # n andl %esi,%esi jne calcul xorl %eax,%eax jmp fin # return 0; calcul: mrmovl 0(%esi), %ebx # s = n->value; mrmovl 4(%esi), %ecx # n->left pushl %ecx call sum addl %eax, %ebx # s += sum(n->left); mrmovl 8(%esi), %ecx # n->right rmmovl %ecx,(%esp) call sum addl %ebx, %eax # return s + sum(n->left); popl %ebx # jeter le paramètre fin: popl %esi popl %ebx ret main: irmovl a,%eax pushl %eax call sum halt .pos 0x100 a: .long 4 # champ value .long b # champ left .long c # champ right b: .long 2 .long 0 .long 0 c: .long 3 .long d .long e d: .long 5 .long 0 .long f e: .long 3 .long 0 .long 0 f: .long 6 .long 0 .long 0 .pos 0x200 Stack: