ํƒœ๊ทธ ๋ณด๊ด€๋ฌผ: stack

stack

๊ฐ€์žฅ ์ž‘์€ ๋ฐ”์ดํŠธ ์ฝ”๋“œ ์ธํ„ฐํ”„๋ฆฌํ„ฐ / VM BRANCH๋ฅผ ํ˜ธ์ถœ ํ•œ ๊ฒฝ์šฐ BRANCHEQ๋ฅผ

๋ฆฌ๋” ๋ณด๋“œ-JIT ์ปดํŒŒ์ผ (๋‚ฎ์„์ˆ˜๋ก ์ข‹์Œ)

  1. es1024-81.2 ์  (์ž‘๋™์ค‘์ธ ์ปดํŒŒ์ผ๋Ÿฌ ํฌํ•จ)
  2. ํ‚ค ์—ํŠธ ๋žœ๋‹ฌ-116 ํฌ์ธํŠธ
  3. ์—˜-121 ํฌ์ธํŠธ

๋ฆฌ๋” ๋ณด๋“œ-ํ•ด์„ ๋จ (๋‚ฎ์„์ˆ˜๋ก ์ข‹์Œ)

  1. Martin Bรผttner-706654 ์  (์•ฝ 2 ์‹œ๊ฐ„ ์ •๋„).
  2. criptych-30379 ํฌ์ธํŠธ (97 ์ดˆ)

๋‹น์‹ ์˜ ์ž„๋ฌด๋Š” ๋‹น์‹ ์ด ๊ทธ๊ฒƒ์„ ๋ฐ›์•„๋“ค์ด๋„๋ก ์„ ํƒํ•œ๋‹ค๋ฉด ๊ฐ€๋Šฅํ•œ ๊ฐ€์žฅ ์ž‘์€ ๋ฐ”์ดํŠธ ์ฝ”๋“œ ์ธํ„ฐํ”„๋ฆฌํ„ฐ / VM์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. VM / ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋Š” ์•„๋ž˜์— ์ง€์ •๋œ ์–ธ์–ด๋กœ ์ž‘์€ CISC ์•„ํ‚คํ…์ฒ˜ (์ž‘์—… ํฌ๊ธฐ๊ฐ€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Œ)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์™„๋ฃŒ๋˜๋ฉด 3 ๊ฐœ์˜ CPU ๋ ˆ์ง€์Šคํ„ฐ ๊ฐ’์„ ์ธ์‡„ํ•˜์—ฌ ์˜ฌ๋ฐ”๋ฅธ ์ถœ๋ ฅ์ด ์ธ์‡„๋˜์—ˆ์Œ์„ ์ฆ๋ช…ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค (3,126,900,366).

์ปดํŒŒ์ผ๋Ÿฌ

์ž์ฒด ํ…Œ์ŠคํŠธ๋ฅผ ์›ํ•œ๋‹ค๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์•„๋ž˜์— ๊ฒŒ์‹œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ต์„ ๊ฐ€์ง€๊ณ  ํ…Œ์ŠคํŠธ๋ฅผ ๊ฒŒ์‹œํ•˜์‹ญ์‹œ์˜ค.

window.compile=function(){var e=$("#text").val().toLowerCase().match(/[^\r\n]+/g);var t=[];for(var n=0;n<e.length;n++)compileLine(e[n],t);var r="";for(var n=0;n<t.length;n++)if(typeof t[n]=="string")r+="\n";else r+="0x"+t[n].toString(16)+" ";$("#compiledArray").val(r)};window.compileLine=function(e,t){var n=e.split(" ");if(n[0]=="load"){t.push(0);t.push(getInt(n[1]));t.pushArr(getInt(n[2]))}if(n[0]=="rload"){t.push(1);t.push(getInt(n[1]));t.push(getInt(n[1]))}if(n[0]=="push"){t.push(2);t.push(getInt(n[1]))}if(n[0]=="pop"){t.push(3);t.push(getInt(n[1]))}if(n[0]=="add"){t.push(4);t.push(getInt(n[1]));t.push(getInt(n[2]))}if(n[0]=="sub"){t.push(5);t.push(getInt(n[1]));t.push(getInt(n[2]))}if(n[0]=="mul"){t.push(6);t.push(getInt(n[1]));t.push(getInt(n[2]))}if(n[0]=="div"){t.push(7);t.push(getInt(n[1]));t.push(getInt(n[2]))}if(n[0]=="jmp"){t.push(8);t.pushArr(getInt(n[1]))}if(n[0]=="cmp"){t.push(9);t.push(getInt(n[1]));t.push(getInt(n[2]))}if(n[0]=="branchlt"){t.push(10);t.pushArr(getInt(n[1]))}if(n[0]=="brancheq"){t.push(11);t.pushArr(getInt(n[1]))}if(n[0]=="branchgt"){t.push(12);t.pushArr(getInt(n[1]))}if(n[0]=="branchne"){t.push(13);t.pushArr(getInt(n[1]))}t.push("NEW LINE")};window.getInt=function(e){if(e.trim().startsWith("<--"))return"COMMENT";if(e=="r0")return 0;if(e=="r1")return 1;if(e=="r2")return 2;if(e.startsWith("0x"))return parseInt(e,16);if(isNaN(parseInt(e)))alert(e);return getIntBytes(parseInt(e))};if(typeof String.prototype.startsWith!="function"){String.prototype.startsWith=function(e){return this.slice(0,e.length)==e}}Array.prototype.pushArr=function(e){this.push.apply(this,e)};window.getIntBytes=function(e){var t=[];var n=4;do{t[--n]=e&255;e=e>>8}while(n);return t}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<textarea id="text" cols="40" rows="10"></textarea>
<br/>
<button onclick="compile()">Compile</button>
<br/>
<textarea id="compiledArray"  cols="40" rows="10" readonly></textarea>

โ€œVMโ€์‚ฌ์–‘

VM์—๋Š” 3 ๊ฐœ์˜ 32 ๋น„ํŠธ ๋ถ€ํ˜ธ์—†๋Š” ์ •์ˆ˜ ๋ ˆ์ง€์Šคํ„ฐ R0, R1, R2๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 16 ์ง„์ˆ˜๋กœ 0x00, 0x01 ๋ฐ 0x02๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ์ž‘์—…์ด ์ง€์›๋˜์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.

ํ˜•์‹์€ [์ด๋ฆ„] [โ€ฆ ํ”ผ์—ฐ์‚ฐ์ž โ€ฆ], [16 ์ง„์ˆ˜ ์—ฐ์‚ฐ ์ฝ”๋“œ] [โ€ฆ ํ”ผ์—ฐ์‚ฐ์ž ๋ฐ˜๋ณต โ€ฆ]์ž…๋‹ˆ๋‹ค.

  • LOAD [๋ ˆ์ง€์Šคํ„ฐ] [4 ๋ฐ”์ดํŠธ ๊ฐ’], 0x00 [๋ ˆ์ง€์Šคํ„ฐ] [4 ๋ฐ”์ดํŠธ ๊ฐ’]
  • PUSH [๋“ฑ๋ก], 0x02 [๋“ฑ๋ก]
  • POP [๋“ฑ๋ก], 0x03 [๋“ฑ๋ก]
  • ADD [๋ ˆ์ง€์Šคํ„ฐ, 1 ๋ฐ”์ดํŠธ] [๋ ˆ์ง€์Šคํ„ฐ, 1 ๋ฐ”์ดํŠธ], 0x04 [๋ ˆ์ง€์Šคํ„ฐ] [๋ ˆ์ง€์Šคํ„ฐ]
  • SUB [๋ ˆ์ง€์Šคํ„ฐ, 1 ๋ฐ”์ดํŠธ] [๋ ˆ์ง€์Šคํ„ฐ, 1 ๋ฐ”์ดํŠธ], 0x05 [๋ ˆ์ง€์Šคํ„ฐ] [๋ ˆ์ง€์Šคํ„ฐ]
  • MUL [๋ ˆ์ง€์Šคํ„ฐ, 1 ๋ฐ”์ดํŠธ] [๋ ˆ์ง€์Šคํ„ฐ, 1 ๋ฐ”์ดํŠธ], 0x06 [๋ ˆ์ง€์Šคํ„ฐ] [๋ ˆ์ง€์Šคํ„ฐ]
  • DIV [register, 1 byte] [register, 1 byte], 0x07 [register] [register]
  • JMP [์ฝ”๋“œ ๋ผ์ธ, 4 ๋ฐ”์ดํŠธ], 0x08 [4 ๋ฐ”์ดํŠธ ์ฝ”๋“œ ๋ผ์ธ ๋ฒˆํ˜ธ]
  • CMP [๋ ˆ์ง€์Šคํ„ฐ, 1 ๋ฐ”์ดํŠธ] [๋ ˆ์ง€์Šคํ„ฐ, 1 ๋ฐ”์ดํŠธ], 0x09 [๋ ˆ์ง€์Šคํ„ฐ] [๋ ˆ์ง€์Šคํ„ฐ]
  • BRANCHLT [์ฝ”๋“œ ๋ผ์ธ, 4 ๋ฐ”์ดํŠธ], 0x0a [4 ๋ฐ”์ดํŠธ ์ฝ”๋“œ ๋ผ์ธ ๋ฒˆํ˜ธ]

๋ช‡ ๊ฐ€์ง€ ์ฐธ๊ณ  ์‚ฌํ•ญ :

  • ์œ„์˜ ์ˆ˜ํ•™ ์—ฐ์‚ฐ์€ 2 ๊ฐœ์˜ ๋ ˆ์ง€์Šคํ„ฐ ๊ฐ’์„ ๋”ํ•˜์—ฌ ์ถœ๋ ฅ์„ ์ฒซ ๋ฒˆ์งธ ๋ ˆ์ง€์Šคํ„ฐ์— ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค.
  • ๋น„๊ต ์—ฐ์‚ฐ์ž ์ธ CMP๋Š” 2 ๊ฐœ์˜ ๋ ˆ์ง€์Šคํ„ฐ ๊ฐ’์„ ๋น„๊ตํ•˜๊ณ  ๋‚˜์ค‘์— ๋ถ„๊ธฐ ๋ช…๋ น์–ด์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ผ๋ถ€ ๋‚ด๋ถ€ ํ”Œ๋ž˜๊ทธ (๊ตฌํ˜„์— ๋”ฐ๋ผ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Œ)์— ์ถœ๋ ฅ์„ ์ €์žฅํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
  • CRAN ์ „์— BRANCH๋ฅผ ํ˜ธ์ถœ ํ•œ ๊ฒฝ์šฐ BRANCHEQ๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์œผ๋ฉด โ€œVMโ€์€ ๋ถ„๊ธฐ๋˜์ง€ ์•Š์•„์•ผํ•ฉ๋‹ˆ๋‹ค.
  • PUSH / POP๋Š” ๋‹น์—ฐํžˆ ์Šคํƒ์—์„œ ์ˆซ์ž๋ฅผ ํ‘ธ์‹œํ•˜๊ฑฐ๋‚˜ ํŒํ•ฉ๋‹ˆ๋‹ค.
  • ์ ํ”„ ๋ฐ ๋ถ„๊ธฐ ์—ฐ์‚ฐ์ž๋Š” ์ด์ง„ ์ฃผ์†Œ๊ฐ€ ์•„๋‹Œ ํŠน์ • ์ž‘์—… (์ฝ”๋“œ ๋ผ์ธ)์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
  • ์ง€์  ์ž‘์—…์€ ๋น„๊ต๋ฅผ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์˜คํžˆ๋ ค ๋งˆ์ง€๋ง‰ ๋น„๊ต์—์„œ ์ถœ๋ ฅ์„ ๊ฐ€์ ธ์™€ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ถ„๊ธฐ ๋ฐ ์ ํ”„ ์—ฐ์‚ฐ์ž๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ๋ผ์ธ ๋ฒˆํ˜ธ ์ธ๋ฑ์‹ฑ ์‹œ์Šคํ…œ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. (์˜ˆ : JMP 0์ด ์ฒซ ๋ฒˆ์งธ ์ค„๋กœ ์ด๋™)
  • ๋ชจ๋“  ์—ฐ์‚ฐ์€ 0์œผ๋กœ ์˜ค๋ฒ„ ํ”Œ๋กœ์šฐ๋˜๊ณ  ์ •์ˆ˜ ์˜ค๋ฒ„ ํ”Œ๋กœ์šฐ์—์„œ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š๋Š” ๋ถ€ํ˜ธ์—†๋Š” ์ˆซ์ž์— ๋Œ€ํ•ด ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค.
  • 0์œผ๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์€ ํ—ˆ์šฉ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ํ”„๋กœ๊ทธ๋žจ์˜ ๋™์ž‘์€ ์ •์˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    • ํ”„๋กœ๊ทธ๋žจ์„ ์ค‘๋‹จํ•˜์‹ญ์‹œ์˜ค.
    • VM์˜ ์‹คํ–‰์„ ์ข…๋ฃŒํ•˜๊ณ  ํ˜„์žฌ ์ƒํƒœ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    • โ€œERR : Division by 0โ€ณ๋ฉ”์‹œ์ง€๋ฅผ ํ‘œ์‹œํ•˜์‹ญ์‹œ์˜ค.
  • ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ๋Š” ๋ช…๋ น ํฌ์ธํ„ฐ๊ฐ€ ํ”„๋กœ๊ทธ๋žจ ๋์— ๋„๋‹ฌ ํ•  ๋•Œ์™€ ๊ฐ™์ด ์ •์˜๋ฉ๋‹ˆ๋‹ค (๋น„ ๋น„์–ด์žˆ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐ€์ • ํ•  ์ˆ˜ ์žˆ์Œ).

์ถœ๋ ฅ
์ถœ๋ ฅ์€ ์ •ํ™•ํžˆ์ด ๊ฐ’์ด์–ด์•ผํ•ฉ๋‹ˆ๋‹ค (์ค„ ๋ฐ”๊พธ๊ธฐ ํฌํ•จ).

R0 3126900366
R1 0
R2 10000

ํฌ์ธํŠธ
ํฌ์ธํŠธ๋Š” ๋‹ค์Œ ๊ณต์‹์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค.Number Of Characters * (Seconds Needed To Run / 2)

๋‹ค๋ฅธ ์‹œ๊ฐ„์„ ์œ ๋ฐœํ•˜๋Š” ํ•˜๋“œ์›จ์–ด ์ฐจ์ด๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ ํ…Œ์ŠคํŠธ๋Š” ์šฐ๋ถ„ํˆฌ ์„œ๋ฒ„ ๋˜๋Š” Windows 8์˜ ๋‚ด ์ปดํ“จํ„ฐ (i5-4210u, 8GB ๋žจ)์—์„œ ์‹คํ–‰๋˜๋ฏ€๋กœ Dual G5์—์„œ๋งŒ ์ปดํŒŒ์ผ๋˜๋Š” ๋ฏธ์นœ ์ด๊ตญ์ ์ธ ๋Ÿฐํƒ€์ž„์„ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์‹ญ์‹œ์˜ค ์ •ํ™•ํžˆ 762.66MB์˜ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ RAM์ด์žˆ๋Š” Mac Pro.

ํŠน์ˆ˜ํ•œ ๋Ÿฐํƒ€์ž„ / ์–ธ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋งํฌ๋ฅผ ๊ฒŒ์‹œํ•˜์‹ญ์‹œ์˜ค.

  • ๊ด€์‹ฌ์žˆ๋Š” ์‚ฌ๋žŒ๋“ค์„ ์œ„ํ•ด ํ…Œ์ŠคํŠธ ์ฝ”๋“œ (C #์œผ๋กœ ์ž‘์„ฑ)๋ฅผ ๊ฒŒ์‹œํ–ˆ์Šต๋‹ˆ๋‹ค : http://pastebin.com/WYCG5Uqu

ํ…Œ์ŠคํŠธ ํ”„๋กœ๊ทธ๋žจ

์•„์ด๋””์–ด๋Š” ์—ฌ๊ธฐ ์—์„œ ์™” ์œผ๋ฏ€๋กœ ์•ฝ๊ฐ„ ์ˆ˜์ • ๋œ ๋ฒ„์ „์˜ ํ”„๋กœ๊ทธ๋žจ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

ํ”„๋กœ๊ทธ๋žจ์˜ ์˜ฌ๋ฐ”๋ฅธ ์ถœ๋ ฅ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 3,126,900,366

C์—์„œ :

int s, i, j;
for (s = 0, i = 0; i < 10000; i++) {
    for (j = 0; j < 10000; j++)
        s += (i * j) / 3;
}

์ฝ”๋“œ์—์„œ : [R0์€ s, j์˜ R1, i์˜ R2๋ฅผ ๋‚˜ํƒ€๋ƒ„]

LOAD R0 0
LOAD R2 0 <--outer loop value
LOAD R1 0 <--inner loop value
     --Begin inner loop--
PUSH R1 <--push inner loop value to the stack
MUL R1 R2 <--(i*j)
PUSH R2
LOAD R2 3
DIV R1 R2 <-- / 3
POP R2
ADD R0 R1 <-- s+=
POP R1
PUSH R2
LOAD R2 1
ADD R1 R2 <--j++
POP R2
PUSH R2
LOAD R2 10000
CMP R1 R2 <-- j < 10000
POP R2
BRANCHLT 3 <--Go back to beginning inner loop
--Drop To outer loop--
LOAD R1 1
ADD R2 R1 <--i++
LOAD R1 10000
CMP R2 R1 <-- i < 10000
LOAD R1 0 <--Reset inner loop
BRANCHLT 2

์ด์ง„ / ์œก๊ฐ์œผ๋กœ :

0x00 0x00 0x00 0x00 0x00 0x00
0x00 0x02 0x00 0x00 0x00 0x00
0x00 0x01 0x00 0x00 0x00 0x00
0x02 0x01
0x06 0x01 0x02
0x02 0x02
0x00 0x02 0x00 0x00 0x00 0x03
0x07 0x01 0x02
0x03 0x02
0x04 0x00 0x01
0x03 0x01
0x02 0x02
0x00 0x02 0x00 0x00 0x00 0x01
0x04 0x01 0x02
0x03 0x02
0x02 0x02
0x00 0x02 0x00 0x00 0x27 0x10
0x09 0x01 0x02
0x03 0x02
0x0a 0x00 0x00 0x00 0x03
0x00 0x01 0x00 0x00 0x00 0x01
0x04 0x02 0x01
0x00 0x01 0x00 0x00 0x27 0x10
0x09 0x02 0x01
0x00 0x01 0x00 0x00 0x00 0x00
0x0a 0x00 0x00 0x00 0x02

๋ณด๋„ˆ์Šค ํฌ์ธํŠธ
(ํšจ๊ณผ๋Š” ๊ณฑ์…ˆ์œผ๋กœ ์ ์šฉ๋จ) ์˜ˆ๋ฅผ ๋“ค์–ด ์„ธ ๊ฐ€์ง€ ๋ชจ๋‘์— ํ•ด๋‹น๋˜๋Š” ๊ฒฝ์šฐ ((๋ฌธ์ž * 0.50) * 0.75) * 0.90

  • ์ธํ„ฐํ”„๋ฆฌํ„ฐ๊ฐ€ ์‹ค์ œ๋กœ JIT ์ปดํŒŒ์ผ๋Ÿฌ ์ธ ๊ฒฝ์šฐ 50 % ๊ฐ์†Œ
  • ๋ฃจํ”„ ์–ธ ๋กค๋ง / ์˜๋ฏธ์žˆ๋Š” ์ตœ์ ํ™”๋ฅผ ์ ์šฉํ•˜๋ฉด 25 % ๊ฐ์†Œํ•ฉ๋‹ˆ๋‹ค.
  • VM์„ ํ™•์žฅํ•˜๋ฉด 10 % ๊ฐ์†Œ
    • BRANCHEQ [์ฝ”๋“œ ๋ผ์ธ, 4 ๋ฐ”์ดํŠธ] (๋™์ผํ•œ ์ง€์ -opcode 0x0b)
    • BRANCHGT [์ฝ”๋“œ ๋ผ์ธ, 4 ๋ฐ”์ดํŠธ] (๋ถ„๊ธฐ๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ-opcode 0x0c)
    • BRANCHNE [์ฝ”๋“œ ๋ผ์ธ, 4 ๋ฐ”์ดํŠธ] (๋™์ผํ•˜์ง€ ์•Š์€ ์ง€์ -opcode 0x0d)
    • RLOAD [๋ ˆ์ง€์Šคํ„ฐ 1] [๋ ˆ์ง€์Šคํ„ฐ 2] (๋ ˆ์ง€์Šคํ„ฐ 2์˜ ๊ฐ’์„ ์ด๋™ํ•˜์—ฌ ๋ ˆ์ง€์Šคํ„ฐ 1-opcode 0x01).

ํ—ˆ์šฉ๋˜์ง€ ์•Š์Œ

  • ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ํ”„๋กœ๊ทธ๋žจ์— ์‚ฌ์ „ ์ปดํŒŒ์ผํ•˜๋Š” ๊ฒƒ์€ ๊ธˆ์ง€๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. STDIN ๋˜๋Š” ํŒŒ์ผ์—์„œ ๋ฐ”์ดํŠธ ์ฝ”๋“œ๋ฅผ ์Šน์ธํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค (์–ด๋–ค ๊ฒƒ์ด ๋“  ์ƒ๊ด€ ์—†์Šต๋‹ˆ๋‹ค).
  • ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜์ง€ ์•Š๊ณ  ์ถœ๋ ฅ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • VM ์š”๊ตฌ ์‚ฌํ•ญ์„ ์†์ด๋ ค๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•.


๋‹ต๋ณ€

C, 752 (ํ”Œ๋ž˜๊ทธ ์ •์˜์˜ ๊ฒฝ์šฐ 589 + 163) * 0.5 (JIT) * 0.9 (ํ™•์žฅ์ž) * (0.75 ์ตœ์ ํ™”) * (0.64 ์ดˆ / 2) = 81.216

C[S],J[S],i,j,k,y,c,X,Y,Z;char*R,a,b[9];main(x){R=mmap(0,S,6,34,-1,0);N=85;while(scanf("%c%s%*[ R]%d%*[ R]%d",&a,b,&x,&y)&&~getchar())a-=65,a-1?a-15?a-9?a?a-2?a-3?a-11?a-12?a-17?(N=41,v):(N=137,v):(N=137,u,N=247,g(H,4),N=139,u):(y?N=189+x,s(y):(N=51,g(G,G))):(N=137,u,N=247,g(H,6),N=139,u):(N=57,v,s(0xFC8A9F),--j):(N=1,v):(N=233,J[k++]=i,s(x)):b[1]-80?N=85+x:(N=93+x):(c=b[5],s(0x0F9EE78A),N=(c-69?c-71?c-76?1:8:11:0)+132,J[k++]=i,s(x)),C[++i]=j;U(E8,X)U(F0,Y)U(F8,Z)s(50013);i=j;while(k--)j=C[J[k]]+1,R[j-1]-233&&(j+=4),s(C[*(int*)(R+j)]-j-4);((int(*)())R)();printf("%u %u %u\n",X,Y,Z);}

์ฝ”๋“œ๋ฅผ ์ทจํ•ฉ๋‹ˆ๋‹ค (LOAD R0 ๋“ฑ), ํ›„ํ–‰ ๋ฌธ์ž, ๋‹จ์ผ ๊ณต๋ฐฑ, ์ค‘๊ฐ„์— ๋นˆ ์ค„, ์ฃผ์„ ์—†์Œ ๋“ฑ์„ ํ•ฉ๋‹ˆ๋‹ค. ํ›„ํ–‰ ์ค„ ๋ฐ”๊ฟˆ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ ๋‹ค์Œ 80386 ๋ฐ”์ดํŠธ ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜๋˜์–ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

๋กœ๋“œ 0๋ ˆ์ง€์Šคํ„ฐ์— ์˜ํ•ด ๊ต์ฒด xor์ž์ฒด ๋ ˆ์ง€์Šคํ„ฐ๋ฅผ ๋ณด๋‚ด๊ณ  ๋Œ€์‹  mov๋ณด๋‚ด๊ณ 0 ์ƒ์„ฑ ๋ฐ”์ดํŠธ์—์„œ 3 ๋ฐ”์ดํŠธ ์งง๋‹ค ๋ ˆ์ง€์Šคํ„ฐ๋กœ, ๋งค์šฐ ๊ทผ์†Œํ•˜๊ฒŒ ๋” ๋น ๋ฅผ ์ˆ˜์žˆ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ปดํŒŒ์ผํ•˜์‹ญ์‹œ์˜ค.

gcc -m32 -D"g(a,b)=(N=192|b<<3|a)"-D"s(b)=(*(int*)(R+j)=b,j+=4)"-DN=R[j++]-D"G=((x+1)|4)"
-D"H=((y+1)|4)"-DS=9999-D"u=g(0,G)"-D"v=g(G,H)"-D"U(b,c)=s(0xA3##b##89),--j,s(&c);"
bytecode.c -o bytecode

POSIX ํ˜ธํ™˜ OS๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

STDIN์—์„œ ์ž…๋ ฅ์„ ์ฝ์Šต๋‹ˆ๋‹ค (์‚ฌ์šฉ ./bytecode < file ํŒŒ์ผ์—์„œ ํŒŒ์ดํ”„๋กœ ).

ํ…Œ์ŠคํŠธ ํ”„๋กœ๊ทธ๋žจ์˜ ๊ฒฐ๊ณผ ๋ฐ”์ดํŠธ ์ฝ”๋“œ :

; start
 0:   55                      push   %ebp
; LOAD R0 0
 1:   33 ed                   xor    %ebp,%ebp
; LOAD R2 0
 3:   33 ff                   xor    %edi,%edi
; LOAD R1 0
 5:   33 f6                   xor    %esi,%esi
; PUSH $1
 7:   56                      push   %esi
; MUL R1 R2
 8:   89 f0                   mov    %esi,%eax
 a:   f7 e7                   mul    %edi
 c:   8b f0                   mov    %eax,%esi
; PUSH R2
 e:   57                      push   %edi
; LOAD R2 3
 f:   bf 03 00 00 00          mov    $0x3,%edi
; DIV R1 R2
14:   89 f0                   mov    %esi,%eax
16:   f7 f7                   div    %edi
18:   8b f0                   mov    %eax,%esi
; POP R2
1a:   5f                      pop    %edi
; ADD R0 R1
1b:   01 f5                   add    %esi,%ebp
; POP R1
1d:   5e                      pop    %esi
; PUSH R2
1e:   57                      push   %edi
; LOAD R2 1
1f:   bf 01 00 00 00          mov    $0x1,%edi
; ADD R1 R2
24:   01 fe                   add    %edi,%esi
; POP R2
26:   5f                      pop    %edi
; PUSH R2
27:   57                      push   %edi
; LOAD R2 10000
28:   bf 10 27 00 00          mov    $0x2710,%ed
; CMP R1 R2
2d:   39 fe                   cmp    %edi,%esi
2f:   9f                      lahf
30:   8a fc                   mov    %ah,%bh
; POP R2
32:   5f                      pop    %edi
; BRANCHLT 3
33:   8a e7                   mov    %bh,%ah
35:   9e                      sahf
36:   0f 8c cb ff ff ff       jl     0x7
; LOAD R1 1
3c:   be 01 00 00 00          mov    $0x1,%esi
; ADD R2 R1
41:   01 f7                   add    %esi,%edi
; LOAD R1 10000
43:   be 10 27 00 00          mov    $0x2710,%es
; CMP R2 R1
48:   39 f7                   cmp    %esi,%edi
4a:   9f                      lahf
4b:   8a fc                   mov    %ah,%bh
; LOAD R1 0
4d:   33 f6                   xor    %esi,%esi
; BRANCHLT 2
4f:   8a e7                   mov    %bh,%ah
51:   9e                      sahf
52:   0f 8c ad ff ff ff       jl     0x5
; copy R0 to X
58:   89 e8                   mov    %ebp,%eax
5a:   a3 28 5b 42 00          mov    %eax,0x425b
; copy R1 to Y
5f:   89 f0                   mov    %esi,%eax
61:   a3 38 55 44 00          mov    %eax,0x4455
; copy R2 to Z
66:   89 f8                   mov    %edi,%eax
68:   a3 40 55 44 00          mov    %eax,0x4455
; exit
6d:   5d                      pop    %ebp
6e:   c3                      ret

์–ธ ๊ณจํ”„ ๋“œ :

C[9999],J[9999],i,j,k,y,c,X,Y,Z;
char *R,a,b[9];
main(x){
    // 6 is PROC_WRITE|PROC_EXEC
    // 34 is MAP_ANON|MAP_PRIVATE
    R=mmap(0,'~~',6,34,-1,0);

    N=0x55;
    while(scanf("%c%s%*[ R]%d%*[ R]%d",&a,b,&x,&y)&&~getchar())
        a-=65,
        a-1? // B[RANCH**]
            a-15? // P[USH/OP]
                a-9? // J[MP]
                    a? // A[DD]
                        a-2? // C[MP]
                            a-3? // D[IV]
                                a-11? // L[OAD]
                                    a-12? // M[UL]
                                        a-17? // R[LOAD]
                                            // SUB
                                            (N=0x29,g(G,H))
                                        :(N=0x89,g(G,H))
                                    :(N=0x89,g(0,G),N=0xF7,g(H,4),N=0x8B,g(0,G))
                                :(y?N=0xBD+x,s(y):(N=0x33,g(G,G)))
                            :(N=0x89,g(0,G),N=0xF7,g(H,6),N=0x8B,g(0,G))
                        :(N=0x39,g(G,H),s(0xfc8a9f),--j)
                    :(N=0x1,g(G,H))
                :(N=0xE9,J[k++]=i,s(x))
            :b[1]-80?
                N=0x55+x // PUSH
            :(N=0x5D+x) // POP
        :(c=b[5],s(0x0f9ee78a),N=(
        c-69? // EQ
            c-71? // GT
                c-76? // LT
                    1 // NE
                :8
            :11
        :0
        )+0x84,J[k++]=i,s(x)),
        C[++i]=j
        ;
    // transfer registers to X,Y,Z
    s(0xA3E889),--j,s(&X);
    s(0xA3F089),--j,s(&Y);
    s(0xA3F889),--j,s(&Z);

    // pop and ret
    s(0xC35D);

    i=j;
    // fix distances for jmp/branch**
    while(k--)
        j=C[J[k]]+1,R[j-1]-0xE9&&(j+=4),
        s(C[*(int*)(R+j)]-j-4);

    // call
    ((int(*)())R)();

    // output
    printf("%u %u %u\n",X,Y,Z);
}

๋‹ต๋ณ€

C, ์ ์ˆ˜ = 854 ๋ฐ”์ดํŠธ ร— (~ 0.8 ์ดˆ / 2) ร— 0.5 [JIT] ร— 0.9 [ํ™•์žฅ] = ~ 154 ๋ฐ”์ดํŠธ ์ดˆ

#define G getchar()
#define L for(i=0;i<3;++i)
#define N*(int*)
#define M(x)"P\x8a\xe7\x9e\xf"#x"    KL"
*T[1<<20],**t=T,*F[1<<20],**f=F,R[3],r[]={1,6,7};char*I[]={"L\xb8    GGJH","I\x8b\xc0HHGH","H\x50GG","H\x58GG","I\3\xc0HHGH","I\53\xc0HHGH","M\x8b\xc0\xf7\xe0\x8b\xc0IHLGJ","O\63\xd2\x8b\xc0\xf7\xf0\x8b\xc0IJNGL","L\xe9    KH","L\73\xc0\x9f\x8a\xfcHHGH",M(\x82),M(\x84),M(\x87),M(\x85)},C[1<<24],*c=C;main(i,o,l,g){N c=0xb7ec8b60;c[4]=70;c+=5;while((o=G)>=0){char*s=I[o];l=*s-'G';memcpy(c,s+1,l);for(s+=l+1;o=*s++;){o-='G';if(o<3){g=r[G];c[*s++-'G']|=g<<3*(o&1);if(o>1)c[*s++-'G']|=g<<3;}else{if(o>3)*f++=c+*s-'G';for(i=4;i;--i)c[*s-'G'+i-1]=G;++s;}}*t++=c;c+=l;}*t=c;while(f>F)--f,**f=(int)T[**f]-(int)*f-4;L N&c[7*i]=0x5893e|r[i]<<19,N&c[3+7*i]=R+i;N&c[21]=0xc361e58b;mprotect((int)C>>12<<12,1<<24,7);((void(*)())C)();L printf("R%d %u\n",i,R[i]);}

gcc vm.c -ovm -m32 -wx86 POSIX ํ˜ธํ™˜ OS์—์„œ ์ปดํŒŒ์ผํ•˜์‹ญ์‹œ์˜ค .
์™€ ์‹คํ–‰ ./vm < program, ์–ด๋”” program๋ฐ”์ด๋„ˆ๋ฆฌ ํ”„๋กœ๊ทธ๋žจ ํŒŒ์ผ์ž…๋‹ˆ๋‹ค.


์†๋„๋ฅผ ๊ฐ„๋‹ค. ์ด ํ”„๋กœ๊ทธ๋žจ์€ ์ž…๋ ฅ ํ”„๋กœ๊ทธ๋žจ์„ x86 ๋จธ์‹  ์ฝ”๋“œ๋กœ ๋งค์šฐ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ณ€ํ™˜ํ•˜์—ฌ CPU๊ฐ€ ๋‚˜๋จธ์ง€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋„๋กํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ์€ ํ…Œ์ŠคํŠธ ํ”„๋กœ๊ทธ๋žจ์˜ ๋ฒˆ์—ญ์ž…๋‹ˆ๋‹ค.
ecx, esi๋ฐ edi์— ํ•ด๋‹นR0 , R1๋ฐ R2๊ฐ๊ฐ์— ; bh์ƒํƒœ ํ”Œ๋ž˜๊ทธ๋ฅผ ๋ณด์œ ํ•ฉ๋‹ˆ๋‹ค. eax๋ฐ edx์Šคํฌ๋ž˜์น˜ ๋ ˆ์ง€์Šคํ„ฐ์ด๊ณ ; ์ฝœ ์Šคํƒ์€ VM์˜ ์Šคํƒ์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค.

# Prologue
     0:   60                      pusha
     1:   8b ec                   mov    ebp,esp
     3:   b7 46                   mov    bh,0x46
# LOAD R0 0
     5:   b9 00 00 00 00          mov    ecx,0x0
# LOAD R2 0 <--outer loop value
     a:   bf 00 00 00 00          mov    edi,0x0
# LOAD R1 0 <--inner loop value
     f:   be 00 00 00 00          mov    esi,0x0
#      --Begin inner loop--
# PUSH R1 <--push inner loop value to the stack
    14:   56                      push   esi
# MUL R1 R2 <--(i*j)
    15:   8b c6                   mov    eax,esi
    15:   f7 e7                   mul    edi
    19:   8b f0                   mov    esi,eax
# PUSH R2
    1b:   57                      push   edi
# LOAD R2 3
    1c:   bf 03 00 00 00          mov    edi,0x3
# DIV R1 R2 <-- / 3
    21:   33 d2                   xor    edx,edx
    23:   8b c6                   mov    eax,esi
    25:   f7 f7                   div    edi
    27:   8b f0                   mov    esi,eax
# POP R2
    29:   5f                      pop    edi
# ADD R0 R1 <-- s+=
    2a:   03 ce                   add    ecx,esi
# POP R1
    2c:   5e                      pop    esi
# PUSH R2
    2d:   57                      push   edi
# LOAD R2 1
    2e:   bf 01 00 00 00          mov    edi,0x1
# ADD R1 R2 <--j++
    33:   03 f7                   add    esi,edi
# POP R2
    35:   5f                      pop    edi
# PUSH R2
    36:   57                      push   edi
# LOAD R2 10000
    37:   bf 10 27 00 00          mov    edi,0x2710
# CMP R1 R2 <-- j < 10000
    3c:   3b f7                   cmp    esi,edi
    3e:   9f                      lahf
    3f:   8a fc                   mov    bh,ah
# POP R2
    41:   5f                      pop    edi
# BRANCHLT 4 <--Go back to beginning inner loop
    42:   8a e7                   mov    ah,bh
    44:   9e                      sahf
    45:   0f 82 c9 ff ff ff       jb     0x14
# --Drop To outer loop--
# LOAD R1 1
    4b:   be 01 00 00 00          mov    esi,0x1
# ADD R2 R1 <--i++
    50:   03 fe                   add    edi,esi
# LOAD R1 10000
    52:   be 10 27 00 00          mov    esi,0x2710
# CMP R2 R1 <-- i < 10000
    57:   3b fe                   cmp    edi,esi
    59:   9f                      lahf
    5a:   8a fc                   mov    bh,ah
# LOAD R1 0 <--Reset inner loop
    5c:   be 00 00 00 00          mov    esi,0x0
# BRANCHLT 3
    61:   8a e7                   mov    ah,bh
    63:   9e                      sahf
    64:   0f 82 a5 ff ff ff       jb     0xf
# Epilogue
    6a:   3e 89 0d 60 ac 04 09    mov    DWORD PTR ds:0x904ac60,ecx
    71:   3e 89 35 64 ac 04 09    mov    DWORD PTR ds:0x904ac64,esi
    78:   3e 89 3d 68 ac 04 09    mov    DWORD PTR ds:0x904ac68,edi
    7f:   8b e5                   mov    esp,ebp
    81:   61                      popa
    82:   c3                      ret

์–ธ ๊ณจํ”„

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <sys/mman.h>

#define MAX_INST_COUNT (1 << 20)
#define MAX_CODE_SIZE (8 * MAX_INST_COUNT)
#define PAGE_SIZE (1 << 12)

uintptr_t *targets[MAX_INST_COUNT], **targets_ptr = targets;
int32_t *fixups[MAX_INST_COUNT], **fixups_ptr = fixups;
char code[MAX_CODE_SIZE], *code_ptr = code;
uint32_t R[3];

const char* inst_defs[] = {
    /* LOAD     */ "\5\xb8    \1\0\4\1",
    /* RLOAD    */ "\2\x8b\xc0\2\1\1\1",
    /* PUSH     */ "\1\x50\1\0",
    /* POP      */ "\1\x58\1\0",
    /* ADD      */ "\2\x03\xc0\2\1\1\1",
    /* SUB      */ "\2\x2b\xc0\2\1\1\1",
    /* MUL      */ "\6\x8b\xc0\xf7\xe0\x8b\xc0\3\1\5\1\3",
    /* DIV      */ "\x8\x33\xd2\x8b\xc0\xf7\xf0\x8b\xc0\3\3\7\1\5",
    /* JMP      */ "\5\xe9    \5\1",
    /* CMP      */ "\5\x3b\xc0\x9f\x8a\xfc\2\1\1\1",
    /* BRANCHLT */ "\x9\x8a\xe7\x9e\x0f\x82    \5\5",
    /* BRANCHEQ */ "\x9\x8a\xe7\x9e\x0f\x84    \5\5",
    /* BRANCHGT */ "\x9\x8a\xe7\x9e\x0f\x87    \5\5",
    /* BRANCHNE */ "\x9\x8a\xe7\x9e\x0f\x85    \5\5"
};

int main() {
    int i;

    {
        const char prologue[] =
            /* PUSHAD       */ "\x60"
            /* MOV EBP, ESP */ "\x8b\xec"
            /* MOV BH, 46h  */ "\xb7\x46"
        ;
        memcpy(code_ptr, prologue, sizeof(prologue) - 1);
        code_ptr += sizeof(prologue) - 1;
    }

    {
        const char reg[] = {1, 6, 7};
        char r;
        int op;
        while ((op = getchar()) != EOF) {
            const char* def = inst_defs[op];
            int len = def[0];
            memcpy(code_ptr, def + 1, len);
            for (def += len + 1; *def; ) {
                switch (*def++) {
                    case 1: code_ptr[*def++] |= reg[getchar()]; break;
                    case 2: code_ptr[*def++] |= reg[getchar()] << 3; break;
                    case 3:
                        r = reg[getchar()];
                        code_ptr[*def++] |= r; code_ptr[*def++] |= r << 3;
                        break;
                    case 5: *fixups_ptr = code_ptr + *def; ++fixups_ptr;
                    case 4:
                        for (i = 4; i; --i) code_ptr[*def + i - 1] = getchar();
                        ++def;
                        break;
                }
            }
            *targets_ptr = code_ptr; ++targets_ptr;
            code_ptr += len;
        }
        *targets_ptr = code_ptr; ++targets_ptr;

        while (fixups_ptr != fixups) {
            --fixups_ptr;
            **fixups_ptr = (char*)targets[**fixups_ptr] - (char*)*fixups_ptr - 4;
        }
    }

    {
        const char epilogue[] =
            /* MOV R[0], ECX */ "\x3e\x89\x0d    "
            /* MOV R[1], ESI */ "\x3e\x89\x35    "
            /* MOV R[2], EDI */ "\x3e\x89\x3d    "
            /* MOV ESP, EBP  */ "\x8b\xe5"
            /* POPAD         */ "\x61"
            /* RET           */ "\xc3"
        ;
        memcpy(code_ptr, epilogue, sizeof(epilogue) - 1);
        for (i = 0; i < 3; ++i) *(uintptr_t*)&code_ptr[3 + 7 * i] = &R[i];
        code_ptr += sizeof(epilogue) - 1;
    }

    mprotect(
        (uintptr_t)code / PAGE_SIZE * PAGE_SIZE, MAX_CODE_SIZE,
        PROT_READ | PROT_WRITE | PROT_EXEC
    );
    { void (*program)(void) = code; program(); }
    for (i = 0; i < 3; ++i) printf("R%d %u\n", i, R[i]);

    return 0;
}

๋‹ต๋ณ€

CJam, 222 (187) 185 ๋ฐ”์ดํŠธ (* ๋„ˆ๋ฌด ๋А๋ ค / 2)

CJam์—์„œ ๋ฐ”์ดํŠธ ์ฝ”๋“œ VM์„ ์ž‘์„ฑํ•˜์—ฌ ์–ผ๋งˆ๋‚˜ ์งง์€ ์ง€ ์•Œ๊ณ  ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค. 200 ๋ฐ”์ดํŠธ ๋ฏธ๋งŒ์€ ๊ฝค ๊ดœ์ฐฎ์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. CJam ์ž์ฒด๊ฐ€ ํ•ด์„๋˜๊ธฐ ๋•Œ๋ฌธ์— ์†๋„๊ฐ€ ๋А๋ฆฝ๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐ ์˜ค๋žœ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค.

304402480 6b:P;q:iD-);{(_P=@/(\L*@@+\}h;]:P;TTT]:R;{_Rf=~}:Q;{4G#%R@0=@t:R;}:O;{TP=("R\(\GG*bt:R;  ~R= R\~@t:R; Q+O Q4G#+-O Q*O Q/O ~(:T; Rf=~-:U; GG*bU0<{(:T}*;"S/=~T):TP,<}g3,{'R\_S\R=N}/

๊ทธ๊ฒƒ์„ ์‹คํ–‰ํ•˜๋ ค๋ฉด ์ด sourceforge ๋งํฌ ์—์„œ Java ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œ ํ•˜๊ณ  ์ฝ”๋“œ๋ฅผ ์ €์žฅํ•˜๊ณ  ๋‹ค์Œ์œผ๋กœ vm.cjam์‹คํ–‰ํ•˜์‹ญ์‹œ์˜ค.

java -jar cjam-0.6.2.jar vm.cjam

ํ”„๋กœ๊ทธ๋žจ์€ STDIN์˜ ๋ฐ”์ดํŠธ ์ฝ”๋“œ๋ฅผ ์˜ˆ์ƒํ•ฉ๋‹ˆ๋‹ค. PowerShell์—์„œ ํ›„ํ–‰ ์ค„ ๋ฐ”๊ฟˆ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๋ณ€ํ™˜ํ•˜์ง€ ์•Š๊ณ  ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ํ”„๋กœ๊ทธ๋žจ์— ํŒŒ์ดํ”„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•„์ง ์ฐพ์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.0x0a ์— 0x0d 0x0a์ •๋ง ์งœ์ฆ๋‚˜์ด๋‹ค. ์ฝ”๋“œ์—๋Š” 4 ๋ฐ”์ดํŠธ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.D-); ํฌํ•จ๋˜์ง€ ์•Š์€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. . ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ๊ทธ์— ๋Œ€ํ•œ ํ•ด๊ฒฐ์ฑ…์„ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด ์•Œ๋ ค์ฃผ์‹ญ์‹œ์˜ค.

์•ฝ๊ฐ„ ๊ณจํผ๋˜์ง€ ์•Š์Œ :

304402480 6b:P; "Create lookup table for instruction sizes. Store in P.";
q:i             "Read program and convert bytes to integers.";
D-);            "Remove spurious carriage returns. This shouldn't be necessary.";
{(_P=@/(\L*@@+\}h;]:P; "Split into instructions. Store in P.";
"We'll use T for the instruction pointer as it's initialised to 0.";
"Likewise, we'll use U for the CMP flag.";
TTT]:R; "Store [0 0 0] in R for the registers.";
{_Rf=~}:Q; "Register lookup block.";
{4G#%R@0=@t:R;}:O; "Save in register block.";
{TP=("R\(\GG*bt:R;

~R=
R\~@t:R;
Q+O
Q4G#+-O
Q*O
Q/O
~(:T;
Rf=~-:U;
GG*bU0<{(:T}*;"N/=~T):TP,<}g "Run program.";
3,{'R\_S\R=N}/

๋‚ด์ผ ์ ์ ˆํ•œ ์„ค๋ช…์„ ์ถ”๊ฐ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์ฆ‰, ๋ชจ๋“  ๋ ˆ์ง€์Šคํ„ฐ, ๋ช…๋ น์–ด ํฌ์ธํ„ฐ ๋ฐ ๋น„๊ต ํ”Œ๋ž˜๊ทธ๋ฅผ ๋ณ€์ˆ˜์— ์ €์žฅํ•˜์—ฌ CJam์˜ ์Šคํƒ์„ VM์˜ ์Šคํƒ์œผ๋กœ ์ž์œ ๋กญ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


๋‹ต๋ณ€

ํŒŒ์ด์ฌ / C ++, ์ ์ˆ˜ = 56.66

1435 ์ž * .234 / 2 ์ดˆ * .5 [JIT] * .75 [์ตœ์ ํ™”] * .90 [์ถ”๊ฐ€ ์ง€์นจ]

์ž…๋ ฅ ํ”„๋กœ๊ทธ๋žจ์„ c ++๋กœ ์ปดํŒŒ์ผํ•˜๊ณ  gcc๋ฅผ ์‹คํ–‰ ํ•œ ๋‹ค์Œ ๊ฒฐ๊ณผ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ์‹œ๊ฐ„์€ gcc ๋‚ด๋ถ€์—์„œ ์†Œ๋น„๋ฉ๋‹ˆ๋‹ค.

๋‚ด๊ฐ€ํ•˜๋Š” ํ•œ ๊ฐ€์ง€ ์ตœ์ ํ™”๋Š” ์Šคํƒ ์—ฐ์‚ฐ์ด ์˜๋ฏธ ์ ์œผ๋กœ ํ—ˆ์šฉ๋˜๋Š” ๊ฒฝ์šฐ ๋ช…์‹œ ์  ๋ณ€์ˆ˜๋กœ ์ค„์ด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ปดํŒŒ์ผ ๋œ ์ฝ”๋“œ์˜ ๋Ÿฐํƒ€์ž„๋ณด๋‹ค ์•ฝ 10 ๋ฐฐ ๋” ๋‚˜์€ ๋Ÿฐํƒ€์ž„์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค (์‹ค์ œ๋กœ ๊ฒฐ๊ณผ ๋ฐ”์ด๋„ˆ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฐ ์•ฝ .056 ์ดˆ). ๋‚˜๋Š” gcc๊ฐ€ ๋ฌด์—‡์„ํ•˜๊ณ  ์žˆ๋Š”์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ ๊ทธ ๊ฐœ์„ ์„ ์–ป๋Š”๋‹ค.

import sys,os
x=map(ord,sys.stdin.read())
w=lambda x:(x[0]<<24)+(x[1]<<16)+(x[2]<<8)+x[3]
I=[]
while x:
 if x[0]==0:f='r%d=%d'%(x[1],w(x[2:]));n=6
 if x[0]==1:f='r%d=r%d'%(x[1],x[2]);n=3
 if x[0]==2:f='P%d'%x[1];n=2
 if x[0]==3:f='O%d'%x[1];n=2
 if x[0]==4:f='r%d=r%d+r%d'%(x[1],x[1],x[2]);n=3
 if x[0]==5:f='r%d=r%d-r%d'%(x[1],x[1],x[2]);n=3
 if x[0]==6:f='r%d=r%d*r%d'%(x[1],x[1],x[2]);n=3
 if x[0]==7:f='r%d=r%d/r%d'%(x[1],x[1],x[2]);n=3
 if x[0]==8:f='goto L%d'%w(x[1:]);n=5
 if x[0]==9:f='a=r%d;b=r%d'%(x[1],x[2]);n=3
 if x[0]==10:f='if(a<b)goto L%d'%w(x[1:]);n=5
 if x[0]==11:f='if(a==b)goto L%d'%w(x[1:]);n=5
 if x[0]==12:f='if(a>b)goto L%d'%w(x[1:]);n=5
 if x[0]==13:f='if(a!=b)goto L%d'%w(x[1:]);n=5
 I+=[f];x=x[n:]
D=[]
d=0
for f in I:D+=[d];d+='P'==f[0];d-='O'==f[0]
J=[]
if all(d==D[int(f[f.find('L')+1:])]for f,d in zip(I,D)if f[0]in'gi'):
 H='uint32_t '+','.join('s%d'%i for i in range(max(D)))+';'
 for f,d in zip(I,D):
  if f[0]=='P':f='s%d=r'%d+f[1:]
  if f[0]=='O':f='r'+f[1:]+'=s%d'%(d-1)
  J+=[f]
else:
 H='std::vector<uint32_t>s;'
 for f,d in zip(I,D):
  if f[0]=='P':f='s.push_back(r'+f[1:]+')'
  if f[0]=='O':f='r'+f[1:]+'=s.back();s.pop_back()'
  J+=[f]
P='#include<vector>\n#include<cstdint>\nuint32_t r0,r1,r2,a,b;'+H+'int main(){'
for i,f in enumerate(J):P+='L%d:'%i+f+';'
P+=r'printf("R0 %u\nR1 %u\nR2 %u\n",r0,r1,r2);}'
c=open("t.cc", "w")
c.write(P)
c.close()
os.system("g++ -O1 t.cc")
os.system("./a.out")

ํ™•์‹คํžˆ ๋” ๋งŽ์€ ๊ณจํ”„๋ฅผ ์ณค๋‹ค.


๋‹ต๋ณ€

Lua 5.2 (๋˜๋Š” LuaJIT), 740 ๋ฐ”์ดํŠธ

๋จผ์ € ์ตœ์†Œํ•œ์˜ ๊ณจํ”„ ๋งŒ ์‹œ๋„ํ•˜์‹ญ์‹œ์˜ค. ์ด ๋ฒ„์ „์€ (์ ์–ด๋„ ํ…Œ์ŠคํŠธ ํ”„๋กœ๊ทธ๋žจ์—์„œ) ์ž‘๋™ํ•˜๋ฉฐ ์ถ”๊ฐ€ opcode๋ฅผ ๊ตฌํ˜„ํ•˜์ง€๋งŒ ์„œ๋ช…๋˜์ง€ ์•Š์€ ์ˆ˜ํ•™ ์š”๊ตฌ ์‚ฌํ•ญ์„์ง€์ง€ํ•˜์ง€ ์•Š์œผ๋ฉฐ ํŠนํžˆ ๋น ๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ณด๋„ˆ์Šค๋กœ VM์—์„œ VM์ด ์‹คํ–‰ ์ค‘์ด๋ฉฐ ํ•ด์„ (PUC-Lua๋กœ ์‹คํ–‰) ๋˜๋Š” JIT ์ •๋ ฌ (LuaJIT์œผ๋กœ ์‹คํ–‰; ์—ฌ์ „ํžˆ ํ•ด์„๋˜์ง€๋งŒ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋Š” ํ˜„์žฌ ํ•ด์„ ๊ฐ€๋Šฅํ•˜๋„๋ก ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค) ์ ํ•ฉ).

ํŽธ์ง‘ : ๋” ํฐ ๊ณจํ”„ ์—ฌ์ „ํžˆ ๊ณจํ”„.

ํŽธ์ง‘ : ์ฃผ์š” ์˜ค๋ฅ˜๊ฐ€ ์ˆ˜์ •๋˜์—ˆ์œผ๋ฉฐ ์ด์ œ ์‚ฐ์ˆ ์„ unsigned long๋ฒ”์œ„๋กœ ์ œํ•œ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์–ด๋–ป๊ฒŒ ๋“  ํฌ๊ธฐ๋ฅผ ๋ฒ—์–ด๋‚˜์ง€ ๋ชปํ–ˆ์ง€๋งŒ ์—ฌ์ „ํžˆ ์ž˜๋ชป๋œ ๋Œ€๋‹ต์„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

ํŽธ์ง‘ : ๊ฒฐ๊ณผ๋Š” ์ •ํ™•ํ•˜์ง€๋งŒ ์ถœ๋ ฅ์€ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. %u๋Œ€์‹  ์ธ์‡„๋กœ ์ „ํ™˜ํ•˜๋ฉด %d๋ชจ๋‘ ์ •์ƒ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ํฌ๊ธฐ ์™€ ์†๋„๋ฅผ ๋‹ค์†Œ ํ–ฅ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ํ…Œ์ด๋ธ” ๊ธฐ๋ฐ˜ ๋ ˆ์ง€์Šคํ„ฐ๋ฅผ ์ „ํ™˜ํ–ˆ์Šต๋‹ˆ๋‹ค .

ํŽธ์ง‘ : Lua 5.2์˜ goto๋ฌธ (LuaJIT์—์„œ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ)์„ ์‚ฌ์šฉํ•˜์—ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ โ€œJIT-to-Luaโ€๋กœ ๊ต์ฒดํ•˜์—ฌ Lua VM ์ž์ฒด์—์„œ ์ง์ ‘ ์‹คํ–‰ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์‹ค์ œ๋กœ JIT๋กœ ๊ณ„์‚ฐ๋˜๋Š”์ง€ ํ™•์‹คํ•˜์ง€ ์•Š์ง€๋งŒ ์†๋„๊ฐ€ ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค.

U,S,P,F=table.unpack,table.insert,table.remove,math.floor X,r0,r1,r2,p,m,s=2^32,0,0,0,1,0,{}C={{'r%u=%u',1,4},{'r%u=r%u',1,1},{'S(s,r%u)',1},{'r%u=P(s)',1},{'r%u=(r%u+r%u)%%X',1,0,1},{'r%u=(r%u-r%u)%%X',1,0,1},{'r%u=(r%u*r%u)%%X',1,0,1},{'r%u=F(r%u/r%u)%%X',1,0,1},{'goto L%u',4},{'m=r%u-r%u',1,1},{'if m<0 then goto L%u end',4},{'if m==0 then goto L%u end',4},{'if m>0 then goto L%u end',4},{'if m~=0 then goto L%u end',4}}t={io.open(arg[1],'rb'):read('*a'):byte(1,-1)}i,n,r=1,0,{}while i<=#t do c,i,x,a=C[t[i]+1],i+1,0,{}for j=2,#c do y=c[j]if y>0 then x=0 for k=1,y do i,x=i+1,x*256+t[i]end end S(a,x)end S(r,('::L%d::'):format(n))n=n+1 S(r,c[1]:format(U(a)))end load(table.concat(r,' '))()print(('R0 %u\nR1 %u\nR2 %u'):format(r0,r1,r2))

์›๋ณธ์„ ์ฝ์„ ์ˆ˜์žˆ๋Š” ๋ฒ„์ „์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

U,S,P,F=table.unpack,table.insert,table.remove,math.floor

X,r0,r1,r2,p,m,s=2^32,0,0,0,1,0,{}

C={
    {'r%u=%u',1,4},
    {'r%u=r%u',1,1},
    {'S(s,r%u)',1},
    {'r%u=P(s)',1},
    {'r%u=(r%u+r%u)%%X',1,0,1},
    {'r%u=(r%u-r%u)%%X',1,0,1},
    {'r%u=(r%u*r%u)%%X',1,0,1},
    {'r%u=F(r%u/r%u)%%X',1,0,1},
    {'goto L%u',4},
    {'m=r%u-r%u',1,1},
    {'if m<0 then goto L%u end',4},
    {'if m==0 then goto L%u end',4},
    {'if m>0 then goto L%u end',4},
    {'if m~=0 then goto L%u end',4},
}

t={io.open(arg[1],'rb'):read('*a'):byte(1,-1)}
i,n,r=1,0,{}
while i<=#t do
    c,i,x,a=C[t[i]+1],i+1,0,{}
    for j=2,#c do
        y=c[j]
        if y>0 then
            x=0
            for k=1,y do
                i,x=i+1,x*256+t[i]
            end
        end
        S(a,x)
    end
    S(r,('::L%d::'):format(n))
    n=n+1
    S(r,c[1]:format(U(a)))
end
load(table.concat(r,' '))()
print(('R0 %u\nR1 %u\nR2 %u'):format(r0,r1,r2))

๋‹ต๋ณ€

์”จ#

1505 1475 ๋ฐ”์ดํŠธ

์ด๊ฒƒ์€ C #์œผ๋กœ ์ž‘์„ฑ๋œ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์˜ ๋‚ด ๋ฒ„์ „์œผ๋กœ, ๋” ๋งŽ์€ ๊ฒƒ์„ ์ƒ๊ฐํ•˜๊ณ  ์ตœ์ ํ™” ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์‹ค์ œ๋กœ๋Š” ์–ด๋””์— ์žˆ๋Š”์ง€ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค.)

๊ณจํ”„ ๋ฒ„์ „ :

using System;using System.Collections.Generic;using System.IO;using System.Linq;class M{static void Main(string[]a){if(a.Length==1&&File.Exists(a[0])){B.E(B.P(File.ReadAllLines(a[0])));Console.WriteLine(B.O);}}}class B{public enum I{L=0x00,P=0x02,Q=0x03,A=0x04,S=0x05,M=0x06,D=0x07,J=0x08,C=0x09,BL=0x0a,BE=0x0b,BG=0x0c,BN=0x0d}public enum R{A,B,C}enum C{N,L,E,G}public static Dictionary<R,uint>r=new Dictionary<R,uint>{{R.A,0},{R.B,0},{R.C,0}};public static Stack<uint>s=new Stack<uint>();static C c=C.N;public static string O{get{return string.Format("R0 {0}\nR1 {1}\nR2 {2}",r[R.A],r[R.B],r[R.C]);}}public static void E(byte[][]l){for(uint i=0;i<l.Length;i++){var q=l[i];switch((I)q[0]){case I.L:r[(R)q[1]]=U(q,2);break;case I.P:r[(R)q[1]]=s.Pop();break;case I.Q:s.Push(r[(R)q[1]]);r[(R)q[1]]=0;break;case I.A:s.Push(r[(R)q[1]]+r[(R)q[2]]);break;case I.S:s.Push(r[(R)q[1]]-r[(R)q[2]]);break;case I.M:s.Push(r[(R)q[1]]*r[(R)q[2]]);break;case I.D:s.Push(r[(R)q[1]]/r[(R)q[2]]);break;case I.J:i=U(q,1)-1;break;case I.C:{uint x=r[(R)q[1]],y=r[(R)q[2]];c=x<y?C.L:x>y?C.G:C.E;}break;case I.BL:if(c==C.L)i=U(q,1)-1;break;case I.BG:if(c==C.G)i=U(q,1)-1;break;case I.BE:if(c==C.E)i=U(q,1)-1;break;case I.BN:if(c!=C.E)i=U(q,1)-1;break;}}}public static byte[][]P(string[]c){return c.Where(l=>!l.StartsWith("#")).Select(r=>r.Split(' ').Where(b=>b.Length>0).Select(b=>Convert.ToByte(b,16)).ToArray()).Where(l=>l.Length>0).ToArray();}static uint U(byte[]b,int i){return(uint)(b[i]<<24|b[i+1]<<16|b[i+2]<<8|b[i+3]);}}

ํŽธ์ง‘ํ•˜๋‹ค

๋ถˆํ•„์š” publicํ•˜๊ณ  private์ˆ˜์ •์ž๋ฅผ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค .

using System;using System.Collections.Generic;using System.IO;using System.Linq;class M{static void Main(string[]a){if(a.Length==1&&File.Exists(a[0])){B.E(B.P(File.ReadAllLines(a[0])));Console.Write(B.O);}}}class B{enum I{L=0x00,P=0x02,Q=0x03,A=0x04,S=0x05,M=0x06,D=0x07,J=0x08,C=0x09,BL=0x0a,BE=0x0b,BG=0x0c,BN=0x0d}enum R{A,B,C}enum C{N,L,E,G}static Dictionary<R,uint>r=new Dictionary<R,uint>{{R.A,0},{R.B,0},{R.C,0}};static Stack<uint>s=new Stack<uint>();static C c=C.N;public static string O{get{return string.Format("R0 {0}\nR1 {1}\nR2 {2}\n",r[R.A],r[R.B],r[R.C]);}}public static void E(byte[][]l){for(uint i=0;i<l.Length;i++){var q=l[i];switch((I)q[0]){case I.L:r[(R)q[1]]=U(q,2);break;case I.P:r[(R)q[1]]=s.Pop();break;case I.Q:s.Push(r[(R)q[1]]);r[(R)q[1]]=0;break;case I.A:s.Push(r[(R)q[1]]+r[(R)q[2]]);break;case I.S:s.Push(r[(R)q[1]]-r[(R)q[2]]);break;case I.M:s.Push(r[(R)q[1]]*r[(R)q[2]]);break;case I.D:s.Push(r[(R)q[1]]/r[(R)q[2]]);break;case I.J:i=U(q,1)-1;break;case I.C:{uint x=r[(R)q[1]],y=r[(R)q[2]];c=x<y?C.L:x>y?C.G:C.E;}break;case I.BL:if(c==C.L)i=U(q,1)-1;break;case I.BG:if(c==C.G)i=U(q,1)-1;break;case I.BE:if(c==C.E)i=U(q,1)-1;break;case I.BN:if(c!=C.E)i=U(q,1)-1;break;}}}public static byte[][]P(string[]c){return c.Where(l=>!l.StartsWith("#")).Select(r=>r.Split(' ').Where(b=>b.Length>0).Select(b=>Convert.ToByte(b,16)).ToArray()).Where(l=>l.Length>0).ToArray();}static uint U(byte[]b,int i){return(uint)(b[i]<<24|b[i+1]<<16|b[i+2]<<8|b[i+3]);}}

executable.exe filename์–ด๋””์„œ๋‚˜ ๋ถˆ๋Ÿฌfilenameํ•ด์„ ํ•  ์ฝ”๋“œ๊ฐ€ ๋“ค์–ด์žˆ๋Š” ํŒŒ์ผ์ด์žˆ๋Š”

๋‚ด โ€œํ…Œ์ŠคํŠธ ํ”„๋กœ๊ทธ๋žจโ€:

# LOAD R0 5
# CMP R0 R1
# BRANCHEQ 13
# LOAD R1 1
# LOAD R2 1
# CMP R0 R2
# MUL R1 R2
# LOAD R1 1
# ADD R2 R1
# PUSH R2
# PUSH R1
# BRANCHEQ 13
# JMP 5
# POP R2
# POP R0
# POP R1
# PUSH R0

0x0 0x0 0x0 0x0 0x0 0x5
0x9 0x0 0x1
0xb 0x0 0x0 0x0 0xd
0x0 0x1 0x0 0x0 0x0 0x1
0x0 0x2 0x0 0x0 0x0 0x1
0x9 0x0 0x2
0x6 0x1 0x2
0x0 0x1 0x0 0x0 0x0 0x1
0x4 0x2 0x1
0x2 0x2
0x2 0x1
0xb 0x0 0x0 0x0 0xd
0x8 0x0 0x0 0x0 0x5
0x3 0x2
0x3 0x0
0x3 0x1
0x2 0x0

๋” ๊ธด ์ด๋ฆ„์˜ ๋ณ€์ˆ˜, ํด๋ž˜์Šค๋กœ ํ•ด์„๋˜์ง€ ์•Š์€ ํ•ด์„๊ธฐ โ€ฆ

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        if (args.Length == 1 && File.Exists(args[0]))
        {
            var code = ByteCodeInterpreter.ParseCode(File.ReadAllLines(args[0]));
            ByteCodeInterpreter.Execute(code);
            Console.WriteLine(ByteCodeInterpreter.Output);
        }
    }
}

public static class ByteCodeInterpreter
{
    public enum Instruction : byte
    {
        LOAD = 0x00,
        PUSH = 0x02,
        POP = 0x03,
        ADD = 0x04,
        SUB = 0x05,
        MUL = 0x06,
        DIV = 0x07,
        JMP = 0x08,
        CMP = 0x09,
        BRANCHLT = 0x0a,
        BRANCHEQ = 0x0b,
        BRANCHGT = 0x0c,
        BRANCHNE = 0x0d
    }

    public enum Register : byte
    {
        R0 = 0x00,
        R1 = 0x01,
        R2 = 0x02
    }

    private enum CompareFlag : byte
    {
        NONE = 0x00,
        LT = 0x01,
        EQ = 0x02,
        GT = 0x03,
    }

    public static readonly Dictionary<Register, uint> register = new Dictionary<Register, uint>
    {
        {Register.R0, 0},
        {Register.R1, 0},
        {Register.R2, 0}
    };

    public static readonly Stack<uint> stack = new Stack<uint>();
    private static CompareFlag compareFlag = CompareFlag.NONE;

    public static string Output
    {
        get
        {
            return string.Format("R0 {0}\nR1 {1}\nR2 {2}", register[Register.R0], register[Register.R1],
                register[Register.R2]);
        }
    }

    public static void Execute(byte[][] lines)
    {
        for (uint i = 0; i < lines.Length; i++)
        {
            var line = lines[i];
            switch ((Instruction)line[0])
            {
                case Instruction.LOAD:
                    register[(Register)line[1]] = GetUint(line, 2);
                    break;
                case Instruction.PUSH:
                    register[(Register)line[1]] = stack.Pop();
                    break;
                case Instruction.POP:
                    stack.Push(register[(Register)line[1]]);
                    register[(Register)line[1]] = 0;
                    break;
                case Instruction.ADD:
                    stack.Push(register[(Register)line[1]] + register[(Register)line[2]]);
                    break;
                case Instruction.SUB:
                    stack.Push(register[(Register)line[1]] - register[(Register)line[2]]);
                    break;
                case Instruction.MUL:
                    stack.Push(register[(Register)line[1]] * register[(Register)line[2]]);
                    break;
                case Instruction.DIV:
                    stack.Push(register[(Register)line[1]] / register[(Register)line[2]]);
                    break;
                case Instruction.JMP:
                    i = GetUint(line, 1) - 1;
                    break;
                case Instruction.CMP:
                    {
                        uint v0 = register[(Register)line[1]], v1 = register[(Register)line[2]];
                        if (v0 < v1)
                            compareFlag = CompareFlag.LT;
                        else if (v0 > v1)
                            compareFlag = CompareFlag.GT;
                        else
                            compareFlag = CompareFlag.EQ;
                    }
                    break;
                case Instruction.BRANCHLT:
                    if (compareFlag == CompareFlag.LT)
                        i = GetUint(line, 1) - 1;
                    break;
                case Instruction.BRANCHGT:
                    if (compareFlag == CompareFlag.GT)
                        i = GetUint(line, 1) - 1;
                    break;
                case Instruction.BRANCHEQ:
                    if (compareFlag == CompareFlag.EQ)
                        i = GetUint(line, 1) - 1;
                    break;
                case Instruction.BRANCHNE:
                    if (compareFlag != CompareFlag.EQ)
                        i = GetUint(line, 1) - 1;
                    break;
            }
        }
    }

    public static byte[][] ParseCode(string[] code)
    {
        return
            code.Where(line => !line.StartsWith("#"))
                .Select(line => line.Split(' ').Where(b => b.Length > 0).Select(b => Convert.ToByte(b, 16)).ToArray())
                .Where(line => line.Length > 0)
                .ToArray();
    }

    private static uint GetUint(byte[] bytes, int index)
    {
        return (uint)(bytes[index] << 24 | bytes[index + 1] << 16 | bytes[index + 2] << 8 | bytes[index + 3]);
    }
}

๋‹ต๋ณ€