This is an old revision of the document!
Avant de commencer le TP, lisez le tutoriel suivant: Mozilla Web Assembly Tutorial.
Pour plus d'informations, la documentation de Web Assembly est disponible ici: WebAssembly Semantics
Pour tester les exemples présentés dans ce TP, on va utiliser la page destiné a Web Assembly : wat2wasm
Web Assembly peut etre utilisé en Javascript de la maniere suivante:
const wasmInstance = new WebAssembly.Instance(wasmModule, { io:{ print: console.log } });
Un exemple de web assembly est:
(module (import "io" "print" (func $print (param $s i32))) (func $start i32.const 120 call $print ) (start $start) )
Le programme ci-dessus va afficher sur l'écran la valeur 120
.
int main () { return 0; }
(module (func $start ) (start $start) )
int sum (int a, int b) { }
(module (func $sum (param $a i32) (param $b i32) (result i32) get_local $a get_local $b i32.add return ) )
int name () { int a; a = 0; return a; }
(module (func $name (local $a i32) (result i32) i32.const 0; set_local $a get_local $a return ) )
(module (import "io" "print" (func $print (param $n i32)) ;; use the print function i32.const 120 call $print )
if (a>b) { print (a); } else { print (b); }
;; import the print function get_local $a get_local $b i32.gt if get_local $a call $print else get_local $b call $print end
a=1; while (a<120) { print (a); a=a+1 }
;; import the print function i32.const 1 set_local $a block $endwhile loop $while get_local $a i32.const 120 i32.gt br_if $endwhile get_local $a call $print get_local $a i32.const 1 i32.add set_local $a br $while end $while end $endwhile
for (i=1; i<120; i++) { print (a); }
;; import the print function i32.const 1 set_local $i block $endfor loop $for get_local $i i32.const 120 i32.gt br_if $endfor get_local $i call $print get_local $i i32.const 1 i32.add set_local $i br $for end $for end $endfor
a=1; do { print (a); a=a+1 } while (a<=120);
;; import the print function i32.const 1 set_local $a loop $dowhile get_local $a call $print get_local $a i32.const 1 i32.add set_local $a get_local $a i32.const 120 i32.le br_if $dowhile end $dowhile
Pour les exercices suivants, vous pouvez écrire et envoyer le code source dans des fichiers texte. Vous devez ajouter aussi le code WAT, que le code JavaScript. Pour tester la validité de vos programmes, vous devez utiliser wat2wasm.
a+b/c
. (1p)x/3
. Dans la fonction start, appelez la fonction is_divisible avec le parametre 6. (1p)