-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
36 lines (28 loc) · 888 Bytes
/
main.py
File metadata and controls
36 lines (28 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import signal
import sys
import threading
from cursed_vm import Asm, CursedVm
def main():
# this is our guest program
asm = Asm()
asm.push_string("Hello, World!\n") # put string on the stack
asm.mov_rsi_rsp() # point RSI at the string
asm.print_loop(port=0x10) # loop: send each char to port 0x10
stop = threading.Event()
signal.signal(signal.SIGINT, lambda *_: stop.set())
def vm_thread():
with CursedVm() as vm:
vm.load(asm.code())
vm.run(
lambda port, data: (
sys.stdout.write(chr(data[0])) or sys.stdout.flush()
if port == 0x10 and len(data) == 1
else None
)
)
t = threading.Thread(target=vm_thread, daemon=True)
t.start()
stop.wait()
print("\nBye.")
if __name__ == "__main__":
main()