Python 2.7 如何在Python中设置列表/数组的偏移量/指针?

Python 2.7 如何在Python中设置列表/数组的偏移量/指针?,python-2.7,Python 2.7,具有讽刺意味的是,我在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, 0

具有讽刺意味的是,我在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,依此类推

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

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


事实上,现在我想起来了。。。似乎我被困在“组装”的做事方式中><希望这一切消失。。。我无法想象“大画面”。Python中的列表访问不是很复杂。有一些不错的介绍性例子

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

list_name[index]
要将值赋给变量,只需使用赋值语句:

some_var = list_name[index]
要将值写入列表,只需将赋值语句转过来:

list_name[index] = some_value
我想你的一些作业是逆向的

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

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

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
运行此命令将增加查找列表中的两个值,我假设这是一个期望的副作用。lookup_main也将递增一次,如果超过7,则环绕

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


在逻辑中使用模数运算符%来递增查找_main可能会更清楚一些,如果它达到8,则强制它环绕到零。类似地,使用左移rnd2,您能否给出一个示例,说明调用函数的预期效果?它不会返回任何东西,我也不清楚你想要它做什么。还有,什么是offset2,它在任何地方都没有定义?@Blckknght我已经编辑了我的帖子,以包含更多信息。我想编写一个随机数查找表。我要花一些时间来理解你的汇编代码,但是如果目的只是生成一个随机数,有更好的方法来实现。Python的随机模块提供了多种生成随机值的方法。这是高级语言的一大优势:许多常见问题已经解决,并且有简单的方法可以重用这些解决方案。@Blckknght没问题。没关系。我正在尝试对这个RNG进行反向工程。我只想访问python中的数组/列表,但我不知道语法。我到处都找不到它p@Blckknght在类似C的伪代码中,它非常简单:{int rnd1;rnd1=返回字节_0x83084[byte_0x62e10[word_0x62e18]+];}
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