So, I'm currently in the process of teaching myself reverse engineering for the purpose of being able to analyze malware. And I have to be honest, it's a very slow and painful process for me. Assembly code just can't seem to get into my head.
To distract myself a bit from the tedious path, I've quickly coded a hex calculator in Python. When you read and try to understand assembly code, you often have to calculate addresses in memory. And sometimes you need to be able to perform different operations with these addresses. So, this will come very in handy as I advance into the field.
It's only a couple of lines of code and to run it, you only need to do this:
- python hexcalc.py address1 operation address2
For example, if I want to calculate 0xf + 0xf, the line arguments will be:
- python hexcalc.py 0xf + 0xf
Simple right? Indeed. So, if this is useful to you, go ahead and grab the code from my github below.
To stay in touch with me, follow @cristi
Cristi Vlad Self-Experimenter and Author
Interesting.
You do seem to be going about it slightly inefficiently, though. There are some built-in methods in Python to help with these sorts of things.
Firstly there is the eval() function, if you pass it a string it attempts to run it as Python code, then there's hex() which takes an integer and converts it to hex.
So if you modify the code you can turn your try block into just:
a = sys.argv[1] b = sys.argv[2] c = sys.argv[3] if b == "add": b = "+" elif b == "sub": b = "-" elif b == "mul": b = "*" elif b == "div": b = "/" print(hex(eval(a + b + c)))
Which has a lot less munging of strings to get them working.
Agree and i recomend https://docs.python.org/3/library/argparse.html
I very rarely write any Python programs that need command line arguments, so haven't used argparse, but it's one of those modules that's on my radar if I ever need to write one.
Thanks! Appreciate the addendum.
i will like to follow you on this journey, but i fear you might drop me off as i may take more time and pain to learn these things, assembly coding is by far one heck of a code system
jump in.
Hey! Quick question - why don't you publish these via Utopian.io? They will get more attention and (possibly) get a reward from @utopian-io .
How would that be different than here? :-/
so you just post like you do normally and simply tag with utopian-io ?
And use the template and follow guidelines. I recommend you join our Discord channel and chat with the mods in the dev / tutorial categories. You can start here: https://join.utopian.io/guidelines/