【程序员升职记系列】乘法研讨会

第20关,乘法研讨会。指令介绍请看:https://www.annhe.net/article-3828.html

优化目标:

15行109步

代码

以第一个输入作为待相加的数,第二个输入为相加次数,可以优化代码行数。

15行136步

-- HUMAN RESOURCE MACHINE PROGRAM --

    JUMP     f
a:
    COPYFROM 2
c:
    OUTBOX
f:    
    INBOX
    COPYTO   0
    COPYTO   2
    INBOX
    COPYTO   1
    JUMPZ    c
h:
    BUMPDN   1
    JUMPZ    a
    COPYFROM 2
    ADD      0
    COPYTO   2
    JUMP     h

28行107步

-- HUMAN RESOURCE MACHINE PROGRAM --

    JUMP     f
z:  COPYFROM 9
    JUMP     c    
a:
    COPYFROM 2
c:
    OUTBOX
f:    
    INBOX
    JUMPZ    i
    COPYTO   0
    COPYTO   2
    INBOX
    JUMPZ    c
    COPYTO   1
    SUB      0
    JUMPN    h
    COPYFROM 1
    COPYTO   2
    COPYFROM 0
    COPYTO   1
    COPYFROM 2
    COPYTO   0
h:
    BUMPDN   1
    JUMPZ    a
    COPYFROM 2
    ADD      0
    COPYTO   2
    JUMP     h
i:
    INBOX
    JUMP     z            

废弃代码

下面几段代码将第一个输入作为相加次数,导致多了几次存取,行数和效率都不好。

27行135步

-- HUMAN RESOURCE MACHINE PROGRAM --

    JUMP     f
a:
    COPYFROM 2
    JUMP     c
b:
    COPYFROM 9
c:
    OUTBOX  
    JUMP     e
d:
    COPYFROM 0
    COPYTO   2
    COPYFROM 1
    COPYTO   0
    COPYFROM 2
    COPYTO   1
    JUMP     g
e:
f:
    INBOX   
    COPYTO   0
    INBOX   
    COPYTO   1
    COPYTO   2
    SUB      0
    JUMPN    d
g:
h:
    BUMPDN   0
    JUMPN    b
    JUMPZ    a
    COPYFROM 2
    ADD      1
    COPYTO   2
    JUMP     h

31行121步

-- HUMAN RESOURCE MACHINE PROGRAM --

    JUMP     f
a:
    COPYFROM 2
    JUMP     c
b:
    COPYFROM 9
c:
    OUTBOX
f:    
    INBOX
    JUMPZ    i    
    COPYTO   0
    INBOX
    JUMPZ    j    
    COPYTO   1
    COPYTO   2
    SUB      0
    JUMPN    d
h:
    BUMPDN   0
    JUMPN    b
    JUMPZ    a
    COPYFROM 2
    ADD      1
    COPYTO   2
    JUMP     h
i:
    INBOX
    JUMP b
j:
    JUMP b
d:
    COPYFROM 0
    COPYTO   2
    COPYFROM 1
    COPYTO   0
    COPYFROM 2
    COPYTO   1
    JUMP     h    

One thought on “【程序员升职记系列】乘法研讨会

  1. -- HUMAN RESOURCE MACHINE PROGRAM --

    JUMP d
    a:
    INBOX
    COPYFROM 0
    OUTBOX
    JUMP e
    b:
    COPYFROM 2
    c:
    OUTBOX
    d:
    e:
    INBOX
    COPYTO 0
    JUMPZ a
    COPYTO 2
    INBOX
    COPYTO 1
    JUMPZ c
    f:
    BUMPDN 1
    JUMPZ b
    COPYFROM 2
    ADD 0
    COPYTO 2
    JUMP f

    在15行139步基础上增加了一个乘数为0的判断;

发表回复

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