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