raz / test / run / AsmName.raz

139 lines · 3.5 KiB · raw · history · last change a072a400

1module AsmName;
2(* Procedures and module variables named in code procedures, where
3 they stand for their addresses (report section 8): call p, jmp p and
4 lea r, [v], of this module and of another. Each is run, and its
5 bytes are read back: the distance in call's, jmp's and lea's 32-bit
6 field must reach what lea and a procedure value say is there. *)
7import Out, AsmNamed;
8import SYSTEM;
9
10var total: I64; (* added to through its address, by lea *)
11
12(* x * 3 + 1: only returns, so a call of it in raz is made in place,
13 but its own code is still there for a code procedure to call *)
14proc triple(x: I64): I64;
15begin
16 return x * 3 + 1;
17end;
18
19(* 1 + 2 + ... + n, reached only from sumTo's code: kept all the same *)
20proc onlyFromCode(n: I64): I64;
21var i, s: I64;
22begin
23 s := 0;
24 for i := 1 to n do s := s + i; end;
25 return s;
26end;
27
28(* triple(x) + 1: x is in rdi already, as System V passes it, and rsp
29 is kept a multiple of 16 at the call *)
30proc callTriple(x: I64): I64;
31code
32 sub rsp, 8;
33 call triple;
34 add rsp, 8;
35 add rax, 1;
36 ret;
37end callTriple;
38
39(* AsmNamed.twice(x), as a tail call: its ret returns to our caller *)
40proc tailTwice(x: I64): I64;
41code
42 jmp AsmNamed.twice;
43end tailTwice;
44
45proc sumTo(n: I64): I64;
46code
47 sub rsp, 8;
48 call onlyFromCode;
49 add rsp, 8;
50 ret;
51end sumTo;
52
53(* adds x to total and to AsmNamed.count, through their addresses *)
54proc addBoth(x: I64);
55code
56 lea rax, [total];
57 add [rax], rdi;
58 lea rcx, [AsmNamed.count];
59 add [rcx], rdi;
60 ret;
61end addBoth;
62
63(* total, read through its address *)
64proc readTotal: I64;
65code
66 lea r9, [total];
67 mov rax, [r9];
68 ret;
69end readTotal;
70
71(* a label is found before a procedure of the same name *)
72proc labelFirst: I64;
73code
74 jmp triple;
75 mov eax, 1;
76 ret;
77 triple:
78 mov eax, 2;
79 ret;
80end labelFirst;
81
82(* the address of triple, and of AsmNamed.twice *)
83proc addresses: (^U8, ^U8);
84code
85 lea rax, [triple];
86 lea rdx, [AsmNamed.twice];
87 ret;
88end addresses;
89
90(* Whether the 32-bit field at b[at], counted from b[at + 4], reaches target *)
91proc reaches(b: ^U8; at: INT; target: ^U8): BOOL;
92var d: I32; t: ^U8;
93begin
94 d := I32(INT(b[at]) | (INT(b[at + 1]) shl 8) | (INT(b[at + 2]) shl 16) | (INT(b[at + 3]) shl 24));
95 t := ^U8(INT(b) + at + 4 + INT(d));
96 return t = target;
97end;
98
99(* yes or no *)
100proc say(what: []const U8; ok: BOOL);
101begin
102 Out.str(what);
103 if ok then Out.str(": yes"); else Out.str(": no"); end;
104 Out.ln;
105end;
106
107proc main*;
108var p: proc(x: I64): I64; q: proc(x: I64); b, t, w: ^U8;
109begin
110 Out.int(callTriple(5)); Out.ln;
111 Out.int(triple(5)); Out.ln;
112 Out.int(tailTwice(21)); Out.ln;
113 Out.int(sumTo(10)); Out.ln;
114 Out.int(labelFirst()); Out.ln;
115 addBoth(7);
116 addBoth(5);
117 Out.int(readTotal()); Out.str(" "); Out.int(total); Out.str(" "); Out.int(AsmNamed.count); Out.ln;
118 t, w := addresses();
119 p := triple;
120 say("lea of a procedure is its address", t = ^U8(p));
121 p := AsmNamed.twice;
122 say("and of another module's", w = ^U8(p));
123 p := callTriple;
124 b := ^U8(p);
125 say("call is E8", b[4] = 0E8H);
126 say("call reaches triple", reaches(b, 5, t));
127 p := tailTwice;
128 b := ^U8(p);
129 say("jmp is E9", b[0] = 0E9H);
130 say("jmp reaches AsmNamed.twice", reaches(b, 1, w));
131 q := addBoth;
132 b := ^U8(q);
133 say("lea is 48 8D 05", (b[0] = 48H) and (b[1] = 8DH) and (b[2] = 05H));
134 say("lea reaches total", reaches(b, 3, ^U8(SYSTEM.ADR(total))));
135 say("lea rcx is 48 8D 0D", (b[10] = 48H) and (b[11] = 8DH) and (b[12] = 0DH));
136 say("lea reaches AsmNamed.count", reaches(b, 13, ^U8(AsmNamed.countAt())));
137end;
138
139end AsmName.