User Tools

Site Tools


archi:y86

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
archi:y86 [2017/09/13 09:57] – [If then Else] orelarchi:y86 [2024/03/18 15:06] (current) – external edit 127.0.0.1
Line 60: Line 60:
 </code> </code>
  
-Afin de se rapprocher du code assembleur à écrire, il peut être utile de réécrire le code C de la façon suivante :+Afin de se rapprocher du code assembleur à écrire, il peut être utile de réécrire le code C de la façon suivante en utilisant //une condition d'évitement// :
  
 <code c> <code c>
  long a = 2, b = 3, c;  long a = 2, b = 3, c;
  // (a < b) <=> (a-b < 0) <=> ! (a-b >= 0)  // (a < b) <=> (a-b < 0) <=> ! (a-b >= 0)
- long tmp = a-b; + long tmp = a-b;            // calcul de la condition d'évitement 
- if (tmp >= 0) goto endif;+ if (tmp >= 0) goto _endif// test de la condition d'évitement
  c = 1;  c = 1;
-endif:+_endif:
  
 </code> </code>
Line 107: Line 107:
  if (tmp > 0) goto _else;  if (tmp > 0) goto _else;
  c = 1;  // tmp <= 0, donc la condition b <= a est vraie  c = 1;  // tmp <= 0, donc la condition b <= a est vraie
 + goto _endif;
 _else: _else:
  c = 2;  // tmp > 0, donc la condition b <= a est fausse  c = 2;  // tmp > 0, donc la condition b <= a est fausse
 +_endif:
 </code> </code>
  
Line 681: Line 682:
  
  
 +==== En vrac ====
  
 +<code - td04_exo1.ys>
 +.pos 0
 +
 +irmovl stack, %esp
 +jmp main
 +
 +# f(long *x, long y)
 +f:
 +mrmovl 4(%esp),%eax   # x
 +mrmovl 8(%esp),%ecx   # y
 +rmmovl %ecx,(%eax)    # *x = y
 +ret
 +
 +# main
 +main:
 +
 +mrmovl u, %eax
 +pushl %eax          # empiler 2eme arg (u)
 +irmovl t, %eax
 +pushl %eax          # empiler 1er arg (&t)
 +call f
 +iaddl 8,%esp        # depiler les args
 +
 +halt
 +
 +.pos 0x100
 +t: .long 0
 +u: .long 2
 +
 +.pos 0x200
 +stack:
 +</code>
 +
 +<code - td04_exo2.ys>
 +.pos 0
 +
 +irmovl stack, %esp
 +jmp main
 +
 +# f(long n, long * t)
 +f:
 +mrmovl 4(%esp),%ecx   # n
 +mrmovl 8(%esp),%eax   # t
 +
 +loop:
 +isubl 1, %ecx
 +jl end
 +mrmovl (%eax),%edx    # load *t
 +iaddl 1, %edx         # inc
 +rmmovl %edx, (%eax)   # store *t 
 +iaddl 4,%eax          # t++
 +jmp loop
 +
 +end:
 +ret
 +
 +# main
 +main:
 +irmovl t, %eax
 +pushl %eax          # empiler 2eme arg (adresse t)
 +mrmovl n, %eax
 +pushl %eax          # empiler 1er arg (valeur n)
 +call f
 +iaddl 8,%esp        # depiler les args
 +
 +halt
 +
 +.pos 0x100
 +n: .long 4
 +t: 
 +.long 1
 +.long 2
 +.long 3
 +.long 4
 +
 +
 +.pos 0x200
 +stack:
 +</code>
 +
 +<code - td04_exo3.ys>
 +.pos 0
 +
 +irmovl stack, %esp
 +jmp main
 +
 +# sommme(long n, long v[0], long v[1], ...)
 +f:
 +pushl %ebx            # save callee-saved registry
 +xorl %eax,%eax        # sum = 0
 +mrmovl 8(%esp),%ecx   # n
 +rrmovl %esp,%ebx
 +iaddl 12, %ebx         # v
 +
 +loop:
 +isubl 1, %ecx
 +jl end
 +mrmovl (%ebx),%edx    # load *v
 +addl %edx,%eax        # sum += *v
 +rmmovl %edx, (%ebx)   # store *v
 +iaddl 4,%ebx          # v++
 +jmp loop
 +end:
 +popl %ebx             # restore callee-saved registry
 +ret  # %eax
 +
 +# main
 +main:
 +irmovl 3, %eax
 +pushl %eax          # empiler v2
 +irmovl 2, %eax
 +pushl %eax          # empiler v1
 +irmovl 1, %eax
 +pushl %eax          # empiler v0
 +irmovl 3, %eax
 +pushl %eax          # empiler n
 +call f
 +iaddl 16,%esp        # depiler les args
 +
 +halt
 +
 +.pos 0x100
 +n: .long 4
 +
 +
 +.pos 0x200
 +stack:
 +</code>
archi/y86.1505296664.txt.gz · Last modified: 2024/03/18 15:05 (external edit)