Content |
Using the ULM-Assembler
The ULM Assembler consists of three GNU-AWK scripts that can be downloaded with wget:
$shell> pwd /home/numerik/hpc/ss17/session02/demo $shell> wget http://www.mathematik.uni-ulm.de/~lehn/ulmas1.awk --2017-05-02 15:49:05-- http://www.mathematik.uni-ulm.de/~lehn/ulmas1.awk Resolving www-proxy.uni-ulm.de... 134.60.1.151 Connecting to www-proxy.uni-ulm.de|134.60.1.151|:3128... connected. Proxy request sent, awaiting response... 200 OK Length: 1431 (1.4K) Saving to: 'ulmas1.awk.7' 0K . 100% 216M=0s 2017-05-02 15:49:05 (216 MB/s) - 'ulmas1.awk.7' saved [1431/1431] $shell> wget http://www.mathematik.uni-ulm.de/~lehn/ulmas2.awk --2017-05-02 15:49:05-- http://www.mathematik.uni-ulm.de/~lehn/ulmas2.awk Resolving www-proxy.uni-ulm.de... 134.60.1.151 Connecting to www-proxy.uni-ulm.de|134.60.1.151|:3128... connected. Proxy request sent, awaiting response... 200 OK Length: 2305 (2.3K) Saving to: 'ulmas2.awk.7' 0K .. 100% 311M=0s 2017-05-02 15:49:05 (311 MB/s) - 'ulmas2.awk.7' saved [2305/2305] $shell> wget http://www.mathematik.uni-ulm.de/~lehn/ulmas3.awk --2017-05-02 15:49:05-- http://www.mathematik.uni-ulm.de/~lehn/ulmas3.awk Resolving www-proxy.uni-ulm.de... 134.60.1.151 Connecting to www-proxy.uni-ulm.de|134.60.1.151|:3128... connected. Proxy request sent, awaiting response... 200 OK Length: 4979 (4.9K) Saving to: 'ulmas3.awk.7' 0K .... 100% 19.5M=0s 2017-05-02 15:49:05 (19.5 MB/s) - 'ulmas3.awk.7' saved [4979/4979] $shell>
Example for an Assembly Program
.equ ZERO, 0 .equ RIP, 1 .equ ERG, 6 .equ N, 5 .macro set VALUE, REGISTER orq \VALUE, %ZERO, \REGISTER .endm .text set $n, %N movq (%N), %N subq $0, %N, %ZERO jz isZero subq $1, %N, %N jmp endif isZero: addq $1, %N, %N endif: movq %N, n(%RIP) halt .data n: .quad
Run the ULM-Assembler
The three scripts can be connected with pipelines:
$shell> cat example.s | gawk -f ulmas1.awk | gawk -f ulmas2.awk | gawk -f ulmas3.awk 81 28 00 05 # 0000 : orq $40, %0, %5 11 05 00 05 # 0004 : movq (%5), %5 63 00 05 00 # 0008 : subq $0, %5, %0 91 00 00 03 # 000C : jz $3 63 01 05 05 # 0010 : subq $1, %5, %5 90 00 00 02 # 0014 : jmp $2 61 01 05 05 # 0018 : addq $1, %5, %5 41 05 01 0C # 001C : movq %5, 12(%1) 00 00 00 00 # 0020 : halt FF FF FF FF # 0028 : (padding) 00 00 00 00 # 0028 00 00 00 00 # 002C $shell>
You can observe what a single script does as follows:
-
The first script only expands macros:
$shell> cat example.s | gawk -f ulmas1.awk .text orq $n, %0, %5 movq (%5), %5 subq $0, %5, %0 jz isZero subq $1, %5, %5 jmp endif isZero: addq $1, %5, %5 endif: movq %5, n(%1) halt .data n: .quad $shell>
-
The second script resolves lables:
$shell> cat example.s | gawk -f ulmas1.awk | gawk -f ulmas2.awk orq $40, %0, %5 movq (%5), %5 subq $0, %5, %0 jz $3 subq $1, %5, %5 jmp $2 addq $1, %5, %5 movq %5, 12(%1) halt FF FF FF FF # 0028 : (padding) 00 00 00 00 # 0028 00 00 00 00 # 002C $shell>
-
The third script generates machine code by translating the assembly instructions one-by-one:
$shell> cat example.s | gawk -f ulmas1.awk | gawk -f ulmas2.awk | gawk -f ulmas3.awk 81 28 00 05 # 0000 : orq $40, %0, %5 11 05 00 05 # 0004 : movq (%5), %5 63 00 05 00 # 0008 : subq $0, %5, %0 91 00 00 03 # 000C : jz $3 63 01 05 05 # 0010 : subq $1, %5, %5 90 00 00 02 # 0014 : jmp $2 61 01 05 05 # 0018 : addq $1, %5, %5 41 05 01 0C # 001C : movq %5, 12(%1) 00 00 00 00 # 0020 : halt FF FF FF FF # 0028 : (padding) 00 00 00 00 # 0028 00 00 00 00 # 002C $shell>
You can store the generated machine code in a file (here 'example.ulm') by redirecting the output:
$shell> cat example.s | gawk -f ulmas1.awk | gawk -f ulmas2.awk | gawk -f ulmas3.awk > example.ulm $shell>
Execute the generated machine code on ULM
As in the last session we now can run the machine code on the ULM:
$shell> ./ulm -r example.ulm CPU cycles done 0 --------------------------------------------------------------------------------------------------------------------------- 81 28 00 05 11 05 00 05 63 00 05 00 91 00 00 03 63 01 05 05 90 00 00 02 61 01 05 05 41 05 01 0c 00 00 00 00 ff ff ff ff 00 --------------------------------------------------------------------------------------------------------------------------- |0 |8 |16 |24 |32 |40 |0x0 |0x8 |0x10 |0x18 |0x20 |0x28 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |40 |48 |56 |64 |72 |80 |0x28 |0x30 |0x38 |0x40 |0x48 |0x50 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |80 |88 |96 |104 |112 |120 |0x50 |0x58 |0x60 |0x68 |0x70 |0x78 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |120 |128 |136 |144 |152 |160 |0x78 |0x80 |0x88 |0x90 |0x98 |0xa0 Reg0 0x0000000000000000 Reg1 0x0000000000000000 IP: 0 Reg2 0x0000000000000000 IR: halt Reg3 0x00000000000000a0 Reg4 0x0000000000400e5a Reg5 0x00007f74ae4d21a8 Reg6 0x0000000000000000 Reg7 0x00007f74ae4d1060 Reg8 0x0000000000000000 Reg9 0x0000000000000000 Reg10 0x00007f74ae2b805f Reg11 0x00007f7400000000 Reg12 0x00007ffed6b8ebb0 Reg13 0x0000000000000000 Reg14 0x0000000000000000 Reg15 0x0000000000000001 ZF = 0, OF = 0, SF = 0, CF = 0 Terminal: CPU cycles done 1 --------------------------------------------------------------------------------------------------------------------------- 81 28 00 05 11 05 00 05 63 00 05 00 91 00 00 03 63 01 05 05 90 00 00 02 61 01 05 05 41 05 01 0c 00 00 00 00 ff ff ff ff 00 --------------------------------------------------------------------------------------------------------------------------- |0 |8 |16 |24 |32 |40 |0x0 |0x8 |0x10 |0x18 |0x20 |0x28 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |40 |48 |56 |64 |72 |80 |0x28 |0x30 |0x38 |0x40 |0x48 |0x50 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |80 |88 |96 |104 |112 |120 |0x50 |0x58 |0x60 |0x68 |0x70 |0x78 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |120 |128 |136 |144 |152 |160 |0x78 |0x80 |0x88 |0x90 |0x98 |0xa0 Reg0 0x0000000000000000 Reg1 0x0000000000000004 IP: 4 Reg2 0x0000000081280005 IR: orq $40, %0, %5 Reg3 0x00000000000000a0 Reg4 0x0000000000400e5a Reg5 0x0000000000000028 Reg6 0x0000000000000000 Reg7 0x00007f74ae4d1060 Reg8 0x0000000000000000 Reg9 0x0000000000000000 Reg10 0x00007f74ae2b805f Reg11 0x00007f7400000000 Reg12 0x00007ffed6b8ebb0 Reg13 0x0000000000000000 Reg14 0x0000000000000000 Reg15 0x0000000000000001 ZF = 0, OF = 0, SF = 0, CF = 0 Terminal: CPU cycles done 2 --------------------------------------------------------------------------------------------------------------------------- 81 28 00 05 11 05 00 05 63 00 05 00 91 00 00 03 63 01 05 05 90 00 00 02 61 01 05 05 41 05 01 0c 00 00 00 00 ff ff ff ff 00 --------------------------------------------------------------------------------------------------------------------------- |0 |8 |16 |24 |32 |40 |0x0 |0x8 |0x10 |0x18 |0x20 |0x28 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |40 |48 |56 |64 |72 |80 |0x28 |0x30 |0x38 |0x40 |0x48 |0x50 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |80 |88 |96 |104 |112 |120 |0x50 |0x58 |0x60 |0x68 |0x70 |0x78 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |120 |128 |136 |144 |152 |160 |0x78 |0x80 |0x88 |0x90 |0x98 |0xa0 Reg0 0x0000000000000000 Reg1 0x0000000000000008 IP: 8 Reg2 0x0000000011050005 IR: movq 0(%5), %5 Reg3 0x00000000000000a0 Reg4 0x0000000000400e5a Reg5 0x0000000000000000 Reg6 0x0000000000000000 Reg7 0x00007f74ae4d1060 Reg8 0x0000000000000000 Reg9 0x0000000000000000 Reg10 0x00007f74ae2b805f Reg11 0x00007f7400000000 Reg12 0x00007ffed6b8ebb0 Reg13 0x0000000000000000 Reg14 0x0000000000000000 Reg15 0x0000000000000001 ZF = 0, OF = 0, SF = 0, CF = 0 Terminal: CPU cycles done 3 --------------------------------------------------------------------------------------------------------------------------- 81 28 00 05 11 05 00 05 63 00 05 00 91 00 00 03 63 01 05 05 90 00 00 02 61 01 05 05 41 05 01 0c 00 00 00 00 ff ff ff ff 00 --------------------------------------------------------------------------------------------------------------------------- |0 |8 |16 |24 |32 |40 |0x0 |0x8 |0x10 |0x18 |0x20 |0x28 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |40 |48 |56 |64 |72 |80 |0x28 |0x30 |0x38 |0x40 |0x48 |0x50 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |80 |88 |96 |104 |112 |120 |0x50 |0x58 |0x60 |0x68 |0x70 |0x78 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |120 |128 |136 |144 |152 |160 |0x78 |0x80 |0x88 |0x90 |0x98 |0xa0 Reg0 0x0000000000000000 Reg1 0x000000000000000c IP: 12 Reg2 0x0000000063000500 IR: subq $0, %5, %0 Reg3 0x00000000000000a0 Reg4 0x0000000000400e5a Reg5 0x0000000000000000 Reg6 0x0000000000000000 Reg7 0x00007f74ae4d1060 Reg8 0x0000000000000000 Reg9 0x0000000000000000 Reg10 0x00007f74ae2b805f Reg11 0x00007f7400000000 Reg12 0x00007ffed6b8ebb0 Reg13 0x0000000000000000 Reg14 0x0000000000000000 Reg15 0x0000000000000001 ZF = 1, OF = 0, SF = 0, CF = 0 Terminal: CPU cycles done 4 --------------------------------------------------------------------------------------------------------------------------- 81 28 00 05 11 05 00 05 63 00 05 00 91 00 00 03 63 01 05 05 90 00 00 02 61 01 05 05 41 05 01 0c 00 00 00 00 ff ff ff ff 00 --------------------------------------------------------------------------------------------------------------------------- |0 |8 |16 |24 |32 |40 |0x0 |0x8 |0x10 |0x18 |0x20 |0x28 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |40 |48 |56 |64 |72 |80 |0x28 |0x30 |0x38 |0x40 |0x48 |0x50 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |80 |88 |96 |104 |112 |120 |0x50 |0x58 |0x60 |0x68 |0x70 |0x78 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |120 |128 |136 |144 |152 |160 |0x78 |0x80 |0x88 |0x90 |0x98 |0xa0 Reg0 0x0000000000000000 Reg1 0x0000000000000018 IP: 24 Reg2 0x0000000091000003 IR: jz @+$3 Reg3 0x00000000000000a0 Reg4 0x0000000000400e5a Reg5 0x0000000000000000 Reg6 0x0000000000000000 Reg7 0x00007f74ae4d1060 Reg8 0x0000000000000000 Reg9 0x0000000000000000 Reg10 0x00007f74ae2b805f Reg11 0x00007f7400000000 Reg12 0x00007ffed6b8ebb0 Reg13 0x0000000000000000 Reg14 0x0000000000000000 Reg15 0x0000000000000001 ZF = 1, OF = 0, SF = 0, CF = 0 Terminal: CPU cycles done 5 --------------------------------------------------------------------------------------------------------------------------- 81 28 00 05 11 05 00 05 63 00 05 00 91 00 00 03 63 01 05 05 90 00 00 02 61 01 05 05 41 05 01 0c 00 00 00 00 ff ff ff ff 00 --------------------------------------------------------------------------------------------------------------------------- |0 |8 |16 |24 |32 |40 |0x0 |0x8 |0x10 |0x18 |0x20 |0x28 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |40 |48 |56 |64 |72 |80 |0x28 |0x30 |0x38 |0x40 |0x48 |0x50 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |80 |88 |96 |104 |112 |120 |0x50 |0x58 |0x60 |0x68 |0x70 |0x78 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |120 |128 |136 |144 |152 |160 |0x78 |0x80 |0x88 |0x90 |0x98 |0xa0 Reg0 0x0000000000000000 Reg1 0x000000000000001c IP: 28 Reg2 0x0000000061010505 IR: addq $1, %5, %5 Reg3 0x00000000000000a0 Reg4 0x0000000000400e5a Reg5 0x0000000000000001 Reg6 0x0000000000000000 Reg7 0x00007f74ae4d1060 Reg8 0x0000000000000000 Reg9 0x0000000000000000 Reg10 0x00007f74ae2b805f Reg11 0x00007f7400000000 Reg12 0x00007ffed6b8ebb0 Reg13 0x0000000000000000 Reg14 0x0000000000000000 Reg15 0x0000000000000001 ZF = 0, OF = 0, SF = 0, CF = 0 Terminal: CPU cycles done 6 --------------------------------------------------------------------------------------------------------------------------- 81 28 00 05 11 05 00 05 63 00 05 00 91 00 00 03 63 01 05 05 90 00 00 02 61 01 05 05 41 05 01 0c 00 00 00 00 ff ff ff ff 00 --------------------------------------------------------------------------------------------------------------------------- |0 |8 |16 |24 |32 |40 |0x0 |0x8 |0x10 |0x18 |0x20 |0x28 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |40 |48 |56 |64 |72 |80 |0x28 |0x30 |0x38 |0x40 |0x48 |0x50 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |80 |88 |96 |104 |112 |120 |0x50 |0x58 |0x60 |0x68 |0x70 |0x78 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |120 |128 |136 |144 |152 |160 |0x78 |0x80 |0x88 |0x90 |0x98 |0xa0 Reg0 0x0000000000000000 Reg1 0x0000000000000020 IP: 32 Reg2 0x000000004105010c IR: movq %5, 12(%1) Reg3 0x00000000000000a0 Reg4 0x0000000000400e5a Reg5 0x0000000000000001 Reg6 0x0000000000000000 Reg7 0x00007f74ae4d1060 Reg8 0x0000000000000000 Reg9 0x0000000000000000 Reg10 0x00007f74ae2b805f Reg11 0x00007f7400000000 Reg12 0x00007ffed6b8ebb0 Reg13 0x0000000000000000 Reg14 0x0000000000000000 Reg15 0x0000000000000001 ZF = 0, OF = 0, SF = 0, CF = 0 Terminal: CPU cycles done 7 --------------------------------------------------------------------------------------------------------------------------- 81 28 00 05 11 05 00 05 63 00 05 00 91 00 00 03 63 01 05 05 90 00 00 02 61 01 05 05 41 05 01 0c 00 00 00 00 ff ff ff ff 00 --------------------------------------------------------------------------------------------------------------------------- |0 |8 |16 |24 |32 |40 |0x0 |0x8 |0x10 |0x18 |0x20 |0x28 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |40 |48 |56 |64 |72 |80 |0x28 |0x30 |0x38 |0x40 |0x48 |0x50 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |80 |88 |96 |104 |112 |120 |0x50 |0x58 |0x60 |0x68 |0x70 |0x78 --------------------------------------------------------------------------------------------------------------------------- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --------------------------------------------------------------------------------------------------------------------------- |120 |128 |136 |144 |152 |160 |0x78 |0x80 |0x88 |0x90 |0x98 |0xa0 Reg0 0x0000000000000000 Reg1 0x0000000000000024 IP: 36 Reg2 0x0000000000000000 IR: halt Reg3 0x00000000000000a0 Reg4 0x0000000000400e5a Reg5 0x0000000000000001 Reg6 0x0000000000000000 Reg7 0x00007f74ae4d1060 Reg8 0x0000000000000000 Reg9 0x0000000000000000 Reg10 0x00007f74ae2b805f Reg11 0x00007f7400000000 Reg12 0x00007ffed6b8ebb0 Reg13 0x0000000000000000 Reg14 0x0000000000000000 Reg15 0x0000000000000001 ZF = 0, OF = 0, SF = 0, CF = 0 Terminal: $shell>