| 1 | module 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. *) |
| 7 | import Out, AsmNamed; |
| 8 | import SYSTEM; |
| 9 | |
| 10 | var 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 *) |
| 14 | proc triple(x: I64): I64; |
| 15 | begin |
| 16 | return x * 3 + 1; |
| 17 | end; |
| 18 | |
| 19 | (* 1 + 2 + ... + n, reached only from sumTo's code: kept all the same *) |
| 20 | proc onlyFromCode(n: I64): I64; |
| 21 | var i, s: I64; |
| 22 | begin |
| 23 | s := 0; |
| 24 | for i := 1 to n do s := s + i; end; |
| 25 | return s; |
| 26 | end; |
| 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 *) |
| 30 | proc callTriple(x: I64): I64; |
| 31 | code |
| 32 | sub rsp, 8; |
| 33 | call triple; |
| 34 | add rsp, 8; |
| 35 | add rax, 1; |
| 36 | ret; |
| 37 | end callTriple; |
| 38 | |
| 39 | (* AsmNamed.twice(x), as a tail call: its ret returns to our caller *) |
| 40 | proc tailTwice(x: I64): I64; |
| 41 | code |
| 42 | jmp AsmNamed.twice; |
| 43 | end tailTwice; |
| 44 | |
| 45 | proc sumTo(n: I64): I64; |
| 46 | code |
| 47 | sub rsp, 8; |
| 48 | call onlyFromCode; |
| 49 | add rsp, 8; |
| 50 | ret; |
| 51 | end sumTo; |
| 52 | |
| 53 | (* adds x to total and to AsmNamed.count, through their addresses *) |
| 54 | proc addBoth(x: I64); |
| 55 | code |
| 56 | lea rax, [total]; |
| 57 | add [rax], rdi; |
| 58 | lea rcx, [AsmNamed.count]; |
| 59 | add [rcx], rdi; |
| 60 | ret; |
| 61 | end addBoth; |
| 62 | |
| 63 | (* total, read through its address *) |
| 64 | proc readTotal: I64; |
| 65 | code |
| 66 | lea r9, [total]; |
| 67 | mov rax, [r9]; |
| 68 | ret; |
| 69 | end readTotal; |
| 70 | |
| 71 | (* a label is found before a procedure of the same name *) |
| 72 | proc labelFirst: I64; |
| 73 | code |
| 74 | jmp triple; |
| 75 | mov eax, 1; |
| 76 | ret; |
| 77 | triple: |
| 78 | mov eax, 2; |
| 79 | ret; |
| 80 | end labelFirst; |
| 81 | |
| 82 | (* the address of triple, and of AsmNamed.twice *) |
| 83 | proc addresses: (^U8, ^U8); |
| 84 | code |
| 85 | lea rax, [triple]; |
| 86 | lea rdx, [AsmNamed.twice]; |
| 87 | ret; |
| 88 | end addresses; |
| 89 | |
| 90 | (* Whether the 32-bit field at b[at], counted from b[at + 4], reaches target *) |
| 91 | proc reaches(b: ^U8; at: INT; target: ^U8): BOOL; |
| 92 | var d: I32; t: ^U8; |
| 93 | begin |
| 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; |
| 97 | end; |
| 98 | |
| 99 | (* yes or no *) |
| 100 | proc say(what: []const U8; ok: BOOL); |
| 101 | begin |
| 102 | Out.str(what); |
| 103 | if ok then Out.str(": yes"); else Out.str(": no"); end; |
| 104 | Out.ln; |
| 105 | end; |
| 106 | |
| 107 | proc main*; |
| 108 | var p: proc(x: I64): I64; q: proc(x: I64); b, t, w: ^U8; |
| 109 | begin |
| 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()))); |
| 137 | end; |
| 138 | |
| 139 | end AsmName. |