======================= VNA: Control Structures [TOC] ======================= ---- BOX ----------------------------------------------------------------------- *Warning* On `thales` the ULM Assembler will not work! The installed `gawk` is to old! Use the Linux computers in E44. If you have your own Linux box you should have no problem. All Linux distributions have a decent `gawk` version. If you want to login on a E44 computer via `ssh`: - first login on thales (`ssh yourloginname@thales.mathematik.uni-ulm.de`) - then login on a E44 computer, e.g. on `heim` with `ssh heim` (here you can skip the login name an the domain `.mathematik.uni-ulm.de`) -------------------------------------------------------------------------------- Update the ULM simulator (and obtain the assembler) =================================================== Change into the directory that contains `ulm.cc` (cloned from github in the previous session). Then update the source code for the simulator: ---- SHELL (path=session01/ulm) ------------------------------------- pwd git pull --------------------------------------------------------------------- Note that this will also download or update the ULM assembler (consisting of `ulmas_label.awk` and `ulmas_codegen.awk`). If you don't get a message that the source code of `ulm.cc` was up-to-date you need to re-compile with ---- SHELL (path=session01/ulm) ------------------------------------- g++ -Wall -std=c++11 -o ulm ulm.cc --------------------------------------------------------------------- Using the Assembler =================== The assembler consists of 3 awk scripts: - `ulmas_macro.awk` Expands macro and `.equ` directives. - `ulmas_label.awk` Resolves labels and encodes data directives. - `ulmas_codegen.awk` Translates assembly instructions into machine code instructions. You have to run these scripts in the given order. Splitting the assembler into different scripts has the advantage that you can observe intermediate results after each step. Example ------- Create a file `foo1_asm.s` (in the directory with the ULM simulator and the assembler) with following content: ---- CODE(type=txt,file=session01/ulm/foo1_asm.s) -------------------- .equ pN 1 .equ N 2 .text orq $n, %0, %pN movzbq (%pN, %0), %N subq $1, %N, %0 ja then orq $0x0D, %0, %N # else then: jmp $0x02 orq $0x2A, %0, %N movb %N, (%pN, %0) .data n: .quad 0x8000000000000000 ---------------------------------------------------------------------- Generate machine code for the ULM with ---- SHELL (path=session01/ulm,hostname=heim) ---------------------------------- cat foo1_asm.s | gawk -f ulmas_macro.awk | gawk -f ulmas_label.awk | +++ gawk -f ulmas_codegen.awk -------------------------------------------------------------------------------- If you want to see intermediate results just stop after running a script: - Only expand macros and `.equ` directives: ---- SHELL (path=session01/ulm,hostname=heim) -------------------------------- cat foo1_asm.s | gawk -f ulmas_macro.awk ------------------------------------------------------------------------------ - Expand macros/`.equ` directives and also resolve labels and data directives: ---- SHELL (path=session01/ulm,hostname=heim) -------------------------------- cat foo1_asm.s | gawk -f ulmas_macro.awk | gawk -f ulmas_label.awk ------------------------------------------------------------------------------ You can redirect the output (containing the machine code) into a file. For example, into `foo1_asm.ulm`: ---- SHELL (path=session01/ulm,hostname=heim) ---------------------------------- cat foo1_asm.s | gawk -f ulmas_macro.awk | gawk -f ulmas_label.awk | +++ gawk -f ulmas_codegen.awk +++ > foo1_asm.ulm -------------------------------------------------------------------------------- Or do both, print the output on the therminal and redirect it, with the `tee` command: ---- SHELL (path=session01/ulm,hostname=heim) ---------------------------------- cat foo1_asm.s | gawk -f ulmas_macro.awk | gawk -f ulmas_label.awk +++ | gawk -f ulmas_codegen.awk +++ | tee foo1_asm.ulm ------------------------------------------------------------------------ Exercise ======== Use the assembler on all the other examples from the lecture. Remember that you can print the exit code of a UNIX process with `echo $?`. Exercise ======== You get the following assembly code when you pulled the ULM simulator and assembler from github: ---- CODE(type=s) -------------------------------------------------------------- .equ N 5 # Immediate value for vector length .equ I 1 # register for i .equ X 2 # address of x .equ RES 3 # register for res .equ TMP 4 # register for tmp .text load $0, %I # I = 0 load $x, %X # address of x[0] load $0, %RES # result = 0 jmp check # goto check loop: movzbq (%X, %I), %TMP # x[I] addq %TMP, %RES, %RES # result += x[I] addq $1, %I, %I # ++I check: subq $N, %I, %0 # If (I1: result <- result * n n <- n-1 return result -------------------------------------------------------------------------------- Submit your Solution ==================== Send your attached solution by Email to `michael.lehn@uni-ulm.de`. Use the subject `HPC0: quiz01`. *Deadline: May 4th, 2pm*