This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Efficiently splitting a long int into bytes

Hi All,

This is related to my previous post, "Efficiently combining bytes into a long int".

I'd like to find an efficient way to do this. My current implementation (below) results in (unnecessary) calls to the library function for shifting a long int.

sendLong(unsigned long out_data);
{
    sendByte((unsigned char)(out_data >> 24));
    sendByte((unsigned char)(out_data >> 16));
    sendByte((unsigned char)(out_data >> 8));
    sendByte((unsigned char)(out_data));
}

I have tried to use unions (which worked well for my previous problem), but I can't seem to figure out the syntax for putting the argument into the "long" of the union, and sending the "bytes" of the union.

I tried creating a temporary union and copying the argument (out_data) to it:

union   LONGBYTES
{
    uchar b[4];
    ulong l;
};

void sendLong(unsigned long out_data)
{
    union LONGBYTES temp;
    temp.l = out_data;
    sendByte(temp.b[0]);
    sendByte(temp.b[1]);
    sendByte(temp.b[2]);
    sendByte(temp.b[3]);
}

And that produced this code:

             ; FUNCTION _sendLong (BEGIN)
                                           ; SOURCE LINE # 349
;---- Variable 'out_data' assigned to Register 'R4/R5/R6/R7' ----
                                           ; SOURCE LINE # 350
                                           ; SOURCE LINE # 352
                 R     MOV     temp+03H,R7
                 R     MOV     temp+02H,R6
                 R     MOV     temp+01H,R5
                 R     MOV     temp,R4
                                           ; SOURCE LINE # 353
                 R     MOV     R7,temp
                 E     CALL    _sendByte
                                           ; SOURCE LINE # 354
                 R     MOV     R7,temp+01H
                 E     CALL    _sendByte
                                           ; SOURCE LINE # 355
                 R     MOV     R7,temp+02H
                 E     CALL    _sendByte
                                           ; SOURCE LINE # 356
                 R     MOV     R7,temp+03H
                 E     CALL    _sendByte
             ; FUNCTION _sendLong (END)

But this results in an unnecessary allocation of 4 bytes for the temporary union and unnecessary copying of the data from registers (register parameter passing) into the ram allocated for the union and back into registers to pass parameters to sendByte() function. Any ideas?

Thanks,
Bob

0