Registers [local]

In Smali, registers are used to store any type of data (such as int, float, boolean, objects, and arrays), except for double and long values, where each one requires two registers for 64 bits and then pass arguments to the method. There are two types of registers: local and param(eter) registers


Let’s talk about registers in more detail today

So, I was thinking, why not take a stroll through some examples? It’ll be like a fun walk in the park, but instead of trees and flowers, we’ll have code and awesomeness. Get ready to explore and learn in a whole new way!

NOTE: If you’re curious about the building process, you can check out this link: https://github.com/AbhiTheModder/understand-smali/blob/main/AndroidIDE.md. It’s got all the details on how to download, install, and build an APK in AndroidIDE. I used it to generate the APK for the explanations in this guide. But no worries if you’re not into that right now. You can always just jump straight into the Smali explanation part.

Let’s start

Basically, there are two types of registers. First: local, Second: Param(eter) registers

Local registers are used to store local variables within a method. They are numbered from v0 to vN, where N is the number of local variables in the method.

ME: did you get it now, did you see how MT increased the registers count from 1 to 2 there now I hope you’ll understand
YOU: (ME: I’ll wait your comments about this if you get it right or not)

Extra INFO:

Ques.) How to increase register count inside a method safely and identify new usable registers?

Ans.) Increase the register count of the method, and use the newly created registers the only gotcha is that the new registers aren’t at the end of the register range - they’re actually just before the parameter registers. For example, let’s take a method that has 5 registers total (.registers 5), 3 of which are parameter registers. So you have v0 and v1 which are non-param registers, and p0-p2 which are the 3 parameter registers, and are aliases for v2-v4. If you need to add an additional 2 registers, you would bump it up to .registers 7. The parameter registers stay at the end of the register range, so p0-p2 are now aliased to v4-v6, and v2 and v3 are the new registers that are safe to use. > Source: JesusFreke on StackOverflow

If you’re having difficulty understanding above ans. by Freke, let’s take an example of cricket:

Think of it like this: You’re in the middle of a cricket match with your pals, ok? There are 5 of you out there, and you’ve got this solid trio you always toss the ball to – they’re like your go-to guys, yep you get it by now we call them the ‘parameter registers,’ because passing to them feels like your second nature. Now, imagine you want to up your game by bringing in a couple of new players, aiming to rack up more runs. But here’s the catch: you can’t just stick them at the back; that’s where your main squad is. So, you slot them in just before your usual crew. Voila, you’ve now got a 7-player lineup! But wait a sec… Your original three musketeers? They’re still your ‘parameter registers,’ the ones you rely on the most, now chilling at the back. And the newbies? They’re your ’new registers.’ You’re breaking them in, using them to swing the bat or roll the arm over. They’re fresh on the team, so you’re still getting the hang of playing with them, but they’re all set for action.

TBC...
← Basics Registers [param] →