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 Specification et Index of Instructions
Pour cet TP, nous utiliserons l'utilitaire was. Was est capable d'assembler, de désassembler et d'exécuter des sources wasm. Veuillez lire le fichier readme.md.
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)
local.get $a
local.get $b
i32.add
return
)
)
int name () { int a; a = 0; return a; }
(module
(func $name (local $a i32) (result i32)
i32.const 0;
local.set $a
local.get $a
return
)
)
(module
(import "io" "print" (func $print (param i32)))
(func $start
i32.const 120
call $print)
(export "start" (func $start))
)
fetch('../out/main.wasm').then(response => response.arrayBuffer() ).then(bytes => WebAssembly.instantiate(bytes, { io: { print: console.log } })).then(results => { let instance = results.instance; // instance is your webassembly module let asm = instance.exports; // asm is an object with all the items that you exported console.log (asm.start()); }).catch(console.error);
if (a>b) { print (a); } else { print (b); }
;; import the print function local.get $a local.get $b i32.gt if local.get $a call $print else local.get $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
local.get $a
i32.const 120
i32.gt
br_if $endwhile
local.get $a
call $print
local.get $a
i32.const 1
i32.add
local.set $a
br $while
end $while
end $endwhile
for (i=1; i<120; i++) { print (a); }
;; import the print function
i32.const 1
local.set $i
block $endfor
loop $for
local.get $i
i32.const 120
i32.gt
br_if $endfor
local.get $i
call $print
local.get $i
i32.const 1
i32.add
local.set $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 local.set $a loop $dowhile local.get $a call $print local.get $a i32.const 1 i32.add local.set $a local.get $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 le project.
a+b%c.