Archive

Archive for the ‘Coding’ Category

Freaky Assembly?

March 9, 2008 Pari 1 comment

After a looong time, I was debugging some embedded C code and thought I found something freaky:

C code

for (i = 0; i < 1000000; i++);

ARM code disassembly (as generated by GNU ARM gcc)

0×0000019c <main+196>: mov r3, #0 ; 0×0
0×000001a0 <main+200>: str r3, [r11, #-16]
0×000001a4 <main+204>: b 0×1b4 <main+220>
0×000001a8 <main+208>: ldr r3, [r11, #-16]
0×000001ac <main+212>: add r3, r3, #1 ; 0×1
0×000001b0 <main+216>: str r3, [r11, #-16]
0×000001b4 <main+220>: ldr r2, [r11, #-16]
0×000001b8 <main+224>: mov r3, #999424 ; 0xf4000
0×000001bc <main+228>: add r3, r3, #572 ; 0×23c
0×000001c0 <main+232>: add r3, r3, #3 ; 0×3

0×000001c4 <main+236>: cmp r2, r3
0×000001c8 <main+240>: bls 0×1a8 <main+208>

The three highlighted lines above in affect initialize r3 with 999999 (first initializes r3 with 999424, then adds 572 to it, then adds 3 to it).

What puzzled me was why couldn’t it do that directly (mov r3, #999999)?

Then after some scratching my head, an almost faint memory of ARM assembly language dawned on me… ARM instructions are 32-bit, of which Operand 2 can be only 12-bits. In addition:

- Of these 12 bits, 8-bits are for data, and 4-bits are used for ROR.
- The ROR bits are in turn multiplied by 2 before being applied on the 8-bits.

The combination of ROR and shifting by 2 greatly extends the range. The assembler automatically does it for you if it sees an operand greater than 8-bits.

This can be a great interview question :-).

Do the math, verify…

999424 + 572 + 3 is the closest tuples you can get to add up to 999999 using the 12-bit ROR with x2 multiplier for the RoR.

Just for verification, here are the instructions from memory:

1b8: 3D39A0E3 ; 0xE3A0393D
1bc: 8F3F83E2 ; 0xE2833F8F
1c0: 033083E2 ; 0xE2833003

To get 999424 (0×0F4000):
0×0000003D ROR 18 (0×9 x 2) = 0×000F4000 (ROR 18 = LSL 6)
As confirmed by the instruction: E3A03 93D

To get 572 (0×023C):
0×0000008F ROR 30 (0xF x 2) = 0×0000023C (ROR 30 = LSL 2)
As confirmed by the instruction: E2833 F8F

To get 3 (0×0003):
0×00000003 ROR 00 (0×0 x 2) = 0×00000003 (ROR 00 = LSL 0)
As confirmed by the instruction: E2833 003

Note: the LSL is just for convenience, it’s good only if data has all zeros padded on the left (at least enough to cover the LSL).

Categories: ARM, Coding

Inject Bug?

June 21, 2007 Pari Leave a comment

Is this a bug??

fruits = %w{apples oranges bananas}

str = “”
fruits.inject { |str, x| str << x }
p str
p fruits

Produces:

“applesorangesbananas” # Correct
["applesorangesbananas", "oranges", "bananas"]

Not only is the first element of the array fruits wrong, the it shouldn’t even get modified! The inject should only be working on str.

To produce the desired result, I had to replace the inject() with inject(“”). Though, still I don’t agree with the behavior – inject should not modify the source array, it should have modified only str.

For example, this code:

n = [1, 2, 3, 4]

sum = 0
n.inject { |sum, i| sum += i }
p sum
p n

Correctly produces:

10
[1, 2, 3, 4]

# Works as should, array n does not get modified.

Categories: Coding

Needle

January 20, 2006 Pari Leave a comment

Dependency injection with Needle.

Sometime last year I had given a little talk on aspect oriented programming, dependency injection, inversion of control. Used the Spring Framework (for Java) to illustrate it. I thought introducing Spring was great for productivity, but it pales in comparison with Needle. Needle for Ruby makes Spring (and as usual Java) look like a beast in terms of productivity, readability, and maintenance costs.

Check it out at: http://needle.rubyforge.org

Categories: Coding

Ruby

January 20, 2001 Pari Leave a comment

Ruby is the newest and fastest growing programming language, invented by Yukihiro Matsumoto (“Matz“), and is catching on like wildfire.

I’ve been coding in Java, Perl, C, C++… and I think Ruby is the biggest leap in the evolution of programming languages since a long time. Suppose you take the most experienced Java developers, ask them what there frustrations with Java are, what can be changed, how it can be improved — you’ll get Ruby.

It has the object-orientedness of Java (and does it the right way), the expressiveness of Perl, and the ease of Python. The best way to describe Ruby is “elegant”, and most importantly is that Ruby complements Agile Software Development. I don’t think anyone in their right minds who has worked on Java/J2EE projects can say the same about that.

Have worked with both Java and Ruby, I can tell you without hesitation that Ruby increasing productivity by at least 40% (and that’s big savings) compared with Java. And have no doubt that it will gain rapid adoption in the IT industry, and wouldn’t be surprised if J2EE becomes the legacy code of the past.

Links

Categories: Coding