使用:
1、把解開后的size.exe復(fù)制到COSMIC\CXSTM8文件夾,就是你的COSMIC編譯工具里有cpstm8.exe的那個文件夾。
2、在SVTD的Project->settings中打開Post-Build選項卡,在commands的最后一行加入:size $(OutputPath)$(TargetSName).map
3、保存返回,編譯試試,最后會出現(xiàn)如:Program Size: Code=12470 Eeprom=0 ZeroPage=227 Ram=180 的結(jié)果。
思路:
打開map文件,統(tǒng)計各段的長度,然后輸出。
主要代碼:
static void Main(string[] args)
{
StreamReader sr = new StreamReader(args[0]);
string[] s;
char[] charSeparators = new char[] { ' ' };
int code_size = 0;
int zeropage_size = 0;
int ram_size = 0;
int eeprom_size = 0;
for (int i = 0; i < 8; i++)
{
string s1 = sr.ReadLine();
}
while (true)
{
string s1 = sr.ReadLine();
s = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
if(s.Length == 0)
{
break;
}
else if (s[7] == ".text" || s[7] == ".const" || s[7] == ".init")
{
code_size += int.Parse(s[5]);
}
else if (s[7] == ".eeprom")
{
eeprom_size += int.Parse(s[5]);
}
else if (s[7] == ".bsct," && s[8] == "initialized")
{
zeropage_size += int.Parse(s[5]);
}
else if (s[7] == ".ubsct" || s[7] == ".share")
{
zeropage_size += int.Parse(s[5]);
}
else if (s[7] == ".data," && s[8] == "initialized")
{
ram_size += int.Parse(s[5]);
}
else if (s[7] == ".bss")
{
ram_size += int.Parse(s[5]);
}
}
Console.WriteLine(" ");
// Program Size: Code=5056 RO-data=288 RW-data=68 ZI-data=1140
Console.WriteLine("Program Size: Code=" + code_size.ToString() + " Eeprom=" + eeprom_size.ToString() + " ZeroPage=" + zeropage_size.ToString() + " Ram=" + ram_size.ToString());
}