Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 20:48 21 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : static const

Author Message
GerryL
Regular Member

Joined: 24/01/2019
Location: Australia
Posts: 41
Posted: 04:33am 28 Dec 2022
Copy link to clipboard 
Print this post

I was playing around on my CMM2 with the static const examples in Peter Mather's post CFunction which is based on Nathans Post and I noticed that in Peter's function p_string that the variable libAdd was equating to zero meaning that the pointer testData added no additional offset to using s1 or s2 address directly.   I re-wrote the CFunction as below and this ran correctly on my CMM2 and H7, which puzzled me, as how does the compiler know the absolute address of the static const strings as it has no idea where this CFunction will be loaded in memory.
#include "ARMCFunctions.h"
//#include "ARMH7CFunctions.h"

MMFLOAT doubleit(MMFLOAT *c){
return *c *2.0;
}

void p_int(int a,int base){
  char b[64];
  IntToStr(b,a,base);
  MMPrintString(b);
}
void p_float(float a,int m, int n, char c){
  char b[14];
  FloatToStr(b,a, m, n ,c);
  MMPrintString(b);
}

void main(long long int *c, MMFLOAT *b){
  static const char crlf[]="\r\n";
  static const char s1[]="First variable = ";
  static const char s2[]="Second variable = ";
  MMPrintString((char *)((void *)s1)); p_int(*c,10); MMPrintString((char *)((void *)crlf));
  MMPrintString((char *)((void *)s2)); p_float(doubleit(b),4,2,' '); MMPrintString((char *)((void *)crlf));
}


The MMBasic program is,
a%=7
b!=4.0
test1 a%,b!
end

CSUB Test1
   0000002C
   B083B480 6078AF00 ED93687B EE377B00 EEB07B07 370C0B47 F85D46BD 47707B04
   B095B590 6078AF02 4B0B6039 461C681B 461A687B 73E2EA4F F1076839 91000008
   4B0647A0 461A681B 0308F107 47904618 374CBF00 BD9046BD 080002C4 080002C0
   B089B590 ED87AF00 60B80A03 46136079 4B0D70FB 461C681B 7A03EDD7 7AE7EEB7
   F10778FB 687A0010 EEB068B9 47A00B47 681B4B06 F107461A 46180310 BF004790
   46BD3724 BF00BD90 08000310 080002C0  
    'main
   B082B580 6078AF00 4B1B6039 461A681B 447B4B1A 47904618 E9D3687B 46132300
   4618210A FFA4F7FF 681B4B13 4B14461A 4618447B 4B104790 461A681B 447B4B11
   47904618 F7FF6838 EEB0FF83 EEF77B40 22207BC7 20042102 0A67EEB0 FFA8F7FF
   681B4B05 4B08461A 4618447B BF004790 46BD3708 BF00BD80 080002C0 00000076
   0000006C 00000062 00000034 73726946 61762074 62616972 3D20656C 00000020
   00000A0D 6F636553 7620646E 61697261 20656C62 D300203D
END CSUB

The static const data is placed in the .rodata area (read only) of the ELF file by the compiler and its normal practice that the .rodata is appended to the .text stuff (the code) on loading the program.  With CFunctions the .rodata is extracted and placed at the end of the .text data so all good.  Looking at the assembly code of this CFunction the compiler creates the code to calculate the offset from where the static const address is loaded to be parsed to MMPrintString to where it resides in the appended .rodata.  All the information to do this is available at compile time.  In addition, it includes code to add the program counter reg (pc) to this value so it becomes the absolute address at run time.  Pretty nifty by the compiler and hopefully should make using static const on these ARM platforms simpler.

Gerry
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4147
Posted: 10:19am 28 Dec 2022
Copy link to clipboard 
Print this post

edit: deleted
Edited 2022-12-28 20:20 by JohnS
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025