Question

I have a variable defined in C on a PIC24

Let's say the name of the variable (in the C file) is The_Number_Of_Bytes

In the PIC24 C code, it is defined like this....

 unsigned long The_Number_Of_Bytes=0;   // number of bytes in buffer

I'm going to be called when an array of bytes named DATABUF01 will contain The_Number_Of_Bytes bytes (sorry for the implied redundancy) and I will need to make sure that I do that many bytes, exactly, and then stop.

I'm pretty confident that the number will be less than 65535, so why it is an unsigned long is for other discussions. For now, I want to know; which is the high order word and which is the low order word ? (For that matter, is long a 32 bit number in PIC24 C ?)

Could somebody tell me What will be in W2 and W3 in this example ?

    Mov.W   #_The_Number_Of_Bytes, W1       ;From the dispatcher
    Mov.W   [W1++], W2                      ;My question: Hi Order or Low Order ?
    Mov.W   [W1],   W3                      ;My question: Hi Order or Low ?

    Mov.W   #_DATABUF01, W4                 ;The Start

    Mov.B   [W4++], W5                      ;First byte...

    :                                       ;Whatever
    :                                       ;Whatever
    :                                       ;Whatever

Could someone please confirm or correct my thinking about Hi / Low order of the unsigned long int ?

EDIT commenter requested this. May answer the question (comments are mine)

Requested comment, The size appears to be 4 bytes, from this...

  45:                 unsigned long i=0;    
    0AB6A  B80060     mul.uu 0x0000,#0,0x0000    ;make a zero
    0AB6C  980710     mov.w 0x0000,[0x001c+2]    ;no clue which word
    0AB6E  980721     mov.w 0x0002,[0x001c+4]    ;aha, if I made it 1 or 2, I'd know
  46:                 unsigned int Fischer;
  47:                 
  48:                 Fischer = sizeof(i);
      0AB70  200040     mov.w #0x4,0x0000         ;So it must be 4
      0AB72  780F00     mov.w 0x0000,[0x001c]     ;okay, it's a 4 byte number
  49:                 
Était-ce utile?

La solution

Here's a snippet from MPLAB C Compiler for PIC24 MCU's User's Guide:

5.3 DATA REPRESENTATION
Multibyte quantities are stored in “little endian” format, which means:

  • The least significant byte is stored at the lowest address
  • The least significant bit is stored at the lowest-numbered bit position

As an example, the long value of 0x12345678 is stored at address 0x100 as follows:
0x1000x1010x1020x103
 0x78  0x56  0x34  0x12 

As another example, the long value of 0x12345678 is stored in registers w4 and w5:
   w4       w5   
0x56780x1234

Autres conseils

you can also view the assembly for a program to try to get an idea of how their compiler does it... for instance:

int main(int argc, char** argv)
{
  long i = 56;
  long j = i;
  return 0;
}

becomes... ( xc16-gcc -S main.c )

    .file "/Users/grady/MPLABXProjects/testpic24.X/main.c"
    .section    .text,code
    .align  2
    .global _main   ; export
    .type   _main,@function
_main:
    .set ___PA___,1
    lnk #12
    mov w0,[w14+8]
    mov w1,[w14+10]
    mov #56,w4
    mov #0,w5
    mov.d   w4,[w14]
    mov.d   [w14],w4
    mov w4,[w14+4]
    mov w5,[w14+6]
    clr w4
    mov w4,w0
    ulnk    
    return  
    .set ___PA___,0

    .section __c30_signature, info, data
    .word 0x0001
    .word 0x0000
    .word 0x0000

; MCHP configuration words

    .set ___PA___,0
    .end
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top