Rough Book

random musings of just another computer nerd

Category: Assembly

Data and Code

Inspired by The Codeless Code:

A novice monk had just started learning assembly programming when he was troubled by doubt. He approached his master and asked:

“Master, how do I know which is code and which is data?”

The master who was meditating, opened his eyes, smiled, and said:

“Each is the other, yet neither is either.”

“Master, I do not understand.”, said the disciple.

The master then brought out two identical pots and said. “Take these. Fill one with the water from the lake, and fill the other with water from the stream that flows into the lake. Then bring them to me.”

The monk bowed and took the pots. He walked to the lake, which was some distance away and filled it with water from the lake. Then he walked around the side of the lake until he found the stream that fed water into the lake. He used this water to fill the second pot and then brought both pots back to his master and set them at his feet. The master looked at the pots, and then back to his disciple and said “Now go. You may come back tomorrow morning.”

The disciple came back the next morning to find his master standing with the two pots. He held up both the pots and then threw them to the opposite sides of the room. The pots smashed, and the water from both pots flowed towards the center of the room, forming a puddle.

The master then said, “Which pot contained water from the stream? Which pot contained water from the lake?” He then pointed to the growing puddle that was forming in the middle of the room. “Which part of the puddle contains water from the stream? Which part contains water from the lake?”

In that moment, the novice was enlightened.

Bytecode optimization in Java

I learnt something new about bytecode optimization today. In languages like C and C++, if you’re really concerned about efficiency, you can drop into assembly mode and write specific assembly code instead of relying on the compiler to convert your C/C++ code into assembly (compilers can optimize, but not as well as humans in all cases).

I saw a question on Stackoverflow today that talked about the getfield opcode in the context of the trim() method in the String class. In the trim() method, you have the following comments:

int off = offset;      /* avoid getfield opcode */
char[] val = value;    /* avoid getfield opcode */

The author of the question wanted to know what these comments mean. This question seemed pretty interesting to me and so I went and did some research. I found out that getfield is an operation that lets you get access to the member variable/field of a class. This operation is fairly expensive as it involves indexing into the runtime constant pool. Performing this operation a few times does not really incur a performance hit. It is when you perform the operation multiple times, that performance becomes an issue. You can see this from the next few lines of code:

while ((st < len) && (val[off + st] <= ' ')) { st++; } while ((st < len) && (val[off + len - 1] <= ' ')) { len--; } [/sourcecode] Now if the author of the trim() method hadn’t assigned offset and value to local variables, a getfield operation would be performed every time the loop-condition is tested. This is obviously inefficient. Therefore, the author assigned the the values of offset and val into the local variables off and val. So now, instead of getfield you have iload (for off anyway), which performs much faster.

Rooting the G2

As some of you may already be aware, it appears that the G2 has some sort of “magic restore” (it’s not a rootkit) function that causes all changes to /system to be reverted. This means that you cannot remove any bundled bloatware. Even more troubling, it looks like the phone will perform the restore while it is running (i.e, you don’t need a reset). I haven’t turned on my G2, so this is what I’ve heard from people at XDA Developers. You can get temporary root on the devices, but after a little while (some people say minutes, others say hours; experiences seem to vary) root is lost. So it is possible that something is performing the restore while the phone is running.

Helpful folks on XDA Developers have posted the datasheet to the eMMC and another kind soul (damnoregonian) was able to get the value of the CSD register (the register that seems to control the behavior of the MMC): d00f00320f5903fffffffdff924040c8.

WP_GRP_SIZE[36:32] and WP_GRP_ENABLE[31:31] seem to be the bits that control the write-protect (per the datasheet). Currently these seem to be set to the default values per the data sheet (11111b and 1b). Clearing the bits should (theoretically) turn off the write protection. The value to do that would be d00f00320f5903fffffffde0124040c8. The CSD node is R/O and so you cannot echo to it directly. The only way to do it would be to write a kernel module/driver that writes to the register. Apparently the kernel exports a function called mmc_send_csd, and so one should be able to write to this register.

I’m tempted to write a kernel module that does just that. But since working at Intel I haven’t written any kernel drivers. Also while I did write drivers at Intel, I pretty much made modifications to what others before me had written, and so I never wrote one from scratch. I’m going to see if I can start on something this weekend… wish I was still in college… I had a lot more time then!

If this works (and that’s a big if), there’s still the issue of restores being done while the phone is in operation. That could cause a lot of inconsistency. So this might be a partial solution.

Either way, I’m sure someone will come up with a way to root the phone. But if there’s nothing by next Friday, I’m going to return the phone.

UPDATE

Someone posted updated specs. It looks like those bits are read-only. Bummer. Also, this from T-Mobile’s website:

Bellevue, Wash. — Oct. 7, 2010

As pioneers in Android-powered mobile devices, T-Mobile and HTC strive to support innovation. The T-Mobile G2 is a powerful and highly customizable Android-powered smartphone, which customers can personalize and make their own, from the look of their home screen to adding their favorite applications and more.

The HTC software implementation on the G2 stores some components in read-only memory as a security measure to prevent key operating system software from becoming corrupted and rendering the device inoperable. There is a small subset of highly technical users who may want to modify and re-engineer their devices at the code level, known as “rooting,” but a side effect of HTC’s security measure is that these modifications are temporary and cannot be saved to permanent memory. As a result the original code is restored.

Well, T-Mobile. How about you provide us technical users a way to root our devices? What you’re doing is stupid. You’re going against everything Android stands for. If I can’t root it, I think I will return my phone and get a Vibrant instead. I would have expected this from Apple or Verizon. But not from you. Very disappointing.

Dr. Pheanis’ Last Class

Today was Dr. Pheanis’ last day as a professor. We were his very last class. He said he would remember us the most because we were his last class. I think it is very fitting that the CSE 521 class would be his last. We are essentially (in his view – I am not just saying this), his best students… the “creme de la creme” as he put it. There are very few students that he invites to take CSE 521. CSE 521 is a hard class and he invites you to take it only if he thinks you are smart enough to take it. I am glad he thought I was smart enough to take it. I like to think that we represent his success as a professor.

ASU is losing an excellent professor… not that anyone in the management cares because they have their own f**king agendas. I really wonder what will become of the embedded systems department once Dr. Pheanis leaves.

I really am glad to have been taught by him. It really is an honour.

Comments, Radix Sort, Adam and Eve

Ah finally! Someone made use of the Comments! I feel so special! Thanks Ed! Yeah and life sucks.

Radix Sort is an amazing algorithm. Too bad my teammate and I failed our first demo because we made the absolutely dumb mistake of not distinguishing between a BYTE and a LONGWORD. Yeah big difference.

Anyway, I thought this was cute:

Adam and Eve had an ideal marriage. He didn’t have to hear about all the men she could have married, and she didn’t have to hear about the way his mother cooked. (Kimberley Broyles)

I don’t want to leave!

I don’t want to leave… It’s back to CSE 421… I can’t believe Thanksgiving is over… I JUST GOT HERE!!! Crap…

Lab 9 Done

Lab 9 is DONE! Woohoo!

Working Code

Yes! My code works! Woohoo!

Intel, Lab 9

Just got a call from Intel today. Found out that I’ve been selected for an interview there. I’ll be doing the interview… and if I get both (!) then I’ll have to decide which one I want. It feels good to know that I’ve been selected…

Lab 9 is pretty interesting! It’s gonna be hella hard… but… interesting!

Lab looks like it works

My lab looks like it works. I think this has been completely unfair. I’ve been working on it for a week… and it’s as if I just started… Sometimes you get it, sometimes you don’t…

All original content on these pages is fingerprinted and certified by Digiprove
%d bloggers like this: