【程序员升职记系列】质数工厂

第 40 关,质数工厂。指令集介绍请看:https://www.annhe.net/article-3828.html

优化目标

28行399步

代码

28行247步

行数速度都满足,见 Github

-- HUMAN RESOURCE MACHINE PROGRAM --

a:
b:
    INBOX   
    COPYTO   10
    COPYFROM 24
    COPYTO   22
    BUMPUP   22
    COPYTO   1
c:
    BUMPUP   1
d:
    COPYFROM 24
    COPYTO   12
    SUB      10
e:
    COPYTO   11
    BUMPUP   12
    COPYFROM 11
    ADD      1
    JUMPN    e
    JUMPZ    f
    BUMPDN   22
    JUMPZ    c
    COPYFROM 10
    OUTBOX  
    JUMP     b
f:
    COPYFROM 1
    OUTBOX  
    BUMPDN   12
    JUMPZ    a
    BUMPUP   12
    COPYTO   10
    JUMP     d

69行267步

提前准备前几个质数

COPYFROM 24
COPYTO   11
BUMPUP   11
BUMPUP   11
COPYTO   12
BUMPUP   12
ADD      11
COPYTO   13
ADD      11
COPYTO   14
ADD      12
COPYTO   15
BUMPUP   15
ADD      11
COPYTO   16
ADD      12
COPYTO   17
BUMPUP   17
ADD      11
COPYTO   18

JUMP     start

out:
COPYFROM 23
OUTBOX

start:
COPYFROM 24
COPYTO   10
BUMPUP   10

INBOX
COPYTO   23
SUB      11
JUMPZ    out
ADD      11
SUB      12
JUMPZ    out
ADD      12
SUB      13
JUMPZ    out
ADD      13
SUB      14
JUMPZ    out
ADD      14
SUB      15
JUMPZ    out
ADD      15
SUB      16
JUMPZ    out
ADD      16
SUB      17
JUMPZ    out
ADD      17
SUB      18
JUMPZ    out

continue:
BUMPUP   10

prepare:
COPYFROM 24
COPYTO   0
COPYFROM 23

test:
SUB      10
JUMPN    continue
JUMPZ    out2
COPYTO   22
BUMPUP   0
COPYFROM 22
JUMP     test

out2:
COPYFROM 10
OUTBOX
COPYFROM 0
JUMPZ    start
COPYTO   23
BUMPUP   23
JUMP     prepare

23行414步

参考 Github。相对于 439 步,将 test 中 COPYTO 22 移到了 JUMPZ,JUMPN 指令之后,并交换了 JUMPZ , JUMPN顺序

start:
COPYFROM 24
COPYTO   10
BUMPUP   10

INBOX
COPYTO   23

continue:
BUMPUP   10

prepare:
COPYFROM 24
COPYTO   0
COPYFROM 23

test:
SUB      10
JUMPN    continue
JUMPZ    out2
COPYTO   22
BUMPUP   0
COPYFROM 22
JUMP     test

out2:
COPYFROM 10
OUTBOX
COPYFROM 0
JUMPZ    start
COPYTO   23
BUMPUP   23
JUMP     prepare

23行439步

start:
COPYFROM 24
COPYTO   10
BUMPUP   10

INBOX
COPYTO   23

continue:
BUMPUP   10

prepare:
COPYFROM 24
COPYTO   0
COPYFROM 23

test:
SUB      10
COPYTO   22
JUMPZ    out2
JUMPN    continue
BUMPUP   0
COPYFROM 22
JUMP     test

out2:
COPYFROM 10
OUTBOX
COPYFROM 0
JUMPZ    start
COPYTO   23
BUMPUP   23
JUMP     prepare

27行500步

start:
COPYFROM 24
COPYTO   0
COPYTO   10
BUMPUP   10

INBOX
COPYTO   23

continue:
BUMPDN   23
JUMPZ    start
BUMPUP   10
COPYFROM 24
COPYTO   0
BUMPUP   23

test:
SUB      10
COPYTO   22
JUMPZ    out2
JUMPN    continue
BUMPUP   0
COPYFROM 22
JUMP     test

out2:
COPYFROM 10
OUTBOX
BUMPUP   0
COPYTO   23
COPYFROM 24
COPYTO   0
COPYFROM 23
JUMP     test

第一次pass 29行508步

从2开始循环找出因子

start:
COPYFROM 24
COPYTO   0
COPYTO   10
BUMPUP   10
BUMPUP   10

INBOX
COPYTO   23

test:
SUB      10
COPYTO   22
JUMPZ    out2
JUMPN    continue
BUMPUP   0
COPYFROM 22
JUMP     test

out2:
COPYFROM 10
OUTBOX
BUMPUP   0
COPYTO   23
COPYFROM 24
COPYTO   0
COPYFROM 23
JUMP     test

continue:
BUMPUP   10
COPYFROM 24
COPYTO   0
BUMPDN   23
JUMPZ    start
BUMPUP   23
JUMP     test

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注