#4 编程(Part.4)密码锁
-郭两岁
编辑于 2023年08月04日 08:01
收录于文集
共30篇


这一关也是属于那种

可以很简单也可以很难的关

先上标准答案,最简单最无脑

从1开始试直到正确答案,暴力穷举法

总时钟刻1029总用时61740

很炸裂的延迟

代码块
Python
自动换行
复制代码
1
copy|0*8|1	#reg1=1

label loop

add			#reg3=reg1+reg2
copy|3*8|output#output=reg3
copy|3*8|2	#reg2=reg3

loop
always
复制成功

密码锁(暴力穷举版)


但是就在关卡说明那里

也已经三番四次地在说

如果你猜的数字比他大就会哔哔叫(输入1,否则0)

所以很显然这关他想要的解法是要通过比较

我第一个想到的想法是二分法

先输出128,比较,然后±64,接着比较±32,±16…直到出结果

最多比较7-8次就可以出结果,延迟应该挺低的

但是我忽略了一个问题

我的代码写了将近90行

但是这是OVERTURE架构,立即数最大是63

也就是指令寻址最大不能超过63

那我63之后的代码就无法被条件跳转到

就很麻烦

(OVERTURE架构真的难用,太喜欢LEG架构了,速速学习!)

为此我决定写一个阉割版的二分法

直接上63,126,189,252

都比较一遍,然后找到小区间再-1-1-1

比如我找到数字再63-126之间

那我就126,125,124…不断地往下输出

我的二分只分两次,分到63就不分了

这样子把代码压缩到42行就能成功运行

最终时钟刻压到了323,总用时19380

缩短到了穷举法的1/3,很不错

虽然我觉得这个数字还不是很理想

但是我暂时没有什么好的想法了

我想过把一开始的二分步进改成31/32

这样子分多一次应该就可以再少一点

但是在想怎么处理溢出,给我脑袋CPU干烧了

所以就作罢

只能说OVERTURE架构,能力有限(大嘘


下面放上代码

代码块
Python
自动换行
复制代码
#二分法(完整版)
#只是一开始写完是这样,但是因为运行不了,所以也不知道这段程序有没有bug
63
copy|0*8|1
copy|0*8|2
add
copy|3*8|2
1
copy|0*8|1
add
copy|3*8|1
#make 127 in reg1

copy|1*8|4
63
copy|0*8|2
1
copy|0*8|1
add
copy|3*8|2
#make 64 in reg2
copy|4*8|1


copy|1*8|output#output=reg1(127)
#output127 to see in which range
copy|input*8|3#reg3=input
#0 is less,1 is greater
equal_0_64
equal_0

sub#比密码大,减去一半
finish_64
always
label equal_0_64
add#比密码小,加上一半
label finish_64

copy|3*8|1
32
copy|0*8|2
copy|3*8|output#output=reg3
#output to see in which range
copy|input*8|3#reg3=input
#0 is less,1 is greater
equal_0_32
equal_0

sub
finish_32
always
label equal_0_32
add
label finish_32

copy|3*8|1
16
copy|0*8|2
copy|3*8|output#output=reg3
#output to see in which range
copy|input*8|3#reg3=input
#0 is less,1 is greater
equal_0_16
equal_0

sub
finish_16
always
label equal_0_16
add
label finish_16

copy|3*8|1
8
copy|0*8|2
copy|3*8|output#output=reg3
#output to see in which range
copy|input*8|3#reg3=input
#0 is less,1 is greater
equal_0_8
equal_0

sub
finish_8
always
label equal_0_8
add
label finish_8

copy|3*8|1
4
copy|0*8|2
copy|3*8|output#output=reg3
#output to see in which range
copy|input*8|3#reg3=input
#0 is less,1 is greater
equal_0_4
equal_0

sub
finish_4
always
label equal_0_4
add
label finish_4

copy|3*8|1
2
copy|0*8|2
copy|3*8|output#output=reg3
#output to see in which range
copy|input*8|3#reg3=input
#0 is less,1 is greater
equal_0_2
equal_0

sub
finish_2
always
label equal_0_2
add
label finish_2

copy|3*8|1
1
copy|0*8|2
copy|3*8|output#output=reg3
#output to see in which range
copy|input*8|3#reg3=input
#0 is less,1 is greater
equal_0_1
equal_0

sub
finish_1
always
label equal_0_1
add
label finish_1
复制成功


代码块
Python
自动换行
复制代码
#二分法(阉割版)
63
copy|0*8|1
copy|0*8|2
copy|1*8|output#63

copy|input*8|3#0 is less,1 is greater
greater
greater_0#less than 63
add#greater than63
copy|3*8|1
copy|1*8|output#126

copy|input*8|3#0 is less,1 is greater
greater
greater_0
add#greater than126
copy|3*8|1
copy|1*8|output#189

copy|input*8|3#0 is less,1 is greater
greater
greater_0
add#greater than189
copy|3*8|1
copy|1*8|output#252

copy|input*8|3#0 is less,1 is greater
greater
greater_0
add#greater than252
1
copy|0*8|2
add
copy|3*8|output#253
copy|3*8|1
add
copy|3*8|output#254
copy|3*8|1
add
copy|3*8|output#255


label greater
1
copy|0*8|2

label jump
sub
copy|3*8|output
copy|3*8|1
jump
always
复制成功

密码锁(二分法版)


OVERTURE架构指令集:

最高两位为操作码,00立即数模式,01算术模式,10复制模式,11条件跳转模式 立即数模式下,1-6位是立即数,写入0号寄存器 算术模式下,1-3位为算术命令,000为OR,001为NAND, 010NOR, 011为AND, 100为ADD, 101为SUB, 110和111为UNUSED 复制模式下,1-3为目的,4-6为源,000-101对应寄存器,110对应输入/输出,111UNUSED 条件跳转模式下,1-3位为条件,000为NEVER, 001为=0, 010为<0, 011为<=0, 100为ALWAYS, 101为!=0, 110为>=0, 111为>0

自定义汇编别名:

input/output: 00000110(6) AND: 01000011(67) add: 01000100(68)  sub: 01000101(69) copy: 10000000(128) equal_0: 11000001(193) less_0: 11000010(194) always: 11000100(196) greater_0: 11000111(199)