Prev: JWasm v1.9
Next: prova
From: shane on
Hi all
Apologies for the multi post

I've been given the task of reading some assembly code compiled by gcc for the
ARM architecture.  I've managed to find most of what I need from google, but I
don't understand the following lines

        ldr     r2, [fp, #-36]
        ldr     r3, [fp, #-40]
        add     r2, r2, r3
        mov     r3, r2, lsr #31
        add     r3, r3, r2
        mov     r3, r3, asr #1
        mov     r0, r3

Clearly lines one and two are about loading (32 bit) values from the frame. Line
three adds the two values and the sum is held in r2.

Then that value is logically shifted right 31 bits to the right, making the MSB
now the LSB.  The result of that being stored in r3.

R3 += R2 best explains line 5.

Then another move, arithmetic shift right by 1 bit

I dont understand the logic behind the moves.  Take the MSB add it to R2 as the
LSB, then move R2 right 1 bit (keeping the sign this time)?

Could someone simplify it for me?

TIA
--
Hardware n: Parts of the computer you can kick
From: Harold Aptroot on
"shane" <shane(a)weasel.is-a-geek.net> wrote in message
news:g6odlp$7vb$1(a)lust.ihug.co.nz...
> Hi all
> Apologies for the multi post
>
> I've been given the task of reading some assembly code compiled by gcc for
> the
> ARM architecture. I've managed to find most of what I need from google,
> but I
> don't understand the following lines
>
> ldr r2, [fp, #-36]
> ldr r3, [fp, #-40]
> add r2, r2, r3
> mov r3, r2, lsr #31
> add r3, r3, r2
> mov r3, r3, asr #1
> mov r0, r3
>
> Clearly lines one and two are about loading (32 bit) values from the
> frame. Line
> three adds the two values and the sum is held in r2.
>
> Then that value is logically shifted right 31 bits to the right, making
> the MSB
> now the LSB. The result of that being stored in r3.
>
> R3 += R2 best explains line 5.
>
> Then another move, arithmetic shift right by 1 bit
>
> I dont understand the logic behind the moves. Take the MSB add it to R2 as
> the
> LSB, then move R2 right 1 bit (keeping the sign this time)?
>
> Could someone simplify it for me?
>
> TIA

Now I'm not an ARM expert, but I think:
Ok you get the loading, after that it adds r3 to r2, what it does then
however is more interesting
It first stores the 31th bit of r2 in r3, adds r2 to it and shifts it right
1 bit while preserving the sign

speudo C:

int a = [fp, #-36]; //proper 32bit int, not a 16bit fake int that should be
called short
int b = [fp, #-40];
int c = a + b;
if (a < 0)
c++;
c /= 2;

Which has no special meaning to me, but that could be because I haven't had
my coffee yet

From: Herbert Kleebauer on
Harold Aptroot wrote:
> "shane" <shane(a)weasel.is-a-geek.net> wrote in message

> > don't understand the following lines
> >
> > ldr r2, [fp, #-36]
> > ldr r3, [fp, #-40]
> > add r2, r2, r3
> > mov r3, r2, lsr #31
> > add r3, r3, r2
> > mov r3, r3, asr #1
> > mov r0, r3

> > Could someone simplify it for me?
> speudo C:
>
> int a = [fp, #-36]; //proper 32bit int, not a 16bit fake int that should be
> called short
> int b = [fp, #-40];
> int c = a + b;
> if (a < 0)
> c++;
> c /= 2;
>
> Which has no special meaning to me, but that could be because I haven't had
> my coffee yet

How about c=(a+b)/2 correctly rounded to zero? So, if you know that
a and b are positive, better use "unsigned int" which is faster
because no rounding correction is necessary.
From: Phil Carmody on
Herbert Kleebauer <klee(a)unibwm.de> writes:
> Harold Aptroot wrote:
>> "shane" <shane(a)weasel.is-a-geek.net> wrote in message
>
>> > don't understand the following lines
>> >
>> > ldr r2, [fp, #-36]
>> > ldr r3, [fp, #-40]
>> > add r2, r2, r3
>> > mov r3, r2, lsr #31
>> > add r3, r3, r2
>> > mov r3, r3, asr #1
>> > mov r0, r3
>
>> > Could someone simplify it for me?
>> speudo C:
>>
>> int a = [fp, #-36]; //proper 32bit int, not a 16bit fake int that should be
>> called short
>> int b = [fp, #-40];
>> int c = a + b;

There's no 3rd register used, I'd blat a here

>> if (a < 0)

The bit tested is that of r2, which I'm calling a, what
you're calling c.

>> c++;
>> c /= 2;


Literally, I think the code is:

int a = fp[-36];
int b = fp[-40];
a += b;
b = (a<0);
b += a;
b /= 2;
return b;

Which in C is more idiomatically:

int a = fp[-36];
int b = fp[-40];
a += b;
if(a<0)
a += b; // changed r�le of a,b
a /= 2;
return a;


>> Which has no special meaning to me, but that could be because I haven't had
>> my coffee yet
>
> How about c=(a+b)/2 correctly rounded to zero? So, if you know that
> a and b are positive, better use "unsigned int" which is faster
> because no rounding correction is necessary.

I agree with that interpretation, but as I mentioned, the pseudo-code
given previously deviates from both our points of view.

Phil
--
Dear aunt, let's set so double the killer delete select all.
-- Microsoft voice recognition live demonstration
From: shane on
shane did scribble:

> Hi all
> Apologies for the multi post
>
> I've been given the task of reading some assembly code compiled by gcc for the
> ARM architecture.  I've managed to find most of what I need from google, but I
> don't understand the following lines
>
> ldr     r2, [fp, #-36]
> ldr     r3, [fp, #-40]
> add     r2, r2, r3
> mov     r3, r2, lsr #31
> add     r3, r3, r2
> mov     r3, r3, asr #1
> mov     r0, r3
>
> Clearly lines one and two are about loading (32 bit) values from the frame.
> Line three adds the two values and the sum is held in r2.
>
> Then that value is logically shifted right 31 bits to the right, making the
> MSB now the LSB.  The result of that being stored in r3.
>
> R3 += R2 best explains line 5.
>
> Then another move, arithmetic shift right by 1 bit
>
> I dont understand the logic behind the moves.  Take the MSB add it to R2 as
> the LSB, then move R2 right 1 bit (keeping the sign this time)?
>
> Could someone simplify it for me?
>
> TIA


Thanks all for the replies. I have since been told that the 3 lines relate to
signed division by 2

--
Hardware n: Parts of the computer you can kick
 | 
Pages: 1
Prev: JWasm v1.9
Next: prova