Last active
January 4, 2020 02:05
-
-
Save natanbc/ff6cdce8c1e1eff90fa66bac5103d027 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs"); | |
const cp = require("child_process"); | |
function equals(a, b) { | |
const compare = toUTF8(b).map(c => ` | |
call readch | |
cmp byte ptr [r8], ${c} | |
jne fail | |
`); | |
const code = `.intel_syntax noprefix | |
read: | |
mov rax, 0 | |
mov rdi, 0 | |
mov rsi, r8 | |
mov rdx, 1 | |
syscall | |
ret | |
readch: | |
call read | |
cmp rax, 0 | |
jle fail | |
ret | |
main: .globl main | |
lea r8, [rsp - 8]; | |
sub rsp, 16 | |
${compare.join('\n')} | |
call read | |
cmp rax, 0 | |
jne fail | |
mov rdi, 0 | |
jmp done | |
fail: | |
mov rdi, 1 | |
done: | |
mov rax, 60 | |
syscall`; | |
fs.writeFileSync("source.s", code); | |
cp.execSync("gcc -o compare source.s"); | |
return cp.spawnSync("./compare", [], { input: toUTF8(a) }).status === 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs"); | |
const cp = require("child_process"); | |
function startsWith(a, b) { | |
const compare = toUTF8(b).map(c => ` | |
call read | |
cmp byte ptr [r8], ${c} | |
jne fail | |
`); | |
const code = `.intel_syntax noprefix | |
read: | |
mov rax, 0 | |
mov rdi, 0 | |
mov rsi, r8 | |
mov rdx, 1 | |
syscall | |
cmp rax, 0 | |
jle fail | |
ret | |
main: .globl main | |
lea r8, [rsp - 8]; | |
sub rsp, 16 | |
${compare.join('\n')} | |
mov rdi, 0 | |
jmp done | |
fail: | |
mov rdi, 1 | |
done: | |
mov rax, 60 | |
syscall`; | |
fs.writeFileSync("source.s", code); | |
cp.execSync("gcc -o compare source.s"); | |
return cp.spawnSync("./compare", [], { input: toUTF8(a) }).status === 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment