This is an old revision of the document!


TP 11 - WebAssembly

Syntax - forme text

Lisez le tutorial a Mozilla Web Assembly Tutorial.

La documentation de Web Assembly est disponible a WebAssembly Semantics

Utilisation de Web Assembly

On va utilizes la page pour Web Assembly a wat2wasm

Web Assembly est utilisé à partir de Javascript.

Pour la partie de Javascript, ecrivez le code suivante

const wasmInstance =
      new WebAssembly.Instance(wasmModule, {
         io:{
           print: console.log
         }
      });

Un example de web assembly est:

(module
  (import "io" "print" (func $print (param $s i32)))
  (func $start
    	i32.const 120
    	call $print
    )
  (start $start)
 )

Trasnlation de code en Web Assembly

Program Principal

int main ()
{
  return 0;
}
(module
  (func $start 
    )
  (start $start)
)

Fonction

int sum (int a, int b)
{
}
(module
  (func $sum (param $a i32) (param $b i32) (result $res i32)
    get_local $a
    get_local $b
    i32.add
    return
    )
)

Attribution de variable local

int name ()
{
  int a;
  a = 0;
  return a;
}
(module
  (func $name (local $a i32) (result $res i32)
    i32.const 0;
    set_local $a
    get_local $a
    return
    )
)

Import function

(module
  (import "io" "print" (func $print (param $n i32))
  ;; use the print function
  i32.const 120
  call $print 
)

if

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

while

a=1;
while (a<120)
{
   print (a);
   a=a+1
}
;; import the print function
i32.const 0
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

for (i=0; i<120; i++)
{
   print (a);
}
;; import the print function
i32.const 0
get_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

do-while

a=1;
do 
{
   print (a);
   a=a+1
} while (a<=120);
;; import the print function
i32.const 0
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

Exercises

  1. Ecrivez le code WebAssembly pour le calcul de l'expression (2 + 3 * (7 + 5)). Écrivez-le en utilisant du papier ou un éditeur de texte. (2p)
  2. Ecrire un programme qui a une fonction de démarrage et imprime le jour actuel du mois. (1p)
  3. Ecrivez un programme qui affiche la somme de deux variables locales de la fonction start. (1p)
  4. Ecrire un programme avec une fonction qui calcule la soustraction de deux nombres. Utilisez la fonction. (1p)
  5. Ecrire une fonction qui vérifie si un nombre est impair. Imprimer 1 si c'est le cas, 0 sinon. (1p)
  6. Ecrire une fonction qui imprime tous les nombres de 1 à 1000. (1p)
  7. Ecrire une fonction qui calcule la puissance d'un nombre. La fonction reçoit deux paramètres: le nombre et la puissance. (1p)
  8. Ecrire une fonction qui vérifie si un nombre est premier. (2p)

Solutions

alf/laboratoare/11.1525681414.txt.gz · Last modified: 2018/05/07 11:23 by alexandru.radovici
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0