PDA

View Full Version : How much code space do PBP statements use.



Darrel Taylor
- 18th September 2005, 23:27
How much code would a code hog hog, if a code hog could hog code?

Answer: All available Code Space.

But sometimes, it's not enough.   "Just a few more words, that's all I need"

So, how do you find out which parts of a program use the most code space?
Here's one way.
NOTE: This has been improved for MPASM users. See post#4

<font color="#008000"><b>ASM
</b></font><font color="#0000FF"><b><i>;----------------------
</i></b></font><font color="#000080">StartSize macro
CodeStartAddress = $
endm

</font><font color="#0000FF"><b><i>;----------------------
</i></b></font><font color="#000080">EndSize macro Name
Name = $ - CodeStartAddress
endm
</font><font color="#008000"><b>ENDASM</b></font>These 2 macros do NOT use any code space. They merely create "Symbols" in the assembler that indicate the size of the measured Code.

To measure the size of any block of code, simply place an @ StartSize statement just before the code you want to measure. Then place a @ EndSize Name after the block of code and replace Name with something that you can easily remember.
<font color="#000080">@ StartSize
</font><font color="#008000"><b>LCDOUT </b></font><b>$FE</b>,<b>1</b>,<font color="#FF0000">&quot;Hello World!&quot;
</font><font color="#000080">@ EndSize SizeOF_HelloWorld</font>This will create an entry in the Symbol Table called SizeOF_HelloWorld, and in this case it will be 0000002A, or 42 words.

The Symbol table can be found at the bottom of the .lst file if you are using MPASM. Or, in the .SYM file if using PM

There are 2 parts to how much space is used by PBP statements. The first part is the code added to the PBP system for any given Command. and, the second part is the code used by the statements themselves. &nbsp; For instance, the first time you use an LCDOUT statement, PBP will add approximately 107 words of code to the system, plus the 42 words for the statement itself, for a total of 149 words. If the exact same statement is used a second time, it will only add another 42 words.

This approach will only measure the code size used by the statements themselves, not the code added to the system. The system code size can be determined by commenting out the statements being measured, and seeing how much difference there is in the total code size. Then subtract the previously measured size of the statements. What's left indicates how much system code was added.<hr>

Let's say for instance that you were using a SELECT CASE structure, and you wanted to see if using a LOOKUP would save any space.


<font color="#000000"><b>row </b><font color="#008000"><b>VAR BYTE
</b></font><b>temp </b><font color="#008000"><b>VAR BYTE

</b></font><font color="#000080">@ StartSize
</font><font color="#008000"><b>SELECT CASE </b></font><b>row
</b><font color="#008000"><b>CASE </b></font><b>1 </b>: <b>temp </b>= <b>$80
</b><font color="#008000"><b>CASE </b></font><b>2 </b>: <b>temp </b>= <b>$C0
</b><font color="#008000"><b>CASE </b></font><b>3 </b>: <b>temp </b>= <b>$90
</b><font color="#008000"><b>CASE </b></font><b>4 </b>: <b>temp </b>= <b>$D0
</b><font color="#008000"><b>END SELECT
</b></font><font color="#000080">@ EndSize sizeOF_CASE

@ StartSize
</font><font color="#008000"><b>LOOKUP </b></font><b>row</b>,[<b>$80</b>,<b>$80</b>,<b>$C0</b>,<b>$90</b>,<b>$D0</b>],<b>temp
</b><font color="#000080">@ EndSize sizeOF_Lookup</font>The symbol table will now contain these 2 entries<pre>sizeOF_CASE 0000002D<br>sizeOF_Lookup 00000016
</pre>Now it's easy to see that the SELECT CASE took 45 words, and the LOOKUP only used 22.<hr>

To find out if any system code was added, write down the total words used by the program, in the above case it's 217. &nbsp;Then comment out the section in question and compile it again.
<font color="#000000"><b>row </b><font color="#008000"><b>VAR BYTE
</b></font><b>temp </b><font color="#008000"><b>VAR BYTE

</b></font><font color="#000080">@ StartSize
</font><font color="#0000FF"><b><i>' SELECT CASE row
' CASE 1 : temp = $80
' CASE 2 : temp = $C0
' CASE 3 : temp = $90
' CASE 4 : temp = $D0
' END SELECT
</i></b></font><font color="#000080">@ EndSize sizeOF_CASE

@ StartSize
</font><font color="#008000"><b>LOOKUP </b></font><b>row</b>,[<b>$80</b>,<b>$80</b>,<b>$C0</b>,<b>$90</b>,<b>$D0</b>],<b>temp
</b><font color="#000080">@ EndSize sizeOF_Lookup</font>After this compile, the total words used is 172. So the difference is 45 words. Since we've already determined that the statements themselves used 45 words, we now know that nothing was added to the PBP system.<hr>

Now, get out there and put the "Squeeze" on some code.
<br>

mister_e
- 18th September 2005, 23:56
Nice catch Darrel. Will be really handy for me ;)

crematory
- 30th September 2005, 18:55
Hey Darrel

Its really nice one :)

If mecanique saw this code, it will be easy to add those nice green bars as in Proton IDE... Any way, may be they haven't seen code yet ;)

Darrel Taylor
- 21st January 2009, 09:43
Version 2.0

This include file adds several more abilities to the idea in post#1. &nbsp; MPASM Required

Along with measuring the size of a block of code, it can provide information about the size of the PBP Library, User code size (excluding the Library), Total code size, and various individual blocks of code, and those blocks can now be Nested. And best of all, it displays the result in the MicroCode Studio window, instead of having to dig thru the .LST file.


Using a similar example to post#1 ...
It looks something like this ...

<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=3134" /> <!-- 3134 -->

From that you can see that the LCDOUT command used 42 WORDs.

But again, there are 2 parts to code used with PBP. The other part is the Library.
By adding a LibrarySize command, you can see how much library code the LCDOUT command used.
The UserSize command shows the total amount of User Code used (everything but the Library).
And TotalSize is the size of everything (including the Library).

<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=3135" /> <!-- 3135 -->

The results can be turned On/Off by either commenting the DEFINE Measure 1 line, or changing its value to 0. When it's turned off, no messages will be displayed.

StartSize and EndSize are given Unique names and both MUST match exactly (case sensitive).
Start/End segments can be "Nested" or they can "Overlap", doesn't really matter.


INCLUDE &quot;CodeSize.pbp&quot;
DEFINE Measure 1

row VAR BYTE
temp VAR BYTE

@ StartSize(Select)
SELECT CASE row
@ StartSize(Case1)
CASE 1 : temp = $80
@ EndSize(Case1)
CASE 2 : temp = $C0
CASE 3 : temp = $90
CASE 4 : temp = $D0
END SELECT
@ EndSize(Select)

@ StartSize(Lookup)
LOOKUP row,[$80,$80,$C0,$90,$D0],temp
@ EndSize(Lookup)

@ LibrarySize

Which gives these results ...

<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=3136" /> <!-- 3136 -->

Neither the SELECT CASE or LOOKUP statements added any code to the Library, so it shows 1 WORD (goto INIT).

When using either UserSize or TotalSize, they MUST be the LAST lines in the program.

It will work with any 18F or 14-bit chip.
With 18F's it displays results in both BYTEs and WORDs, and the result may differ from the size reported by MPASM because MPASM includes Config words in the total, and doesn't count locations that were skipped with ORG statements. This program shows the Real size.

These routines DO NOT use any code space (flash) or variable space (RAM). So you can safely include it in your program and simply turn it ON/OFF whenever desired.

Download the file below and un-zip it to your PBP folder (the one with PBPW.EXE).
There is only 1 file in the .zip "CodeSize.pbp".

Luckyborg
- 13th February 2009, 17:24
Great piece of code, I just tried it out and got a slightly different result than expected based on your screen shots. After compiling my Results box showed the results for both words and bytes.

http://www.matko.com/images/tmp/window.jpg

I figured you upgraded the program since you took the shots and actually considered modifying your program to allow a "DEFINE Measure 2" to allow just the words to be displayed. While poking around your file I realized your code already allows for both types of display based on if BSR was defined. I'm not clear on what this is or why my code meets the requirements. Could you explain it to me. I could easily just change it to print what I want, but wanted to understand it more.

I'm getting ready for a major code overhaul using your interrupts, so I'm sure you'll be hearing from me again. (By getting ready I mean doing everything I can think of short of actually starting to code including spending most of the last 3-4 days reading every thread on this list I can find relating to my needs.) :)

Thanks, David

mister_e
- 13th February 2009, 21:31
It's because you're using a 18F and compilation method/results may differ a little bit Between 16F and 18F as they don't have the same Library, not the same instruction set/core too + something that I missed :D