[英]How to set the offset/pointer to a list/array in Python?
Ironically I have made this program successfully in x86 ASM but I'm having major trouble with implementing it in Python. 具有讽刺意味的是,我已经在x86 ASM中成功完成了该程序,但是在Python中实现它遇到了很大的麻烦。
I have only just started learning a high level language. 我才刚刚开始学习高级语言。 Here is a snippet:
这是一个片段:
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()
I want to use lookup_main as a kind of pointer to the lookups list. 我想将lookup_main用作指向查找列表的一种指针。 So, position 0 in lookups is 0. Position 1 is 2, and so on.
因此,查找中的位置0为0。位置1为2,依此类推。
I want to therefore increase this pointer/offset, but I am clearly doing it wrong. 因此,我想增加此指针/偏移量,但是我显然做错了。
How can I increment the offset to the list? 如何增加列表的偏移量? I don't want to directly manipulate any numbers in the list.
我不想直接操纵列表中的任何数字。 I just want to change the offset.
我只想更改偏移量。
Actually, now that I think about it... it seems I'm stuck thinking in the 'Assembly' way of doing things >.< Hope this goes away... I'm unable to think 'big picture' >.<'''' 实际上,现在,我开始思考……似乎我一直在以“组装”方式做事>。<希望这一切消失了……我无法思考“大局”>。 ''''
The (dis)assembly that works is like: 起作用的(反汇编)如下:
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
List access in Python is not very complicated. Python中的列表访问不是很复杂。 The Python tutorial has some decent introductory examples.
Python教程有一些不错的入门示例。
To read a value from a list, just provide a suitable index: 要从列表中读取值,只需提供合适的索引即可:
list_name[index]
To assign the value to a variable, just use an assignment statement: 要将值分配给变量,只需使用赋值语句:
some_var = list_name[index]
To write a value into a list, just turn the assignment statement around: 要将值写入列表,只需将赋值语句转过来:
list_name[index] = some_value
I think you had a few of your assignments backwards. 我认为您的工作分配有些落后。
The other major issue you had was due to using the logical or
and and
operators instead of the bitwise |
您遇到的另一个主要问题是由于使用逻辑“
or
和“ and
运算符而不是按位|
and &
operators. 和
&
运算符。 The former won't do what you want, in most cases. 在大多数情况下,前者不会做您想要的。
Here's some fixed code. 这是一些固定的代码。 I've omitted the
offset
values, since they're not really needed: 我已经省略了
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
Two values in the lookups
list will be incremented by running this, which I assume is a desired side effect. 通过运行此命令,
lookups
列表中的两个值将增加,我认为这是所需的副作用。 lookup_main
will also be incremented once, wrapping around if it goes above 7. lookup_main
也将增加一次,如果超过7则回绕。
As I commented in the code, some of the bitwise operations might be more clear if you used different operators (with the same effects). 正如我在代码中评论的那样,如果您使用不同的运算符(效果相同),则某些按位运算可能会更清晰。 This is a style judgement, so your mileage may vary.
这是一种风格判断,因此您的里程可能会有所不同。
Using the modulus operator %
in the logic to increment lookup_main
may be a bit clearer that you're forcing it to wrap around to zero if it reaches 8. Similarly, left shifting rnd2
with the <<
operator may be more clear than doing a multiplication. 在逻辑中使用模数运算符
%
来增加lookup_main
可能会更清楚一点,即如果它达到8,则将其强制环绕为零。类似地,用<<
操作符向左移动rnd2
可能比乘法更清楚。 。 I'd also suggest using hexidecimal for any constants you use in bitwise operations (so 0xFFFF
instead of 65535
). 我还建议将十六进制用于在按位运算中使用的任何常量(因此为
0xFFFF
而不是65535
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.