View previous topic :: View next topic |
Author |
Message |
bknight2602 Grandmaster Cheater Reputation: 0
Joined: 08 Oct 2012 Posts: 577
|
Posted: Sun Jun 25, 2017 8:42 am Post subject: How to read the value of a 3 byte address |
|
|
What code would be necessary to read a 3 byte address
0xXXXXXX1, 0xXXXXXX2, 0xXXXXXX3, AND not including 0xXXXXXX4? |
|
Back to top |
|
|
++METHOS I post too much Reputation: 92
Joined: 29 Oct 2010 Posts: 4197
|
Posted: Sun Jun 25, 2017 8:55 am Post subject: |
|
|
Can you elaborate? |
|
Back to top |
|
|
ParkourPenguin I post too much Reputation: 138
Joined: 06 Jul 2014 Posts: 4275
|
Posted: Sun Jun 25, 2017 9:16 am Post subject: |
|
|
Code: | local b1,b2,b3 = readBytes(address,3)
local val = b1 | b2 << 8 | b3 << 16 |
_________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
Back to top |
|
|
mgr.inz.Player I post too much Reputation: 218
Joined: 07 Nov 2008 Posts: 4438 Location: W kraju nad Wisla. UTC+01:00
|
Posted: Sun Jun 25, 2017 9:26 am Post subject: |
|
|
Code: | local val = readInteger('address') & 0xffffff |
_________________
|
|
Back to top |
|
|
FreeER Grandmaster Cheater Supreme Reputation: 53
Joined: 09 Aug 2013 Posts: 1091
|
Posted: Sun Jun 25, 2017 11:04 am Post subject: |
|
|
As the experts have provided code for, you generally use bitwise operators.
ParkourPenguin's reads the 3 bytes and uses the bitwise "or" operator and "left shift" (essentially multiplies by 2 to the power of whatever the second value is) to combine them together.
I'm going to slightly rewrite mgr.inz.Player's to show the implied 0 digits
Code: | local val = readInteger('address') & 0x00ffffff |
Now mgr.inz.Player's code reads 4 bytes and uses the bitwise "and" operator to ignore the most significant byte (change it to 0). Since little endian systems (like windows) store the bytes in multibyte values in reverse this actually has the same effect as ParkourPenguin's even though it seems like one is ignoring the first byte and the other is ignoring the last.
To achieve this without any bitwise operators (less efficient but perhaps more understandable) you can do this:
Code: | -- copy last byte that we don't want
-- +0 is the first, +1 the second, +2 the third, +3 the fourth
lastByte = readBytes(address+3,1)
-- overwrite it with 0
writeBytes(address+3, 0)
-- read the value
value = readInteger(address)
-- restore the last byte
writeBytes(address+3, lastByte)
|
|
|
Back to top |
|
|
bknight2602 Grandmaster Cheater Reputation: 0
Joined: 08 Oct 2012 Posts: 577
|
Posted: Sun Jun 25, 2017 6:55 pm Post subject: |
|
|
++METHOS wrote: | Can you elaborate? |
Yes for example 3e 92 03 when input into a calculator = 234046 (inverse like in games)
ParkourPenguin wrote: | Code: | local b1,b2,b3 = readBytes(address,3)
local val = b1 | b2 << 8 | b3 << |
|
I don't get this to work the vertical bar isn't recognized
mgr.inz.Player wrote: | Code: | local val = readInteger('address') & 0xffffff |
|
Yes, that worked, without the 0xffffff. I thought I tried it earlier today, but I must have mistyped it because it works tonight.
EDIT: I checked another value it that doesn't work as this morning.
FreeER wrote: | As the experts have provided code for, you generally use bitwise operators.
ParkourPenguin's reads the 3 bytes and uses the bitwise "or" operator and "left shift" (essentially multiplies by 2 to the power of whatever the second value is) to combine them together.
Or secondarily how to code the vertical bar "or" in
I'm going to slightly rewrite mgr.inz.Player's to show the implied 0 digits
Code: | local val = readInteger('address') & 0x00ffffff |
Now mgr.inz.Player's code reads 4 bytes and uses the bitwise "and" operator to ignore the most significant byte (change it to 0). Since little endian systems (like windows) store the bytes in multibyte values in reverse this actually has the same effect as ParkourPenguin's even though it seems like one is ignoring the first byte and the other is ignoring the last.
To achieve this without any bitwise operators (less efficient but perhaps more understandable) you can do this:
Code: | -- copy last byte that we don't want
-- +0 is the first, +1 the second, +2 the third, +3 the fourth
lastByte = readBytes(address+3,1)
-- overwrite it with 0
writeBytes(address+3, 0)
-- read the value
value = readInteger(address)
-- restore the last byte
writeBytes(address+3, lastByte)
|
|
Would writing a 0 in the fourth byte crash the game? Is there a method without writing?
Or what is the code for the vertical bar "or" as ParkourPenguin posted?
Thanks to all |
|
Back to top |
|
|
ParkourPenguin I post too much Reputation: 138
Joined: 06 Jul 2014 Posts: 4275
|
Posted: Sun Jun 25, 2017 7:23 pm Post subject: |
|
|
bknight2602 wrote: | I don't get this to work the vertical bar isn't recognized |
That's another reason why you should use the latest version of CE instead of outdated software. Use multiplication and addition instead. If you don't know how to, use Google to learn what the bitwise operators do and you should be able to figure it out. _________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
Back to top |
|
|
bknight2602 Grandmaster Cheater Reputation: 0
Joined: 08 Oct 2012 Posts: 577
|
Posted: Sun Jun 25, 2017 8:53 pm Post subject: |
|
|
ParkourPenguin wrote: | bknight2602 wrote: | I don't get this to work the vertical bar isn't recognized |
That's another reason why you should use the latest version of CE instead of outdated software. Use multiplication and addition instead. If you don't know how to, use Google to learn what the bitwise operators do and you should be able to figure it out. |
I did https://en.wikipedia.org/wiki/Bitwise_operation#OR
http://lua-users.org/wiki/BitwiseOperators
If you are referring to bit.bor(x1[,x2...]), I'm afraid that is above my pay grade.
I did try local val = b1 bit.bor[ b2] << 8 bit.bot [b3] << 16 but I don't understand, as it gives an error '=' expected near '<' |
|
Back to top |
|
|
ParkourPenguin I post too much Reputation: 138
Joined: 06 Jul 2014 Posts: 4275
|
Posted: Sun Jun 25, 2017 9:02 pm Post subject: |
|
|
ParkourPenguin wrote: | Use multiplication and addition instead. |
Code: | 5 << 3 == 5 * 2^3 == 5 * 8
0x130000 + 0xF900 + 0x31 == 0x13F931 |
_________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
Back to top |
|
|
Zanzer I post too much Reputation: 126
Joined: 09 Jun 2013 Posts: 3278
|
Posted: Sun Jun 25, 2017 9:49 pm Post subject: |
|
|
Code: | local value = readBytes(address, 3, true)
table.insert(value, 0)
value = byteTableToDword(value) |
Code: | value = dwordToByteTable(value)
table.remove(value)
writeBytes(address, value) |
|
|
Back to top |
|
|
bknight2602 Grandmaster Cheater Reputation: 0
Joined: 08 Oct 2012 Posts: 577
|
Posted: Mon Jun 26, 2017 7:11 am Post subject: |
|
|
ParkourPenguin wrote: | ParkourPenguin wrote: | Use multiplication and addition instead. |
Code: | 5 << 3 == 5 * 2^3 == 5 * 8
0x130000 + 0xF900 + 0x31 == 0x13F931 |
|
I see your point, the only tweak is to convert the readBytes value to hex from the decimal value obtained to make the multiplication correct.
Zanzer wrote: | Code: | local value = readBytes(address, 3, true)
table.insert(value, 0)
value = byteTableToDword(value) |
Code: | value = dwordToByteTable(value)
table.remove(value)
writeBytes(address, value) |
|
That does work, but the write part isn't required/requested at this time.
Thanks to both of you. |
|
Back to top |
|
|
mgr.inz.Player I post too much Reputation: 218
Joined: 07 Nov 2008 Posts: 4438 Location: W kraju nad Wisla. UTC+01:00
|
Posted: Mon Jun 26, 2017 2:48 pm Post subject: |
|
|
bknight2602 wrote: | mgr.inz.Player wrote: | Code: | local val = readInteger('address') & 0xffffff |
|
Yes, that worked, without the 0xffffff. I thought I tried it earlier today, but I must have mistyped it because it works tonight.
EDIT: I checked another value it that doesn't work as this morning. |
If you are using older CE version, then try this:
Code: | local val = readInteger('address') % 0x1000000 |
or this
Code: | local val = bAnd( readInteger('address') , 0x00ffffff) |
_________________
|
|
Back to top |
|
|
bknight2602 Grandmaster Cheater Reputation: 0
Joined: 08 Oct 2012 Posts: 577
|
Posted: Mon Jun 26, 2017 3:59 pm Post subject: |
|
|
mgr.inz.Player wrote: | bknight2602 wrote: | mgr.inz.Player wrote: | Code: | local val = readInteger('address') & 0xffffff |
|
Yes, that worked, without the 0xffffff. I thought I tried it earlier today, but I must have mistyped it because it works tonight.
EDIT: I checked another value it that doesn't work as this morning. |
If you are using older CE version, then try this:
Code: | local val = readInteger('address') % 0x1000000 |
or this
Code: | local val = bAnd( readInteger('address') , 0x00ffffff) |
|
Both choices worked and the second pointed me in the direction of how to code the and/or also. |
|
Back to top |
|
|
bknight2602 Grandmaster Cheater Reputation: 0
Joined: 08 Oct 2012 Posts: 577
|
Posted: Thu Mar 31, 2022 9:27 am Post subject: |
|
|
I'm going to open this necro thread for another question.
Ok we have numbers than span 3 bytes such as 3e 92 03 at known addresses, but this time i would like to add for example a number larger than two bytes say 70000.
What would be the best code for that operation? |
|
Back to top |
|
|
panraven Grandmaster Cheater Reputation: 55
Joined: 01 Oct 2008 Posts: 941
|
Posted: Thu Mar 31, 2022 10:20 am Post subject: |
|
|
Assume overflow not matter, it might do it just like 4 byte during read and addition then only write the 3 bytes like:
Code: |
mov eax,[<-some-input-address->]
add eax,#70000
mov [<-some-output-address>],ax /// write 1st (least significant byte),2nd byte
sar eax,16 /// shift 3rd byte to 1st byte position (2 byte space, 16-bit), keep signed
mov [<-some-output-address>+2],al /// write 3rd byte (most significant byte)
** sar = shift arithmetic right
|
ADDED:
oops, I thought it is AA, sorry~ It should work similarly with lua. _________________
- Retarded. |
|
Back to top |
|
|
|