raz / test / run / Asm.raz

178 lines · 4.0 KiB · raw · history · last change 264716cb

1module Asm;
2(* Code procedures in assembly: the instructions the assembler knows,
3 each checked by what it computes *)
4import Out;
5import SYSTEM;
6
7const
8 Seven = 7;
9 Eight = 8;
10
11(* a * 10 + b - 7, by lea, shifts and adds *)
12proc tens(a, b: I64): I64;
13code
14 lea rax, [rdi + rdi * 4];
15 shl rax, 1;
16 add rax, rsi;
17 sub rax, Seven;
18 ret;
19end tens;
20
21(* the sum of n quadwords at p, by a loop back to a label *)
22proc sum(p: ^I64; n: INT): I64;
23code
24 xor eax, eax;
25 test rsi, rsi;
26 jz done;
27 again:
28 add rax, [rdi];
29 add rdi, 8;
30 dec rsi;
31 jnz again;
32 done:
33 ret;
34end sum;
35
36(* p[i + 2] + p[i - 1]: a scale, by number and by constant, then a
37 displacement *)
38proc around(p: ^I64; i: INT): I64;
39code
40 mov rax, [rdi + rsi * 8 + 16];
41 add rax, [rdi + rsi * Eight - 8];
42 ret;
43end around;
44
45(* the byte at p + i, zero-extended, and the byte there set to v *)
46proc swapByte(p: ^U8; i: INT; v: U8): I64;
47code
48 movzx eax, byte [rdi + rsi];
49 mov [rdi + rsi * 1], dl;
50 ret;
51end swapByte;
52
53(* a div b and a mod b, unsigned; two results in rax and rdx *)
54proc udivmod(a, b: U64): (U64, U64);
55code
56 mov rax, rdi;
57 xor edx, edx;
58 div rsi;
59 ret;
60end udivmod;
61
62(* the larger of a and b, signed, by cmov; and whether a < b by setl *)
63proc max(a, b: I64): I64;
64code
65 cmp rdi, rsi;
66 mov rax, rdi;
67 cmovl rax, rsi;
68 ret;
69end max;
70
71proc below(a, b: I64): BOOL;
72code
73 xor eax, eax;
74 cmp rdi, rsi;
75 setl al;
76 ret;
77end below;
78
79(* (x + y) * x, then its square root *)
80proc real(x, y: F64): F64;
81code
82 movsd xmm2, xmm0;
83 addsd xmm0, xmm1;
84 mulsd xmm0, xmm2;
85 sqrtsd xmm0, xmm0;
86 ret;
87end real;
88
89(* -a, ~a and a * 3 + 1 for an I32, through eax *)
90proc mix(a: I32): I64;
91code
92 mov eax, edi;
93 neg eax;
94 not eax;
95 imul eax, eax, 3;
96 inc eax;
97 movsxd rax, eax;
98 ret;
99end mix;
100
101(* XADD by hand: the old value of p^, which goes up by v *)
102proc fetchAdd(p: ^INT; v: INT): INT;
103code
104 mov rax, rsi;
105 lock xadd [rdi], rax;
106 ret;
107end fetchAdd;
108
109(* the real 2.0, from its bits, through a general register *)
110proc two: F64;
111code
112 mov rax, 4000000000000000H;
113 movq xmm0, rax;
114 ret;
115end two;
116
117(* bytes the assembler is given as they are: mov eax, 42; ret *)
118proc answer: I64;
119code
120 db 0B8H, 42, 0, 0, 0;
121 db 0C3H;
122end answer;
123
124(* the procedure f, jumped to through a register: its result comes back as ours *)
125proc through(f: proc: I64): I64;
126code
127 jmp rdi;
128end through;
129
130(* s without its first k bytes: s in rdi and rsi, k in rdx, the slice back in rax and rdx *)
131proc tail(s: []U8; k: INT): []U8;
132code
133 add rdi, rdx;
134 sub rsi, rdx;
135 mov rax, rdi;
136 mov rdx, rsi;
137 ret;
138end tail;
139
140(* the byte of s at x, or -1; the slice is not the first parameter:
141 x in rdi, the address of s in rsi, its length in rdx *)
142proc pick(x: INT; s: []U8): INT;
143code
144 cmp rdi, rdx;
145 jae none;
146 movzx eax, byte [rsi + rdi];
147 ret;
148 none:
149 mov rax, -1;
150 ret;
151end pick;
152
153proc main*;
154var a: [4]I64; s: [3]U8; t: [6]U8; q, r: U64; c: INT;
155begin
156 Out.int(tens(4, 2)); Out.ln;
157 a := [1, 20, 300, 4000];
158 Out.int(sum(SYSTEM.ADR(a[0]), 4)); Out.str(" "); Out.int(sum(SYSTEM.ADR(a[0]), 0)); Out.str(" ");
159 Out.int(around(SYSTEM.ADR(a[0]), 1)); Out.ln;
160 s := "abc";
161 Out.int(swapByte(SYSTEM.ADR(s[0]), 1, 'z')); Out.str(" "); Out.str(s[0..3]); Out.ln;
162 q, r := udivmod(1000, 7);
163 Out.int(I64(q)); Out.str(" "); Out.int(I64(r)); Out.ln;
164 Out.int(max(-5, 3)); Out.str(" "); Out.int(max(9, 3)); Out.str(" ");
165 if below(-1, 1) and not below(1, -1) then Out.str("below"); end;
166 Out.ln;
167 Out.int(I64(real(1.0, 3.0))); Out.ln;
168 Out.int(mix(5)); Out.ln;
169 c := 40;
170 Out.int(fetchAdd(SYSTEM.ADR(c), 2)); Out.str(" "); Out.int(c); Out.ln;
171 Out.int(I64(two() * 21.0)); Out.str(" "); Out.int(answer()); Out.ln;
172 Out.int(through(answer)); Out.ln;
173 t := "abcdef";
174 Out.str(tail(t[0..6], 2)); Out.str(" "); Out.int(LEN(tail(t[0..6], 6))); Out.ln;
175 Out.int(pick(3, t[0..6])); Out.str(" "); Out.int(pick(9, t[0..6])); Out.ln;
176end;
177
178end Asm.