Search code examples
assemblymipsmips32

Can someone please explain .equ N, (. - myarray) / 2 to me?


So if you had

myarray:   .half 71, 92, 24, 59, 0x68, 10, 35, 34

The line of code below would assign the number of elements in myarray to symbol N.

.equ N, (. - myarray) / 2

I don't understand how this works. What does the . - part of the code really do?

(On PIC32MX360F256L with MPLAB 1.33 IDE)


Solution

  • You don't specify which assembler you are using, so this could be way off. I'm assuming you have some sort of GNU as-compatible, for MIPS, which supports the .half directive to declare half-words.

    The equ directive sets the symbol N to the value of the expression (. - myarray) / 2.

    In turn, (. - myarray) is the difference between myarray, the beginning of the array you declared, and the address ., the current address that as is assembling into.

    Since myarray is an array of half-words (16-bits wide), dividing by 2 will make N equal to the number of elements in myarray, similar to the C expression: sizeof myarray / sizeof *myarray.