archi:y86
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| archi:y86 [2017/09/13 09:50] – [If then] orel | archi:y86 [2024/03/18 15:06] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 60: | Line 60: | ||
| </ | </ | ||
| - | 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 |
| <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' |
| - | if (tmp >= 0) goto endif; | + | if (tmp >= 0) goto _endif; // test de la condition d' |
| - | c = 1; | + | c = 1; |
| - | endif: | + | _endif: |
| </ | </ | ||
| Line 99: | Line 99: | ||
| if (b <= a) c = 1; else c = 2; | if (b <= a) c = 1; else c = 2; | ||
| </ | </ | ||
| + | |||
| + | Un peu de réécriture du code C : | ||
| + | |||
| + | <code c> | ||
| + | long a = 2, b = 3, c; | ||
| + | long tmp = b - a; | ||
| + | if (tmp > 0) goto _else; | ||
| + | c = 1; // tmp <= 0, donc la condition b <= a est vraie | ||
| + | goto _endif; | ||
| + | _else: | ||
| + | c = 2; // tmp > 0, donc la condition b <= a est fausse | ||
| + | _endif: | ||
| + | </ | ||
| + | |||
| + | Avant de traduire cela en assembleur : | ||
| <code - ifthenelse.ys> | <code - ifthenelse.ys> | ||
| Line 667: | Line 682: | ||
| + | ==== En vrac ==== | ||
| + | <code - td04_exo1.ys> | ||
| + | .pos 0 | ||
| + | |||
| + | irmovl stack, %esp | ||
| + | jmp main | ||
| + | |||
| + | # f(long *x, long y) | ||
| + | f: | ||
| + | mrmovl 4(%esp), | ||
| + | mrmovl 8(%esp), | ||
| + | rmmovl %ecx, | ||
| + | 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, | ||
| + | |||
| + | halt | ||
| + | |||
| + | .pos 0x100 | ||
| + | t: .long 0 | ||
| + | u: .long 2 | ||
| + | |||
| + | .pos 0x200 | ||
| + | stack: | ||
| + | </ | ||
| + | |||
| + | <code - td04_exo2.ys> | ||
| + | .pos 0 | ||
| + | |||
| + | irmovl stack, %esp | ||
| + | jmp main | ||
| + | |||
| + | # f(long n, long * t) | ||
| + | f: | ||
| + | mrmovl 4(%esp), | ||
| + | mrmovl 8(%esp), | ||
| + | |||
| + | loop: | ||
| + | isubl 1, %ecx | ||
| + | jl end | ||
| + | mrmovl (%eax), | ||
| + | iaddl 1, %edx # inc | ||
| + | rmmovl %edx, (%eax) | ||
| + | iaddl 4, | ||
| + | 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, | ||
| + | |||
| + | halt | ||
| + | |||
| + | .pos 0x100 | ||
| + | n: .long 4 | ||
| + | t: | ||
| + | .long 1 | ||
| + | .long 2 | ||
| + | .long 3 | ||
| + | .long 4 | ||
| + | |||
| + | |||
| + | .pos 0x200 | ||
| + | stack: | ||
| + | </ | ||
| + | |||
| + | <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, | ||
| + | mrmovl 8(%esp), | ||
| + | rrmovl %esp,%ebx | ||
| + | iaddl 12, %ebx # v | ||
| + | |||
| + | loop: | ||
| + | isubl 1, %ecx | ||
| + | jl end | ||
| + | mrmovl (%ebx), | ||
| + | addl %edx, | ||
| + | rmmovl %edx, (%ebx) | ||
| + | iaddl 4, | ||
| + | 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, | ||
| + | |||
| + | halt | ||
| + | |||
| + | .pos 0x100 | ||
| + | n: .long 4 | ||
| + | |||
| + | |||
| + | .pos 0x200 | ||
| + | stack: | ||
| + | </ | ||
archi/y86.1505296225.txt.gz · Last modified: 2024/03/18 15:05 (external edit)
