#50 i = ((int)c1<<8) | c2; is rather long

open
nobody
None
5
2003-12-20
2003-12-20
No

the relatively common operation of combining
two unsigned char variables into an integer
produces long code:

unsigned char c1,c2;
unsigned int i;

void main()
{
i = ((int)c1<<8) | c2;
}

;test.c:7: i=((int)c1<<8) | c2;
; genCast
mov r2,_c1
mov r3,#0x00
; genLeftShift
; genLeftShiftLiteral
; genlshTwo
mov ar3,r2
mov r2,#0x00
; genCast
mov r4,_c2
mov r5,#0x00
; genOr
mov a,r4
orl a,r2
mov _i,a
mov a,r5
orl a,r3
mov (_i + 1),a

(likewise if you use '+' instead of '|')

Frieder

Discussion

  • Bernhard Held

    Bernhard Held - 2003-12-20

    Logged In: YES
    user_id=203539

    Still not perfect, but much shorter:

    void foo()
    {
    ((char near *) &i)[0] = c2;
    ((char near *) &i)[1] = c1;
    }

     
  • Frieder Ferlemann

    Logged In: YES
    user_id=589052

    Thanks Bernhard,

    that does it for me:)

    Slightly offtopic: you're using 'near' here,
    is this a recommended alias for 'data'?

     
  • Bernhard Held

    Bernhard Held - 2003-12-20

    Logged In: YES
    user_id=203539

    'near' is exactly the same as 'data'. But it's not in the docs,
    so I can't recommend it ;-)

    I personally use 'near' to document, that it doesn't matter if
    it's a pointer to 'data' or 'idata' space.

    BTW: I've manually update the online docs to finally make
    your work available to all the rest of the world!

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks