{"id":324123,"date":"2021-06-01T15:00:23","date_gmt":"2021-06-01T15:00:23","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=324123"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=324123","title":{"rendered":"\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0441\u0442\u0435\u043a\u043e\u0432\u043e\u0439 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 \u043c\u0430\u0448\u0438\u043d\u044b \u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 \u043f\u043e\u0434 \u043d\u0435\u0451 (\u0447\u0430\u0441\u0442\u044c I)"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/258\/b8e\/5f8\/258b8e5f8fbe678d3834315c4d4e4044.jpg\" width=\"852\" height=\"480\"><figcaption><\/figcaption><\/figure>\n<p>\u0422\u0430\u043a \u0441\u043b\u043e\u0436\u0438\u043b\u043e\u0441\u044c, \u0447\u0442\u043e \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 18 \u043b\u0435\u0442, \u043d\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430 C\/C++. \u041d\u0430 \u0440\u0430\u0431\u043e\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0430\u0441\u044c Java, \u0434\u0430 \u0438 \u0432\u0432\u0438\u0434\u0443 \u0434\u043e\u043b\u0436\u043d\u043e\u0441\u0442\u0435\u0439 \u0434\u0435\u044f\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435 \u0431\u044b\u043b\u0430 \u0441\u0432\u044f\u0437\u0430\u043d\u0430 \u0441 \u043f\u0440\u0435\u0434\u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e\u043c &#8212; \u043f\u0435\u0440\u0435\u0433\u043e\u0432\u043e\u0440\u044b, \u043a\u043e\u0440\u043f\u043e\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0435 \u043f\u0440\u043e\u0434\u0430\u0436\u0438, \u0432\u044b\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0441\u0434\u0435\u043b\u043e\u043a. \u0417\u0430\u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0432 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0435 \u043e\u0442 \u0440\u0430\u0431\u043e\u0442\u044b \u0432\u0440\u0435\u043c\u044f \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043d\u0430\u0432\u044b\u043a\u0438, \u0440\u0430\u0437\u043c\u044f\u0442\u044c \u0447\u0430\u0441\u0442\u044c \u043c\u043e\u0437\u0433\u0430 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0435 \u043d\u0430\u043f\u0440\u044f\u0433\u0430\u043b \u0432\u0441\u0435 18 \u043b\u0435\u0442 \u0438, \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u043d\u0430\u0447\u0430\u0442\u044c \u0441 \u0441\u0430\u043c\u044b\u0445 \u043e\u0441\u043d\u043e\u0432. \u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0442\u044c \u0441\u0435\u0431\u0435 \u0437\u0430\u0434\u0430\u0447\u0443.<\/p>\n<p>\u0412 \u0443\u043d\u0438\u0432\u0435\u0440\u0435 \u043f\u0440\u0435\u043f\u043e\u0434\u0430\u0432\u0430\u0442\u0435\u043b\u0438, \u043c\u043e\u043b\u043e\u0434\u043e\u0441\u0442\u044c \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u043b\u0430\u0441\u044c \u043d\u0430 70-80\u0435 \u0433\u043e\u0434\u044b, \u0434\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u043e-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0443\u0431\u0438\u0432\u0430\u043b\u0438\u0441\u044c \u043f\u043e \u0442\u0435\u043c\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u043e\u0432 (\u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u043e\u0432, \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u0432) \u043f\u043e\u0434 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043d\u044b\u0435 \u043e\u0431\u043b\u0430\u0441\u0442\u0438. \u0412\u0441\u0451 \u044d\u0442\u043e \u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u043d\u0435\u0432\u043e\u0441\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u043d\u044b\u043c &#171;\u0441\u0442\u0430\u0440\u044c\u0451\u043c&#187;, \u043d\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u044b\u0445 \u044f\u0437\u044b\u043a\u043e\u0432 \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0434\u0435\u0441\u044f\u0442\u0438\u043b\u0435\u0442\u0438\u0435 (Go, Kotlin \u0438 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0434\u0440\u0443\u0433\u0438\u0445) \u043f\u043e\u0432\u044b\u0441\u0438\u043b\u0438 \u043c\u043e\u0439 \u0438\u043d\u0442\u0435\u0440\u0435\u0441 \u043a \u044d\u0442\u043e\u0439 \u0442\u0435\u043c\u0435.<\/p>\n<p>\u0420\u0435\u0448\u0438\u043b \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0445\u043e\u0431\u0431\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c <strong>32-bit \u0441\u0442\u0435\u043a\u043e\u0432\u0443\u044e \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u0443\u044e \u043c\u0430\u0448\u0438\u043d\u0443 \u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 C \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u044f\u0437\u044b\u043a\u0430 \u043f\u043e\u0434 \u043d\u0435\u0451<\/strong>, \u0447\u0442\u043e\u0431\u044b \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u043d\u0430\u0432\u044b\u043a\u0438. \u0422\u0430\u043a\u0430\u044f \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0430\u044f Computer Science \u0437\u0430\u0434\u0430\u0447\u043a\u0430 \u0434\u043b\u044f \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432\u0435\u0447\u0435\u0440\u043e\u0432 \u0441 \u043f\u0438\u0432\u043e\u043c. \u041a\u0430\u043a \u043f\u0440\u0435\u0434\u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c, \u044f \u0447\u0435\u0442\u043a\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u044e, \u0447\u0442\u043e \u043e\u043d\u0430 \u043d\u0438\u043a\u043e\u043c\u0443 \u043d\u0435 \u043d\u0443\u0436\u043d\u0430, \u043d\u043e \u0442\u0430\u043a\u0430\u044f \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0430 \u043d\u0443\u0436\u043d\u0430 \u043c\u043d\u0435 \u0434\u043b\u044f \u044d\u0441\u0442\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0438\u043d\u0436\u0435\u043d\u0435\u0440\u043d\u043e\u0433\u043e \u0443\u0434\u043e\u0432\u043e\u043b\u044c\u0441\u0442\u0432\u0438\u044f. \u041f\u043b\u044e\u0441 \u043a\u043e\u0433\u0434\u0430 \u043e\u0431 \u044d\u0442\u043e\u043c \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0448\u044c \u0441\u0430\u043c \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u0448\u044c \u0433\u043b\u0443\u0431\u0436\u0435. \u0421 \u0446\u0435\u043b\u044c\u044e \u0438 \u043c\u043e\u0442\u0438\u0432\u0430\u043c\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043b\u0441\u044f. \u041d\u0430\u0447\u043d\u0451\u043c.<\/p>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u044d\u0442\u043e \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u0430\u044f \u043c\u0430\u0448\u0438\u043d\u0430, \u043c\u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u0441 \u0435\u0451 \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0430\u043c\u0438: <\/p>\n<p><strong>CPU<\/strong>: 32-bit\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u043a\u043e\u043c\u0430\u043d\u0434, \u0442\u0430\u043a \u043a\u0430\u043a \u043c\u0430\u0448\u0438\u043d\u0430 \u0441\u0442\u0435\u043a\u043e\u0432\u0430\u044f, \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043e\u043f\u0435\u0440\u0430\u043d\u0434\u044b \u043a\u043e\u043c\u0430\u043d\u0434 \u0445\u0440\u0430\u043d\u0438\u043c \u0432 \u0441\u0442\u0435\u043a\u0435, \u0438\u0437 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u043e\u0432 \u0442\u043e\u043b\u044c\u043a\u043e IP (Instruction Pointer) \u0438 SP (Stack Pointer), \u043f\u043e\u043a\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0441 \u0446\u0435\u043b\u044b\u043c\u0438 \u0447\u0438\u0441\u043b\u0430\u043c\u0438 \u0441\u043e \u0437\u043d\u0430\u043a\u043e\u043c (__int32), \u043f\u043e\u0437\u0436\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445. <\/p>\n<p><strong>RAM<\/strong>: \u043f\u0443\u0441\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u043e\u043a\u0430 \u0431\u0443\u0434\u0435\u0442 65536 \u044f\u0447\u0435\u0435\u043a \u043f\u043e 32-bit`\u0430.<\/p>\n<p>\u041f\u0440\u0435\u0434\u0443\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0432\u043e\u0442 \u0442\u0430\u043a\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438:<\/p>\n<pre><code>  typedef __int32 WORD;    \tconstexpr WORD OP_CODE_MASK = 0b00000000000000000000000011111111; \tconstexpr WORD OP_TYPE_MASK = 0b00000000000000000000111000000000;  \tconstexpr WORD OP_HALT      = 0b00000000000000000000000000000000; \tconstexpr WORD OP_CONST     = 0b00000000000000000000000000000001; \tconstexpr WORD OP_PUSH      = 0b00000000000000000000000000000010; \tconstexpr WORD OP_POP       = 0b00000000000000000000000000000011;  \tconstexpr WORD OP_INC       = 0b00000000000000000000000000000100; \tconstexpr WORD OP_DEC       = 0b00000000000000000000000000000101; \tconstexpr WORD OP_ADD       = 0b00000000000000000000000000000110; \tconstexpr WORD OP_SUB       = 0b00000000000000000000000000000111; \tconstexpr WORD OP_MUL       = 0b00000000000000000000000000001000; \tconstexpr WORD OP_DIV       = 0b00000000000000000000000000001001;  \tconstexpr WORD OP_AND       = 0b00000000000000000000000000001010; \tconstexpr WORD OP_OR        = 0b00000000000000000000000000001011; \tconstexpr WORD OP_XOR       = 0b00000000000000000000000000001100; \tconstexpr WORD OP_NOT       = 0b00000000000000000000000000001101; \tconstexpr WORD OP_SHL       = 0b00000000000000000000000000001110; \tconstexpr WORD OP_SHR       = 0b00000000000000000000000000001111;  \tconstexpr WORD OP_JMP       = 0b00000000000000000000000000010001; \tconstexpr WORD OP_CMPJE     = 0b00000000000000000000000000010010; \tconstexpr WORD OP_CMPJNE    = 0b00000000000000000000000000010011; \tconstexpr WORD OP_CMPJG     = 0b00000000000000000000000000010100; \tconstexpr WORD OP_CMPJGE    = 0b00000000000000000000000000010101; \tconstexpr WORD OP_CMPJL     = 0b00000000000000000000000000010110; \tconstexpr WORD OP_CMPJLE    = 0b00000000000000000000000000010111;  \tconstexpr WORD OP_DUP       = 0b00000000000000000000000000011000; \tconstexpr WORD OP_CALL      = 0b00000000000000000000000000011001; \tconstexpr WORD OP_RET       = 0b00000000000000000000000000011010; \tconstexpr WORD OP_SYSCALL   = 0b00000000000000000000000000011011;  \tconstexpr WORD OP_RESERVED1 = 0b00000000000000000000000000011100; \tconstexpr WORD OP_RESERVED2 = 0b00000000000000000000000000011101; \tconstexpr WORD OP_RESERVED3 = 0b00000000000000000000000000011110; \tconstexpr WORD OP_RESERVED4 = 0b00000000000000000000000000011111;      constexpr WORD MAX_MEMORY = 65536;<\/code><\/pre>\n<p>\u041f\u043e\u043a\u0430 \u043f\u0435\u0440\u0432\u044b\u0435 8 \u0438\u0437 32 \u0431\u0438\u0442 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0434 \u043a\u043e\u043c\u0430\u043d\u0434\u0443 (opcode), 1 \u0431\u0438\u0442 \u0437\u0430\u0440\u0435\u0437\u0435\u0440\u0432\u0438\u0440\u0443\u0435\u043c \u043f\u043e\u0434 \u0442\u0438\u043f \u043e\u043f\u0435\u0440\u0430\u043d\u0434\u0430 (immediate \u0438\u043b\u0438 \u0438\u0437 \u0441\u0442\u0435\u043a\u0430), 3 \u0431\u0438\u0442\u0430 \u043f\u043e\u0434 \u0431\u0443\u0434\u0443\u0449\u0438\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445 (byte, short, int, long, char, float, double \u0438 \u0447\u0442\u043e \u043d\u0438\u0431\u0443\u0434\u044c \u0435\u0449\u0451), \u0430 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0431\u0438\u0442\u044b \u0434\u043b\u044f \u0447\u0435\u0433\u043e \u043d\u0438\u0431\u0443\u0434\u044c \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c. \u041f\u043e\u043a\u0430 \u043d\u0435 \u0437\u043d\u0430\u044e.<\/p>\n<pre><code>class VMRuntime { \tpublic: \t\tVMRuntime();                                \/\/ Constructor \t\t~VMRuntime();                               \/\/ Desctructor \t\tbool loadImage(void* image, size_t size);   \/\/ Load executable image \t\tvoid run();                                 \/\/ Runs image from address 0 \t\tWORD readWord(WORD address);                \/\/ Read WORD from memory \t\tvoid writeWord(WORD address, WORD value);   \/\/ Write WORD to memory  \t\tWORD getMaxAddress();                       \/\/ Get max address in 32-bit words \t\tWORD getIP();                               \/\/ Get Instruction Pointer address \t\tWORD getSP();                               \/\/ Get Stack Pointer address \tprivate: \t\tWORD  memory[MAX_MEMORY];                   \/\/ Random access memory array \t\tWORD  ip;                                   \/\/ Instruction pointer \t\tWORD  sp;                                   \/\/ Stack pointer \t\tWORD  fp;                                   \/\/ Frame pointer \t\tvoid systemCall(WORD n);                    \/\/ System call \t\tvoid printState();                          \/\/ Print current VM state \t};<\/code><\/pre>\n<p>\u0412 \u043a\u043b\u0430\u0441\u0441\u0435 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 \u043c\u0430\u0448\u0438\u043d\u0435 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0442\u0430\u043a\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0439 \u043e\u0431\u0440\u0430\u0437 \u0432 \u043f\u0430\u043c\u044f\u0442\u044c \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 \u043c\u0430\u0448\u0438\u043d\u044b (loadImage), \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 (run), \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0442\u044c \u0441\u043d\u0430\u0440\u0443\u0436\u0438 \u0447\u0438\u0442\u0430\u0442\u044c\/\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c (readWord, writeWord), \u0447\u0438\u0442\u0430\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u043e\u0432 IP, SP. \u0418\u0437 \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0432\u044b\u0432\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043c\u0430\u0448\u0438\u043d\u044b (\u0440\u0430\u0441\u043f\u0435\u0447\u0430\u0442\u043a\u0430 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u043e\u0432 \u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u044f \u0441\u0442\u0435\u043a\u0430), \u0430 \u0442\u0430\u043a\u0436\u0435 \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432, \u0447\u0442\u043e\u0431\u044b \u043a\u043e\u0434 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 \u043c\u0430\u0448\u0438\u043d\u044b \u043c\u043e\u0433 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0441\u043d\u0430\u0440\u0443\u0436\u0438 (\u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u043e\u0431\u0440\u043e\u0441 \u043a\u0430\u043a\u043e\u0433\u043e-\u043d\u0438\u0431\u0443\u0434\u044c API).<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u043e &#8212; \u0448\u0430\u0433\u0430\u0435\u043c \u043f\u043e \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0447\u0438\u0442\u0430\u0435\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u0443, \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0431\u0435\u0440\u0451\u043c \u043e\u043f\u0435\u0440\u0430\u043d\u0434\u044b \u043b\u0438\u0431\u043e \u0438\u0437 \u0441\u0442\u0435\u043a\u0430, \u043b\u0438\u0431\u043e \u0447\u0438\u0442\u0430\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u043d\u0434 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u044f\u0447\u0435\u0439\u043a\u0435 \u043f\u043e\u0441\u043b\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b. \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u043c \u043f\u043e\u043a\u0430 \u043d\u0435 \u0434\u043e\u0439\u0434\u0451\u043c \u0434\u043e \u043a\u043e\u043c\u0430\u043d\u0434\u044b HALT.<\/p>\n<pre><code>void VMRuntime::run() { \tWORD a, b; \tWORD opcode;  \tip = 0; \tsp = MAX_MEMORY - 1;  \twhile (1) {  \t\topcode = memory[ip++]; \t\t \t\tswitch (opcode) { \t\t\/\/------------------------------------------------------------------------ \t\t\/\/ STACK OPERATIONS \t\t\/\/------------------------------------------------------------------------ \t\tcase OP_CONST:  \t\t    memory[--sp] = memory[ip++];  \t\t\tbreak; \t\tcase OP_PUSH: \t\t\tmemory[--sp] = memory[memory[ip++]]; \t\t\tbreak; \t\tcase OP_POP:   \t\t  memory[memory[ip++]] = memory[sp++];  \t\t\tbreak; \t\tcase OP_DUP: \t\t\ta = memory[sp]; \t\t\tmemory[--sp] = a; \t\t\tbreak; \t\t\/\/------------------------------------------------------------------------ \t\t\/\/ ARITHMETIC OPERATIONS \t\t\/\/------------------------------------------------------------------------ \t\tcase OP_INC: \t\t\tmemory[sp]++; \t\t\tbreak; \t\tcase OP_DEC: \t\t\tmemory[sp]--; \t\t\tbreak; \t\tcase OP_ADD:   \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tmemory[--sp] = a + b; \t\t\tbreak; \t\tcase OP_SUB: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tmemory[--sp] = a - b; \t\t\tbreak; \t\tcase OP_MUL: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tmemory[--sp] = a * b; \t\t\tbreak; \t\tcase OP_DIV: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tmemory[--sp] = a \/ b; \t\t\tbreak; \t\t\/\/------------------------------------------------------------------------ \t\t\/\/ BITWISE OPERATIONS \t\t\/\/------------------------------------------------------------------------ \t\tcase OP_AND: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tmemory[--sp] = a &amp; b; \t\t\tbreak; \t\tcase OP_OR: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tmemory[--sp] = a | b; \t\t\tbreak; \t\tcase OP_XOR: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tmemory[--sp] = a ^ b; \t\t\tbreak; \t\tcase OP_NOT: \t\t\ta = memory[sp++]; \t\t\tmemory[--sp] = ~a; \t\t\tbreak; \t\tcase OP_SHL: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tmemory[--sp] = a &lt;&lt; b; \t\t\tbreak; \t\tcase OP_SHR: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tmemory[--sp] = a &gt;&gt; b; \t\t\tbreak; \t\t\/\/------------------------------------------------------------------------ \t\t\/\/ FLOW CONTROL OPERATIONS \t\t\/\/------------------------------------------------------------------------ \t\tcase OP_JMP: \t\t\tip = memory[ip]; \t\t\tbreak; \t\tcase OP_CMPJE: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tif (a == b) ip = memory[ip]; else ip++; \t\t\tbreak; \t\tcase OP_CMPJNE: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tif (a != b) ip = memory[ip]; else ip++; \t\t\tbreak; \t\tcase OP_CMPJG: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tif (a &gt; b) ip = memory[ip]; else ip++; \t\t\tbreak; \t\tcase OP_CMPJGE: \t\t\ta = memory[sp++]; \t\t\tb = memory[sp++]; \t\t\tif (a &gt;= b) ip = memory[ip]; else ip++; \t\t\tbreak; \t\tcase OP_CMPJL: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tif (a &lt; b) ip = memory[ip]; else ip++; \t\t\tbreak; \t\tcase OP_CMPJLE: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tif (a &lt;= b) ip = memory[ip]; else ip++; \t\t\tbreak; \t\t\/\/------------------------------------------------------------------------ \t\t\/\/ PROCEDURE CALL OPERATIONS \t\t\/\/------------------------------------------------------------------------ \t\tcase OP_CALL: \t\t\ta = memory[ip++]; \t\t\tmemory[--sp] = ip;        \t\t\tip = a;                   \t\t\tbreak; \t\tcase OP_RET: \t\t\tip = memory[sp++];        \t\t\tbreak; \t\tcase OP_SYSCALL: \t\t\ta = memory[ip++]; \t\t\tsystemCall(a); \t\t\tbreak; \t\tcase OP_HALT:  \t\t\tprintState(); \t\t  return; \t\tdefault: \t\t\tcout &lt;&lt; \"Runtime error - unknown opcode=\" &lt;&lt; opcode &lt;&lt; endl; \t\t\tprintState(); \t\t\treturn; \t\t} \t}      \/\/ Only one system call implemented - print string (0x20)   void VMRuntime::systemCall(WORD n) { \t\tWORD ptr; \t\tswitch (n) { \t\tcase 0x20:  \/\/ print C style string \t\t\tptr = memory[sp++]; \t\t\tcout &lt;&lt; ((char*)&amp;memory[ptr]); \t\tbreak; \t} }<\/code><\/pre>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u0435\u043d\u044c\u043a\u0443\u044e \u043c\u0430\u0448\u0438\u043d\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0443\u043c\u0435\u0435\u0442 \u0432 \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0438 \u0431\u0438\u0442\u043e\u0432\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0445\u043e\u0434\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0441 \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u043c\u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430\u043c\u0438, \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (\u043f\u043e\u043a\u0430 \u0447\u0435\u0442\u043a\u043e\u0439 \u043a\u043e\u043d\u0432\u0435\u043d\u0446\u0438\u0438 \u043f\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0447\u0435\u0440\u0435\u0437 \u0441\u0442\u0435\u043a \u043d\u0435\u0442, \u043d\u043e \u0434\u0430\u043b\u044c\u0448\u0435 \u0434\u043e\u0434\u0435\u043b\u0430\u0435\u043c). <\/p>\n<p>\u0415\u0449\u0435 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0439 \u043e\u0431\u0440\u0430\u0437. \u041f\u0440\u0438\u043a\u0440\u0443\u0442\u0438\u043c \u043a \u043d\u0435\u043c\u0443 \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0438 \u0434\u0430\u043d\u043d\u044b\u0435, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0442\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u044d\u0442\u043e\u0442 \u043e\u0431\u0440\u0430\u0437 &#171;\u0441\u043a\u043e\u0440\u043c\u0438\u0442\u044c&#187; \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 \u043c\u0430\u0448\u0438\u043d\u0435.<\/p>\n<pre><code>class VMImage { \tpublic: \t\tVMImage(); \t\t~VMImage(); \t\tvoid clear(); \t\tWORD setEmitPointer(WORD address); \t\tWORD getEmitPointer(); \t\tWORD emit(WORD opcode); \t\tWORD emit(WORD opcode, WORD operand); \t\tWORD readWord(WORD address); \t\tvoid writeWord(WORD address, WORD value); \t\tWORD writeData(WORD address, void* data, size_t length); \t\tvoid* getImage(); \t\tsize_t getImageSize(); \t\tvoid dissasemble(); \tprivate: \t\tWORD memory[MAX_MEMORY]; \t\tWORD imageSize; \t\tWORD ep;  \t};<\/code><\/pre>\n<p>\u0418 \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u0435\u043d\u044c\u043a\u0443\u044e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u0441 \u0446\u0438\u043a\u043b\u043e\u043c \u0438 \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0435\u0447\u0430\u0442\u0430\u0435\u0442 &#171;Hello, world from VM!&#187; 10 \u0440\u0430\u0437, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0447\u0442\u043e \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u0430\u044f \u043c\u0430\u0448\u0438\u043d\u0430 \u0431\u043e\u043b\u0435\u0435 \u043c\u0435\u043d\u0435\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u041d\u0430 \u0430\u0441\u0441\u0435\u043c\u0431\u043b\u0435\u0440\u0435 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 \u043c\u0430\u0448\u0438\u043d\u044b (\u043c\u043d\u0435\u043c\u043e\u043d\u0438\u043a\u0438 \u043f\u043e\u043a\u0430 \u0431\u0443\u0434\u0443\u0442 \u043e\u0447\u0435\u043d\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0435, \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u0434\u043e \u043a\u043e\u043d\u0446\u0430 \u043d\u0435 \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043b) \u044d\u0442\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code>start:                           \/\/ \u0410\u0434\u0440\u0435\u0441 [0] push   iVar                      \/\/ \u0437\u0430\u0442\u043e\u043b\u043a\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 iVar \u0432 \u0441\u0442\u0435\u043a dec                              \/\/ \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u043c \u043d\u0430 \u043e\u0434\u0438\u043d call   fn                        \/\/ \u0432\u044b\u0437\u043e\u0432\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e fn dup                              \/\/ \u043f\u0440\u043e\u0434\u0443\u0431\u043b\u0438\u0440\u0443\u0435\u043c \u0432\u0435\u0440\u0445\u043d\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0441\u0442\u0435\u043a\u0435 (Top Of Stack) pop    iVar                      \/\/ \u0432\u044b\u0442\u0430\u0449\u0438\u043c \u0432\u0435\u0440\u0445\u043d\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0442\u0435\u043a\u0430 \u0438 \u0437\u0430\u043f\u0438\u0448\u0435\u043c \u0432 iVar const  0                         \/\/ \u0437\u0430\u0442\u043e\u043b\u043a\u0430\u0435\u043c \u0432 \u0441\u0442\u0435\u043a \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0443 0 cmpjg  start                     \/\/ \u0435\u0441\u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 TOS (iVar) \u0431\u043e\u043b\u044c\u0448\u0435 0 \u043f\u0440\u044b\u0433\u0430\u0435\u043c \u043d\u0430 start: halt                             \/\/ \u0438\u043d\u0430\u0447\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443   fn:                              \/\/ \u0410\u0434\u0440\u0435\u0441 [64] const  myStr                     \/\/ \u0437\u0430\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0435\u043c \u0432 \u0441\u0442\u0435\u043a \u0430\u0434\u0440\u0435\u0441 \u0441\u0442\u0440\u043e\u043a\u0438 syscall 0x20                     \/\/ \u0434\u0435\u043b\u0430\u0435\u043c \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c ret                              \/\/ \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u0441\u044f \u0442\u0443\u0434\u0430 \u043e\u0442\u043a\u0443\u0434\u0430 \u043d\u0430\u0441 \u0432\u044b\u0437\u0432\u0430\u043b\u0438   dataSeg:                         \/\/ \u0410\u0434\u0440\u0435\u0441 [128] iVar = 10                        myStr = \"Hello, world from VM!\\n\"<\/code><\/pre>\n<p>\u0421\u0435\u0439\u0447\u0430\u0441 \u043b\u0435\u043d\u044c \u043f\u0438\u0441\u0430\u0442\u044c \u043f\u043e\u0434 \u044d\u0442\u0443 \u0437\u0430\u0434\u0430\u0447\u0443 \u0442\u0440\u0430\u043d\u0441\u043b\u044f\u0442\u043e\u0440 \u0434\u043b\u044f \u0430\u0441\u0441\u0435\u043c\u0431\u043b\u0435\u0440\u0430 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 \u043c\u0430\u0448\u0438\u043d\u044b, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u043c \u0432\u044b\u0441\u043e\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u044b\u0439 \u044f\u0437\u044b\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u043c \u0441\u0445\u043e\u0434\u0443 \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 \u043c\u0430\u0448\u0438\u043d\u044b. \u041d\u043e \u0447\u0442\u043e\u0431\u044b \u044d\u0442\u043e \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0439 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 \u043c\u0430\u0448\u0438\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0437 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u043a\u043b\u0430\u0441\u0441\u043e\u043c VMImage.:<\/p>\n<pre><code>void createExecutableImage(VMImage* img) { \t \tWORD dataSeg = 256;\t\t\t\t\t\t\t\/\/ Data segment starts at 256 \t \tWORD iVar = dataSeg; \tWORD myStr = dataSeg + 1; \timg-&gt;writeWord(iVar, 10); \timg-&gt;writeData(myStr, \"Hello, world from VM!\\n\", 23);     \t \tWORD fn = 128;  \tWORD start = img-&gt;emit(OP_PUSH, iVar);      \/\/ stack &lt;- [iVar] (operand 1) \timg-&gt;emit(OP_DEC);                          \/\/ stack[top]--  (operand 1 decrement) \timg-&gt;emit(OP_CALL, fn);                     \/\/ Call function fn()      \timg-&gt;emit(OP_DUP);                          \/\/ duplicate stack top (operand 1 duplicate) \timg-&gt;emit(OP_POP, iVar);                    \/\/ stack -&gt; [iVar] (pop operand 1 duplicate to iVar) \timg-&gt;emit(OP_CONST, 0);                     \/\/ push const 0 (operand 2) \timg-&gt;emit(OP_CMPJG, start);                 \/\/ if (operand1 &gt; operand2) jump to addr            \timg-&gt;emit(OP_HALT);                         \/\/ end of program  \timg-&gt;setEmitPointer(fn);                    \/\/ Function fn() \timg-&gt;emit(OP_CONST, myStr);                 \/\/ Push constant string address \timg-&gt;emit(OP_SYSCALL, 0x20);                \/\/ Call system call 0x20, to print C style string to standard output \timg-&gt;emit(OP_RET);                          \/\/ Return    }<\/code><\/pre>\n<p>\u0410 \u0437\u0430\u0442\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043d\u0430\u0448\u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u043e\u0433\u043e \u043e\u0431\u0440\u0430\u0437\u0430 \u043d\u0430 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 \u043c\u0430\u0448\u0438\u043d\u0435, \u0437\u0430\u043c\u0435\u0440\u0438\u0432 \u0432\u0440\u0435\u043c\u044f \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f:<\/p>\n<pre><code>int main() { \tVMImage* img = new VMImage(); \tcreateExecutableImage(img); \tVMRuntime* vm = new VMRuntime(); \tvm-&gt;loadImage(img-&gt;getImage(), img-&gt;getImageSize());   auto start = std::chrono::high_resolution_clock::now(); \t   vm-&gt;run();    \tauto end = std::chrono::high_resolution_clock::now(); \tauto ms_int = chrono::duration_cast&lt;chrono::nanoseconds&gt;(end - start).count(); \tcout &lt;&lt; \"EXECUTION TIME: \" &lt;&lt; ms_int \/ 1000000000.0 &lt;&lt; \"s\" &lt;&lt; endl; \t   delete vm;   delete img; }<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7e2\/f1f\/881\/7e2f1f881060b4ed1f32f938fd900fb3.jpg\" width=\"311\" height=\"203\"><figcaption><\/figcaption><\/figure>\n<p>\u0423\u0440\u0430! \u041a\u043b\u0430\u0441\u0441\u043d\u043e! \u0420\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u043a\u0430, \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u0432 \u0438 \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0439!<br \/>\u042d\u0442\u043e \u0432\u043e\u043e\u0434\u0443\u0448\u0435\u0432\u043b\u044f\u0435\u0442. \u0412\u0438\u0434\u0438\u043c\u043e \u0434\u0430\u043b\u044c\u0448\u0435 \u0431\u0443\u0434\u0443 \u0440\u0430\u0437\u0432\u0438\u0432\u0430\u0442\u044c \u044d\u0442\u0443 \u0438\u0441\u0442\u043e\u0440\u0438\u044e&#8230;<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/560356\/\"> https:\/\/habr.com\/ru\/post\/560356\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0422\u0430\u043a \u0441\u043b\u043e\u0436\u0438\u043b\u043e\u0441\u044c, \u0447\u0442\u043e \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 18 \u043b\u0435\u0442, \u043d\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430 C\/C++. \u041d\u0430 \u0440\u0430\u0431\u043e\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0430\u0441\u044c Java, \u0434\u0430 \u0438 \u0432\u0432\u0438\u0434\u0443 \u0434\u043e\u043b\u0436\u043d\u043e\u0441\u0442\u0435\u0439 \u0434\u0435\u044f\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435 \u0431\u044b\u043b\u0430 \u0441\u0432\u044f\u0437\u0430\u043d\u0430 \u0441 \u043f\u0440\u0435\u0434\u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e\u043c &#8212; \u043f\u0435\u0440\u0435\u0433\u043e\u0432\u043e\u0440\u044b, \u043a\u043e\u0440\u043f\u043e\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0435 \u043f\u0440\u043e\u0434\u0430\u0436\u0438, \u0432\u044b\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0441\u0434\u0435\u043b\u043e\u043a. \u0417\u0430\u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0432 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0435 \u043e\u0442 \u0440\u0430\u0431\u043e\u0442\u044b \u0432\u0440\u0435\u043c\u044f \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043d\u0430\u0432\u044b\u043a\u0438, \u0440\u0430\u0437\u043c\u044f\u0442\u044c \u0447\u0430\u0441\u0442\u044c \u043c\u043e\u0437\u0433\u0430 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0435 \u043d\u0430\u043f\u0440\u044f\u0433\u0430\u043b \u0432\u0441\u0435 18 \u043b\u0435\u0442 \u0438, \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u043d\u0430\u0447\u0430\u0442\u044c \u0441 \u0441\u0430\u043c\u044b\u0445 \u043e\u0441\u043d\u043e\u0432. \u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0442\u044c \u0441\u0435\u0431\u0435 \u0437\u0430\u0434\u0430\u0447\u0443.<\/p>\n<p>\u0412 \u0443\u043d\u0438\u0432\u0435\u0440\u0435 \u043f\u0440\u0435\u043f\u043e\u0434\u0430\u0432\u0430\u0442\u0435\u043b\u0438, \u043c\u043e\u043b\u043e\u0434\u043e\u0441\u0442\u044c \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u043b\u0430\u0441\u044c \u043d\u0430 70-80\u0435 \u0433\u043e\u0434\u044b, \u0434\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u043e-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0443\u0431\u0438\u0432\u0430\u043b\u0438\u0441\u044c \u043f\u043e \u0442\u0435\u043c\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u043e\u0432 (\u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u043e\u0432, \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u0432) \u043f\u043e\u0434 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043d\u044b\u0435 \u043e\u0431\u043b\u0430\u0441\u0442\u0438. \u0412\u0441\u0451 \u044d\u0442\u043e \u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u043d\u0435\u0432\u043e\u0441\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u043d\u044b\u043c &#171;\u0441\u0442\u0430\u0440\u044c\u0451\u043c&#187;, \u043d\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u044b\u0445 \u044f\u0437\u044b\u043a\u043e\u0432 \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0434\u0435\u0441\u044f\u0442\u0438\u043b\u0435\u0442\u0438\u0435 (Go, Kotlin \u0438 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0434\u0440\u0443\u0433\u0438\u0445) \u043f\u043e\u0432\u044b\u0441\u0438\u043b\u0438 \u043c\u043e\u0439 \u0438\u043d\u0442\u0435\u0440\u0435\u0441 \u043a \u044d\u0442\u043e\u0439 \u0442\u0435\u043c\u0435.<\/p>\n<p>\u0420\u0435\u0448\u0438\u043b \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0445\u043e\u0431\u0431\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c <strong>32-bit \u0441\u0442\u0435\u043a\u043e\u0432\u0443\u044e \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u0443\u044e \u043c\u0430\u0448\u0438\u043d\u0443 \u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 C \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u044f\u0437\u044b\u043a\u0430 \u043f\u043e\u0434 \u043d\u0435\u0451<\/strong>, \u0447\u0442\u043e\u0431\u044b \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u043d\u0430\u0432\u044b\u043a\u0438. \u0422\u0430\u043a\u0430\u044f \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0430\u044f Computer Science \u0437\u0430\u0434\u0430\u0447\u043a\u0430 \u0434\u043b\u044f \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432\u0435\u0447\u0435\u0440\u043e\u0432 \u0441 \u043f\u0438\u0432\u043e\u043c. \u041a\u0430\u043a \u043f\u0440\u0435\u0434\u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c, \u044f \u0447\u0435\u0442\u043a\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u044e, \u0447\u0442\u043e \u043e\u043d\u0430 \u043d\u0438\u043a\u043e\u043c\u0443 \u043d\u0435 \u043d\u0443\u0436\u043d\u0430, \u043d\u043e \u0442\u0430\u043a\u0430\u044f \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0430 \u043d\u0443\u0436\u043d\u0430 \u043c\u043d\u0435 \u0434\u043b\u044f \u044d\u0441\u0442\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0438\u043d\u0436\u0435\u043d\u0435\u0440\u043d\u043e\u0433\u043e \u0443\u0434\u043e\u0432\u043e\u043b\u044c\u0441\u0442\u0432\u0438\u044f. \u041f\u043b\u044e\u0441 \u043a\u043e\u0433\u0434\u0430 \u043e\u0431 \u044d\u0442\u043e\u043c \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0448\u044c \u0441\u0430\u043c \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u0448\u044c \u0433\u043b\u0443\u0431\u0436\u0435. \u0421 \u0446\u0435\u043b\u044c\u044e \u0438 \u043c\u043e\u0442\u0438\u0432\u0430\u043c\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043b\u0441\u044f. \u041d\u0430\u0447\u043d\u0451\u043c.<\/p>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u044d\u0442\u043e \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u0430\u044f \u043c\u0430\u0448\u0438\u043d\u0430, \u043c\u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u0441 \u0435\u0451 \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0430\u043c\u0438: <\/p>\n<p><strong>CPU<\/strong>: 32-bit\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u043a\u043e\u043c\u0430\u043d\u0434, \u0442\u0430\u043a \u043a\u0430\u043a \u043c\u0430\u0448\u0438\u043d\u0430 \u0441\u0442\u0435\u043a\u043e\u0432\u0430\u044f, \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043e\u043f\u0435\u0440\u0430\u043d\u0434\u044b \u043a\u043e\u043c\u0430\u043d\u0434 \u0445\u0440\u0430\u043d\u0438\u043c \u0432 \u0441\u0442\u0435\u043a\u0435, \u0438\u0437 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u043e\u0432 \u0442\u043e\u043b\u044c\u043a\u043e IP (Instruction Pointer) \u0438 SP (Stack Pointer), \u043f\u043e\u043a\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0441 \u0446\u0435\u043b\u044b\u043c\u0438 \u0447\u0438\u0441\u043b\u0430\u043c\u0438 \u0441\u043e \u0437\u043d\u0430\u043a\u043e\u043c (__int32), \u043f\u043e\u0437\u0436\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445. <\/p>\n<p><strong>RAM<\/strong>: \u043f\u0443\u0441\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u043e\u043a\u0430 \u0431\u0443\u0434\u0435\u0442 65536 \u044f\u0447\u0435\u0435\u043a \u043f\u043e 32-bit`\u0430.<\/p>\n<p>\u041f\u0440\u0435\u0434\u0443\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0432\u043e\u0442 \u0442\u0430\u043a\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438:<\/p>\n<pre><code>  typedef __int32 WORD;    \tconstexpr WORD OP_CODE_MASK = 0b00000000000000000000000011111111; \tconstexpr WORD OP_TYPE_MASK = 0b00000000000000000000111000000000;  \tconstexpr WORD OP_HALT      = 0b00000000000000000000000000000000; \tconstexpr WORD OP_CONST     = 0b00000000000000000000000000000001; \tconstexpr WORD OP_PUSH      = 0b00000000000000000000000000000010; \tconstexpr WORD OP_POP       = 0b00000000000000000000000000000011;  \tconstexpr WORD OP_INC       = 0b00000000000000000000000000000100; \tconstexpr WORD OP_DEC       = 0b00000000000000000000000000000101; \tconstexpr WORD OP_ADD       = 0b00000000000000000000000000000110; \tconstexpr WORD OP_SUB       = 0b00000000000000000000000000000111; \tconstexpr WORD OP_MUL       = 0b00000000000000000000000000001000; \tconstexpr WORD OP_DIV       = 0b00000000000000000000000000001001;  \tconstexpr WORD OP_AND       = 0b00000000000000000000000000001010; \tconstexpr WORD OP_OR        = 0b00000000000000000000000000001011; \tconstexpr WORD OP_XOR       = 0b00000000000000000000000000001100; \tconstexpr WORD OP_NOT       = 0b00000000000000000000000000001101; \tconstexpr WORD OP_SHL       = 0b00000000000000000000000000001110; \tconstexpr WORD OP_SHR       = 0b00000000000000000000000000001111;  \tconstexpr WORD OP_JMP       = 0b00000000000000000000000000010001; \tconstexpr WORD OP_CMPJE     = 0b00000000000000000000000000010010; \tconstexpr WORD OP_CMPJNE    = 0b00000000000000000000000000010011; \tconstexpr WORD OP_CMPJG     = 0b00000000000000000000000000010100; \tconstexpr WORD OP_CMPJGE    = 0b00000000000000000000000000010101; \tconstexpr WORD OP_CMPJL     = 0b00000000000000000000000000010110; \tconstexpr WORD OP_CMPJLE    = 0b00000000000000000000000000010111;  \tconstexpr WORD OP_DUP       = 0b00000000000000000000000000011000; \tconstexpr WORD OP_CALL      = 0b00000000000000000000000000011001; \tconstexpr WORD OP_RET       = 0b00000000000000000000000000011010; \tconstexpr WORD OP_SYSCALL   = 0b00000000000000000000000000011011;  \tconstexpr WORD OP_RESERVED1 = 0b00000000000000000000000000011100; \tconstexpr WORD OP_RESERVED2 = 0b00000000000000000000000000011101; \tconstexpr WORD OP_RESERVED3 = 0b00000000000000000000000000011110; \tconstexpr WORD OP_RESERVED4 = 0b00000000000000000000000000011111;      constexpr WORD MAX_MEMORY = 65536;<\/code><\/pre>\n<p>\u041f\u043e\u043a\u0430 \u043f\u0435\u0440\u0432\u044b\u0435 8 \u0438\u0437 32 \u0431\u0438\u0442 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0434 \u043a\u043e\u043c\u0430\u043d\u0434\u0443 (opcode), 1 \u0431\u0438\u0442 \u0437\u0430\u0440\u0435\u0437\u0435\u0440\u0432\u0438\u0440\u0443\u0435\u043c \u043f\u043e\u0434 \u0442\u0438\u043f \u043e\u043f\u0435\u0440\u0430\u043d\u0434\u0430 (immediate \u0438\u043b\u0438 \u0438\u0437 \u0441\u0442\u0435\u043a\u0430), 3 \u0431\u0438\u0442\u0430 \u043f\u043e\u0434 \u0431\u0443\u0434\u0443\u0449\u0438\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445 (byte, short, int, long, char, float, double \u0438 \u0447\u0442\u043e \u043d\u0438\u0431\u0443\u0434\u044c \u0435\u0449\u0451), \u0430 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0431\u0438\u0442\u044b \u0434\u043b\u044f \u0447\u0435\u0433\u043e \u043d\u0438\u0431\u0443\u0434\u044c \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c. \u041f\u043e\u043a\u0430 \u043d\u0435 \u0437\u043d\u0430\u044e.<\/p>\n<pre><code>class VMRuntime { \tpublic: \t\tVMRuntime();                                \/\/ Constructor \t\t~VMRuntime();                               \/\/ Desctructor \t\tbool loadImage(void* image, size_t size);   \/\/ Load executable image \t\tvoid run();                                 \/\/ Runs image from address 0 \t\tWORD readWord(WORD address);                \/\/ Read WORD from memory \t\tvoid writeWord(WORD address, WORD value);   \/\/ Write WORD to memory  \t\tWORD getMaxAddress();                       \/\/ Get max address in 32-bit words \t\tWORD getIP();                               \/\/ Get Instruction Pointer address \t\tWORD getSP();                               \/\/ Get Stack Pointer address \tprivate: \t\tWORD  memory[MAX_MEMORY];                   \/\/ Random access memory array \t\tWORD  ip;                                   \/\/ Instruction pointer \t\tWORD  sp;                                   \/\/ Stack pointer \t\tWORD  fp;                                   \/\/ Frame pointer \t\tvoid systemCall(WORD n);                    \/\/ System call \t\tvoid printState();                          \/\/ Print current VM state \t};<\/code><\/pre>\n<p>\u0412 \u043a\u043b\u0430\u0441\u0441\u0435 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 \u043c\u0430\u0448\u0438\u043d\u0435 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0442\u0430\u043a\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0439 \u043e\u0431\u0440\u0430\u0437 \u0432 \u043f\u0430\u043c\u044f\u0442\u044c \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 \u043c\u0430\u0448\u0438\u043d\u044b (loadImage), \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 (run), \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0442\u044c \u0441\u043d\u0430\u0440\u0443\u0436\u0438 \u0447\u0438\u0442\u0430\u0442\u044c\/\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c (readWord, writeWord), \u0447\u0438\u0442\u0430\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u043e\u0432 IP, SP. \u0418\u0437 \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0432\u044b\u0432\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043c\u0430\u0448\u0438\u043d\u044b (\u0440\u0430\u0441\u043f\u0435\u0447\u0430\u0442\u043a\u0430 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u043e\u0432 \u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u044f \u0441\u0442\u0435\u043a\u0430), \u0430 \u0442\u0430\u043a\u0436\u0435 \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432, \u0447\u0442\u043e\u0431\u044b \u043a\u043e\u0434 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 \u043c\u0430\u0448\u0438\u043d\u044b \u043c\u043e\u0433 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0441\u043d\u0430\u0440\u0443\u0436\u0438 (\u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u043e\u0431\u0440\u043e\u0441 \u043a\u0430\u043a\u043e\u0433\u043e-\u043d\u0438\u0431\u0443\u0434\u044c API).<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u043e &#8212; \u0448\u0430\u0433\u0430\u0435\u043c \u043f\u043e \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0447\u0438\u0442\u0430\u0435\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u0443, \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0431\u0435\u0440\u0451\u043c \u043e\u043f\u0435\u0440\u0430\u043d\u0434\u044b \u043b\u0438\u0431\u043e \u0438\u0437 \u0441\u0442\u0435\u043a\u0430, \u043b\u0438\u0431\u043e \u0447\u0438\u0442\u0430\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u043d\u0434 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u044f\u0447\u0435\u0439\u043a\u0435 \u043f\u043e\u0441\u043b\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b. \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u043c \u043f\u043e\u043a\u0430 \u043d\u0435 \u0434\u043e\u0439\u0434\u0451\u043c \u0434\u043e \u043a\u043e\u043c\u0430\u043d\u0434\u044b HALT.<\/p>\n<pre><code>void VMRuntime::run() { \tWORD a, b; \tWORD opcode;  \tip = 0; \tsp = MAX_MEMORY - 1;  \twhile (1) {  \t\topcode = memory[ip++]; \t\t \t\tswitch (opcode) { \t\t\/\/------------------------------------------------------------------------ \t\t\/\/ STACK OPERATIONS \t\t\/\/------------------------------------------------------------------------ \t\tcase OP_CONST:  \t\t    memory[--sp] = memory[ip++];  \t\t\tbreak; \t\tcase OP_PUSH: \t\t\tmemory[--sp] = memory[memory[ip++]]; \t\t\tbreak; \t\tcase OP_POP:   \t\t  memory[memory[ip++]] = memory[sp++];  \t\t\tbreak; \t\tcase OP_DUP: \t\t\ta = memory[sp]; \t\t\tmemory[--sp] = a; \t\t\tbreak; \t\t\/\/------------------------------------------------------------------------ \t\t\/\/ ARITHMETIC OPERATIONS \t\t\/\/------------------------------------------------------------------------ \t\tcase OP_INC: \t\t\tmemory[sp]++; \t\t\tbreak; \t\tcase OP_DEC: \t\t\tmemory[sp]--; \t\t\tbreak; \t\tcase OP_ADD:   \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tmemory[--sp] = a + b; \t\t\tbreak; \t\tcase OP_SUB: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tmemory[--sp] = a - b; \t\t\tbreak; \t\tcase OP_MUL: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tmemory[--sp] = a * b; \t\t\tbreak; \t\tcase OP_DIV: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tmemory[--sp] = a \/ b; \t\t\tbreak; \t\t\/\/------------------------------------------------------------------------ \t\t\/\/ BITWISE OPERATIONS \t\t\/\/------------------------------------------------------------------------ \t\tcase OP_AND: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tmemory[--sp] = a &amp; b; \t\t\tbreak; \t\tcase OP_OR: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tmemory[--sp] = a | b; \t\t\tbreak; \t\tcase OP_XOR: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tmemory[--sp] = a ^ b; \t\t\tbreak; \t\tcase OP_NOT: \t\t\ta = memory[sp++]; \t\t\tmemory[--sp] = ~a; \t\t\tbreak; \t\tcase OP_SHL: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tmemory[--sp] = a &lt;&lt; b; \t\t\tbreak; \t\tcase OP_SHR: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tmemory[--sp] = a &gt;&gt; b; \t\t\tbreak; \t\t\/\/------------------------------------------------------------------------ \t\t\/\/ FLOW CONTROL OPERATIONS \t\t\/\/------------------------------------------------------------------------ \t\tcase OP_JMP: \t\t\tip = memory[ip]; \t\t\tbreak; \t\tcase OP_CMPJE: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tif (a == b) ip = memory[ip]; else ip++; \t\t\tbreak; \t\tcase OP_CMPJNE: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tif (a != b) ip = memory[ip]; else ip++; \t\t\tbreak; \t\tcase OP_CMPJG: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tif (a &gt; b) ip = memory[ip]; else ip++; \t\t\tbreak; \t\tcase OP_CMPJGE: \t\t\ta = memory[sp++]; \t\t\tb = memory[sp++]; \t\t\tif (a &gt;= b) ip = memory[ip]; else ip++; \t\t\tbreak; \t\tcase OP_CMPJL: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tif (a &lt; b) ip = memory[ip]; else ip++; \t\t\tbreak; \t\tcase OP_CMPJLE: \t\t\tb = memory[sp++]; \t\t\ta = memory[sp++]; \t\t\tif (a &lt;= b) ip = memory[ip]; else ip++; \t\t\tbreak; \t\t\/\/------------------------------------------------------------------------ \t\t\/\/ PROCEDURE CALL OPERATIONS \t\t\/\/------------------------------------------------------------------------ \t\tcase OP_CALL: \t\t\ta = memory[ip++]; \t\t\tmemory[--sp] = ip;        \t\t\tip = a;                   \t\t\tbreak; \t\tcase OP_RET: \t\t\tip = memory[sp++];        \t\t\tbreak; \t\tcase OP_SYSCALL: \t\t\ta = memory[ip++]; \t\t\tsystemCall(a); \t\t\tbreak; \t\tcase OP_HALT:  \t\t\tprintState(); \t\t  return; \t\tdefault: \t\t\tcout &lt;&lt; \"Runtime error - unknown opcode=\" &lt;&lt; opcode &lt;&lt; endl; \t\t\tprintState(); \t\t\treturn; \t\t} \t}      \/\/ Only one system call implemented - print string (0x20)   void VMRuntime::systemCall(WORD n) { \t\tWORD ptr; \t\tswitch (n) { \t\tcase 0x20:  \/\/ print C style string \t\t\tptr = memory[sp++]; \t\t\tcout &lt;&lt; ((char*)&amp;memory[ptr]); \t\tbreak; \t} }<\/code><\/pre>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u0435\u043d\u044c\u043a\u0443\u044e \u043c\u0430\u0448\u0438\u043d\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0443\u043c\u0435\u0435\u0442 \u0432 \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0438 \u0431\u0438\u0442\u043e\u0432\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0445\u043e\u0434\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0441 \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u043c\u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430\u043c\u0438, \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (\u043f\u043e\u043a\u0430 \u0447\u0435\u0442\u043a\u043e\u0439 \u043a\u043e\u043d\u0432\u0435\u043d\u0446\u0438\u0438 \u043f\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0447\u0435\u0440\u0435\u0437 \u0441\u0442\u0435\u043a \u043d\u0435\u0442, \u043d\u043e \u0434\u0430\u043b\u044c\u0448\u0435 \u0434\u043e\u0434\u0435\u043b\u0430\u0435\u043c). <\/p>\n<p>\u0415\u0449\u0435 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f<\/p>\n<p><\/br><\/p>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-324123","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/324123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=324123"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/324123\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=324123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=324123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=324123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}