1

具有讽刺意味的是,我已经在 x86 ASM 中成功地制作了这个程序,但是在 Python 中实现它时遇到了很大的麻烦。

我才刚刚开始学习高级语言。这是一个片段:

rn_table = [0x63, 0x06, 0xF0, 0x23, 0xF8, 0xE5, 0xA8, 0x01, 0xC1, 0xAE, 0x7F, 0x48,
0x7B, 0xB1, 0xDC, 0x09, 0x22, 0x6D, 0x7D, 0xEE, 0x9D, 0x58, 0xD5, 0x55, 0x24, 0x39, 0x7A, 0xDF,
0x8E, 0x54, 0x6C, 0x1B, 0xC0, 0x0B, 0xD0, 0x43, 0xD8, 0x9A, 0x47, 0x5D, 0x21, 0x02, 0x17, 0x4B,
0xDB, 0x11, 0xAF, 0x70, 0xCD, 0x4D, 0x34, 0x49, 0x72, 0x91, 0x2D, 0x62, 0x97, 0x59, 0x45, 0xF7,
0x6E, 0x46, 0xAA, 0x0A, 0xA3, 0xC8, 0x31, 0x92, 0x38, 0xFA, 0xD4, 0xE6, 0xCB, 0xF3, 0xDE, 0x6B,
0xBB, 0xF1, 0x1C, 0x3C, 0xD6, 0xAD, 0xB2, 0xA9, 0xDD, 0x57, 0x42, 0x95, 0x0C, 0x79, 0x25, 0x1F,
0xBC, 0xE7, 0xAC, 0x5B, 0x83, 0x28, 0x76, 0xF2, 0x18, 0xDA, 0x87, 0xA1, 0x61, 0x6F, 0xBE, 0x5A,
0x5E, 0x51, 0xEF, 0xB0, 0xC9, 0x15, 0x74, 0x89, 0xBD, 0xD1, 0xA2, 0x75, 0xD7, 0x99, 0x85, 0x4C,
0x4F, 0xD2, 0xBF, 0x4A, 0x20, 0x08, 0x56, 0xA0, 0x50, 0x3A, 0x67, 0x26, 0x41, 0x33, 0xB7, 0xBA,
0xFB, 0x30, 0xCF, 0x7C, 0x84, 0x2C, 0x32, 0xE9, 0x1D, 0x16, 0x82, 0x78, 0xA4, 0x80, 0x65, 0x5F,
0x0E, 0x27, 0xB9, 0x19, 0xC3, 0xA7, 0xB6, 0x00, 0x3B, 0xFC, 0x88, 0xE1, 0xC6, 0x93, 0xFE, 0x8B,
0xD9, 0xB8, 0x13, 0x69, 0x2F, 0x64, 0x12, 0x37, 0xFD, 0x77, 0xE2, 0xB5, 0x04, 0xE0, 0x1A, 0x8C,
0x8F, 0xB4, 0xCC, 0xF9, 0x60, 0xEB, 0x29, 0xE3, 0x90, 0xA5, 0x68, 0x3D, 0x81, 0x73, 0x3F, 0xAB,
0x7E, 0xB3, 0x0F, 0xCE, 0xC4, 0x35, 0x94, 0x96, 0x86, 0x71, 0xD3, 0x2A, 0xE4, 0x9F, 0x9C, 0xEC,
0x4E, 0x14, 0xF5, 0xEA, 0x40, 0xA6, 0xF6, 0x03, 0x98, 0xC5, 0x07, 0xF4, 0x2B, 0xC2, 0x3E, 0xE8,
0x9B, 0x36, 0x53, 0x2E, 0x8D, 0x0D, 0x52, 0x10, 0x66, 0x1E, 0xED, 0x8A, 0x44, 0x9E, 0x05, 0xFF,
0x5C, 0xC7, 0x6A, 0xCA, 0x00, 0x0C, 0x12, 0x19, 0x21, 0x27, 0x2D, 0x35, 0x3D, 0x49, 0x51, 0x5A,
0x62, 0x69, 0x72, 0x7A, 0x83, 0x8B, 0x94, 0x9D, 0xA5, 0xAB, 0xB2, 0xB9, 0xC1, 0xC9, 0xD2, 0xD9,
0xDE, 0xE3, 0xE8, 0xED, 0xF5]


cluck = 16
clevel = 12
enemy_level = 10

crit_pc = (cluck + clevel - enemy_level) / 4

offset2 = 0
offset3 = 0

rnd1 = 0
rnd2 = 0
rnd0 = 0

crit_rnd = 0

lookups = [0, 0, 0, 0, 0, 0, 0, 0]
lookup_main = 0


def rng_go():
    global lookup_main
    lookups[lookup_main] = offset2      # lookup_main chooses lookups item. position = offset2
    lookups[lookup_main] += 1           # increase the lookups chosen value by 1 (previously offset2)
    rn_table[offset2] = rnd1            # choose value from rn_table, position of value is offset2

    if crit_pc == 0:
        print "No critical\n"
    lookup_main += 1                    # increment lookup_main by 1
    lookup_main = lookup_main and 7     # check lookup_main < 8
    lookups[lookup_main] = offset3      # lookup_main chooses another lookups item. position = offset3
    lookups[lookup_main] += 1           # increase lookups chosen value by 1
    rn_table[offset3] = rnd2            # choose value from rn_table, position of value is offset3
    # calculate critical%
    rnd2 * 256
    rnd0 = rnd1 or rnd2
    rnd0 = rnd0 and 65535
    crit_rnd = ((rnd0 * 99) / 65535) + 1
    if crit_pc >= crit_rnd:
        print "Critical hit success!\n"
        print "crit_rnd: %d \t crit_pc: %d" % (crit_rnd, crit_pc)
        print "rnd1: %d \t rnd2: %d" % (rnd1, rnd2)
        print "lookups: %r" % (lookups)
    else:
        print "No critical\n"
        print "crit_rnd: %d \t crit_pc: %d" % (crit_rnd, crit_pc)
        print "rnd1: %d \t rnd2: %d" % (rnd1, rnd2)
        print "lookups: %r" % (lookups)
rng_go()

我想使用 lookup_main 作为一种指向查找列表的指针。因此,查找中的位置 0 为 0。位置 1 为 2,依此类推。

因此,我想增加这个指针/偏移量,但我显然做错了。

如何增加列表的偏移量?我不想直接操作列表中的任何数字。我只想更改偏移量。

实际上,现在我想起来了……似乎我一直在思考“组装”的做事方式>。<希望这会消失……我无法思考“大局”>。< ''''

有效的(反)程序集如下:

lw r3,0x00d4(r28)      # load word from 80062e18 = offset1
lui r2, 0x8006
addiu r2,r2,0x2e10      # 80062e10
addu r3,r3,r2         # 80062e10 + (word from 80062e18 as offset)
lbu r2,0x0(r3)         # load byte from 80062e10(offset1) = offset2
nop

addiu r4,r2,0x01      
andi r2,r2,0xff
sb r4,0x0(r3)         # increment by 1 and store

lui r1,0x8008
addu r1,r1,r2         # 80083084
lbu r2,0x3084(r1)      # load byte from 80083084(offset2) = rnd1
jr r31
nop

lw r3,0x0008(r28)      # load word from 80062d4c
nop

addiu r4,r3,0x01      # increment by 1

andi r3,r3,0x07         # AND 7 (check that value < 8)
sw r4,0x08(r28)         # store incremented value back to 8062d4c


beq r3,r0,0x14c18      # if crit% = 0 then no critical
addu r16,r2,r0         # move rnd1
jal 0x14b54
nop

lw r2,0xd4(r28)         # load word from 80062e18
nop
addiu r2,r2,0x01      # increment by 1
andi r2,r2,0x07         # AND with 7 (check that value < 8) = offset2
sw r2,0xd4(r28)         # store in 80026e18
jr r31
nop

jal 0x14b70
nop

lw r3,0xd4(r28)         # load word from 80062e18 = offset3
lui r2,0x8006
addiu r2,r2,0x2e10      
addu r3,r3,r2         
lbu r2,0x0(r3)         # load byte from 80062e10(offset3) = offset4
nop

addiu r4,r2,0x01      
andi r2,r2,0xff         
sb r4,0x0(r3)         # increment byte by 1 and store in 80062e11

lui r1,0x8008         
addu r1,r1,r2         
lbu r2,0x3084(r1)      # load byte from 80083084(offset4) = rnd2
jr r31
nop

andi r2,r2,0xff         
sll r2,r2,0x08         # rnd2 * 256
andi r3,r16,0xff      
or r2,r3,r2         # rnd1 OR rnd2 (combine rnd1 and rnd2) = rnd0
lw r31,0x14(r29)
lw r16,0x10(r29)
addiu r29,r29,0x18
jr r31
nop

lw r31,0x10(r29)
andi r2,r2,0xffff      # take 2 bytes of rnd0
jr r31
addiu r29,r29,0x18


## calculate crit_rnd for which to compare against crit% ##

lui r4,0x8000
ori r4,r4,0x8001      
andi r2,r2,0xffff      
sll r3,r2,0x01         
addu r3,r3,r2         
sll r2,r3,0x05         ## Just: crit_rnd = ((rnd0 * 99) / 65535) + 1
addu r3,r3,r2         
mult r3,r4         
addu r2,r5,r3         
sra r2,r2,0x0f         
sra r3,r3,0x1f         
subu r2,r2,r3         
lw r31,0x10(r29)
addiu r2,r2,0x01      
jr r31
addiu r29,r29,0x0018      

slt r2,r16,r2         
bne r2,r0,0xb0888      # if crit% >= crit_rnd then critical hit success
nop 
4

1 回答 1

0

Python 中的列表访问并不是很复杂。Python 教程有一些不错的介绍性示例。

要从列表中读取值,只需提供合适的索引:

list_name[index]

要将值分配给变量,只需使用赋值语句:

some_var = list_name[index]

要将值写入列表,只需翻转赋值语句:

list_name[index] = some_value

我认为你的一些作业倒过来了。

您遇到的另一个主要问题是由于使用逻辑orand运算符而不是按位|&运算符。在大多数情况下,前者不会做你想做的事。

这是一些固定的代码。我省略了这些offset值,因为它们并不是真正需要的:

rnd1 = rn_table[lookups[lookup_main]]
lookups[lookup_main] += 1

lookup_main += 1
lookup_main &= 7   # doing %= 8 might be more clear

rnd2 = rn_table[lookups[lookup_main]]
lookups[lookup_main] += 1

rnd2 *= 256        # or use <<= 8 to left shift instead of multiplying

rnd0 = rnd1 | rnd2
rnd0 &= 65535      # this step should be unnecessary

运行此命令将增加列表中的两个值lookups,我认为这是期望的副作用。lookup_main也将增加一次,如果超过 7 则回绕。

正如我在代码中评论的那样,如果您使用不同的运算符(具有相同的效果),某些按位运算可能会更清楚。这是一种风格判断,因此您的里程可能会有所不同。

在逻辑中使用模数运算符%来增加lookup_main可能会更清楚一点,如果它达到 8,您将强制它回绕为零。类似地,rnd2使用<<运算符左移可能比进行乘法更清楚。我还建议对您在按位运算中使用的任何常量使用十六进制(0xFFFF而不是65535)。

于 2013-09-20T23:13:47.767 回答