Flag usage in assembly (advanced)

Sep 1, 2009 at 8:03 AM
Been here way too long...
"Ha! Ha! Ha! Mega Man is no match for my Mimiga Man!"
Join Date: Jun 22, 2008
Location:
Posts: 251
GIR was asking how to use flags in assembly, so I made this post since he may not be the only one wondering.
===========================

say [0049E650] is the variable for the equip flag used with EQ+ and EQ- (it is)

Now say you and to equip flag 8. Flag 8 in binary is 00001000 so we use OR with the flag variable, like so

AND [0049E650],8

lets look at this in more detail. lets say currently flags 1,2,16,and 64 are set.That means that in binary, [0049E650] currently says 01010011. The OR instruction sets a flag if the flag is set in the first parameter of the instruction OR if the flag is set in the second parameter of the instruction. Here are the two variables on top of each other, with the result underneath them:

Code:
00001000 (the 8)
01010011 (the [0049E650])
--------
01011011 (the [0049E650] after the OR instruction)

Unsettting a flag is a bit more complex. It is done with the AND command. First take the flag you want to unset. Say we changed our minds and now want to unset flag 8. First take the binary of flag 8: 00001000. Then inverse it by making the 1's 0's and making the 0's 1's. The result is 11110111. Convert that back to hex, which results in F7. Now AND that with your flag variable. The AND instruction sets the flag if the flag is set in the first paramater AND the second paramater. Again, that would look like this:

Code:
11110111 (the F7)
01011011 (the [0049E650])
--------
01010011 (the [0049E650] after the AND instruction)

And of course the last thing is checking a flag. This is done by using AND with the flag variable and the flag you want to check. If the flag is set, the result will be the flag you are trying to check. If the flag is unset, the result will be 0. Then use a CMP like any branching code. So lets say we want to check if flag 8 is set. If we used AND [0049E650],8 we would unset all the flags but 8, which is not what we want. Instead, move the flag variable to a temporary location, usually a register. Example code:

MOV EAX,[0049E650]
AND EAX,8
CMP EAX,0
JGE (offset to what you do if the flag is set)
(what you do if the flag isn't set)

One thing to note: all these values are one byte long. Since most of the time you will be using a dword (4 bytes) you need to take that into account. So the 8 would be 00000008 and the F7 would be FFFFFFF7.


Anyway, what I've basically explained here is the basics of binary logic operations. Some other ones are NOT, NOR, XOR, NXOR, and NAND. If you want to know more, do some googling. It might take a bit of effort, my 20 seconds of searching didn't turn up a whole lot, so be prepared to spend 10-15 minutes on this. You shouldn't really need to for hacking, though, you will probably only see/need OR and AND.

=========================
If any of this needs clarification, let me know!
 
Sep 1, 2009 at 12:42 PM
Been here way too long...
"Life begins and ends with Nu."
Join Date: Jan 4, 2008
Location: Lingerie, but also, like, fancy curtains
Posts: 3054
now, why are the eq+ and - functions so dang long?
good info though.
 
Sep 2, 2009 at 12:56 AM
Been here way too long...
"Life begins and ends with Nu."
Join Date: Jan 4, 2008
Location: Lingerie, but also, like, fancy curtains
Posts: 3054
eh?
I must've missed the end of the function then, looked soopa long to me.
as someone who had trouble with equip flags a while back, this be a good resource.
 
Top