News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Symbol format

Started by Pepe, May 19, 2021, 01:15 PM

Previous topic - Next topic

Pepe

Can you assign a variable format to Symbol or is the format created automatically depending on the assignment?

For example

Symbol esc = 1.3
Symbol value = 10 * esc

Value is a constant of type float

How to make the constant value have Word format without having to create a variable in ram like

Dim value as word = 10 * esc

top204

#1
There is no need for formats with constants in the compiler. For example:

Symbol cEsc = 1.3              ' Create a floating point constant value
Symbol cValue = 10 * cEsc      ' cValue will also be a floating point constant because cEsc is a floating point constant
 


Dim wValue As Word = 10 * cEsc  ' Load a newly created word variable with the converted floating point constant value

or

Dim wValue As Word = cValue  ' Load a newly created word variable with the converted floating point constant value

As can be seen, the floating point constant will be automatically converted to an integer if assigned to an integer variable, and the assembler code will be:

clrf wValueH,0
movlw 13
movwf wValue,0


Which loads the word variable "wValue" with the value of 13. i.e. 10 * 1.3

A floating point variable or constant value will automatically be converted into integer when the assignment is an integer variable, and vice-versa.

Pepe

I ask if a constant created with another constant of floating type instead of being floating is created of type word without having to create a variable in ram

RGV250

Hi,
I am struggling to understand what you are trying to do, can you give an example or explain why having "Value" as a float is a problem.

Regards,
Bob

trastikata

Quote from: RGV250 on May 20, 2021, 06:18 PMHi,
I am struggling to understand what you are trying to do, can you give an example or explain why having "Value" as a float is a problem.

He assigns a floating point constant ex: Symbol Pi = 3.14

Now he wants to assign a second constant by using the first constant ex: Symbol IntegerPi = 10 * Pi

The second constant will be treated as floating point (ex:31.4) every time when called, however he wants it be treated as an integer value (ex: 31) ... at least that's what I understood.

RGV250

Hi,
I understand that but the question is I just don't see why it would be a problem in the program.

Bob

trastikata

Quote from: RGV250 on May 20, 2021, 08:37 PMI understand that but the question is I just don't see why it would be a problem in the program.

I can only guess that maybe a misconception of how the compiler deals with variables if some analogy is being drawn between Proton compiler and other PC compilers where variable types are assigned dynamically.

Pepe

I was wondering if a constant can be formatted to fix the problem that doing a shift to multiply by 2 doesn't give the correct value if the constant becomes float
I don't know if the problem is already solved with the latest version of the compiler
I use 3.7.3.6

this is de example whit de result


Device = 18F2550
Declare Xtal = 48
Declare Create_Coff  On

$define freeram          ' buffer video usa ram usb

Symbol esc = 1.001
Symbol esc1 = 1

Symbol plargo = 10 * esc
Symbol plargo1 = 10 * esc1
Dim plargo2 As Word = 10 * esc

Dim pasos As Word
Dim pasos1 As Word
Dim pasos2 As Word
Dim largo As Word
 
Include "SSD1306_Def.inc"
Include "Graphic_Def.inc"
 
Include "SSD1306_Sub.inc"
Include "Graphic_Sub.inc"

SelectFont(Arial_9)

largo = 100
pasos = (largo + plargo) << 1
pasos1 = (largo + plargo1) << 1
pasos2 = (largo + plargo2) << 1

ClearVideoMem
  Print At 0,0,"largo:",Dec pasos
  Print At 0,13,"largo1:",Dec pasos1
  Print At 0,26,"largo2:",Dec pasos2
RefreshDisplay

Do
Loop

Include "Arial_9-12x13.inc"
Include "18F2550_fuses20.inc"
test.jpg

Pepe

doing a shift to divide is also wrong if applied to a constant that is float

joesaliba

Try: -

Symbol esc1 = 1.00

RGV250

Hi,
If you do the addition first I think it would work as planned.
largo = 100 + plargo
pasos = largo << 1   

Bob

Pepe

I understand that you cannot do a shift to a floating format to multiply or divide which must be of type integer
but I keep asking if a constant can be formatted so as not to create more code or more variables in ram

RGV250

Hi,
From the manual, they don't actually use space so they cannot use less.
QuoteSymbols
The Symbol directive provides yet another method for aliasing variables and constants. Symbol  cannot  be  used  to  create  a  variable.  Constants  declared  using  Symbol  do  not  use  any  RAM within the microcontroller and are only accessed at compile time, not while the program is running. 
From the solution I suggested you have already declared the variable so it does not use any more space. 

Bob

Pepe

Thank for the information

Pepe

In the examples below if the constant trim could be formatted from word the program would have fewer bytes and variables

Device = 18F2550
Declare Xtal = 48
Declare Create_Coff  On

$define freeram          ' buffer video usa ram usb

Symbol esc = 1         

Symbol trim = 1000 * esc  ' trim is created as word in compiation time because esc is word
                          ' so program = 3234 bytes and 127 variables

Dim pasos As Word
Dim largo As Word

Include "SSD1306_Def.inc"
Include "Graphic_Def.inc"
 
Include "SSD1306_Sub.inc"
Include "Graphic_Sub.inc"

SelectFont(Arial_9)

largo = 2000
pasos = trim + largo

ClearVideoMem
  Print At 0,0,"largo:",Dec pasos
RefreshDisplay

Do
Loop

Include "Arial_9-12x13.inc"
Include "18F2550_fuses20.inc"

Device = 18F2550
Declare Xtal = 48
Declare Create_Coff  On

$define freeram          ' buffer video usa ram usb

Symbol esc = 1.0         

Symbol trim = 1000 * esc  ' trim is created as float in compiation time because esc is float
                          ' so program = 3756 bytes and 141 variables

Dim pasos As Word
Dim largo As Word

Include "SSD1306_Def.inc"
Include "Graphic_Def.inc"
 
Include "SSD1306_Sub.inc"
Include "Graphic_Sub.inc"

SelectFont(Arial_9)

largo = 2000
pasos = trim + largo

ClearVideoMem
  Print At 0,0,"largo:",Dec pasos
RefreshDisplay

Do
Loop

Include "Arial_9-12x13.inc"
Include "18F2550_fuses20.inc"

Pepe

I do not know if anyone is interested, but my question of being able to format a constant and not solve it automatically is given because as programming in basic for me is more comfortable than in c, I converted the source into c of a labeller that I program with stepper motor to the basic of proton which is excellent (thanks to Less for such development) and the problem I had was that I did not respect the length of the label until I realized that when creating the scale constant for the relation stepper motor and use shift to double the length of the path to label 2 labels per container did not respect the length because the result of the sum of an integer variable (word) was being shifted to a floating constant resulting in a floating value which was incorrect, but in c it did work because the declaration of the adjustment constant could be formatted as an integer (word).
As a suggestion, if a shift is applied to a floating value, it would be good if the compiler gave a warning that a shift was performed to a floating value so that one can modify the program if the operation performed is not correct.

tumbleweed

Les pointed out in post #1 that "the floating point constant will be automatically converted to an integer if assigned to an integer variable"

So, the floating point will go away if you change your second example in post #14 to
pasos = trim    ' assign float const to an integer
pasos = pasos + largo

RGV250

Hi,
I can see the point of a warning but don't see why you need to complicate things by having a format for the constant. If you don't apply any decimal places it will be treated as an integer, if you apply decimal places it will be treated as a float.
1 = Integer
1.0 = float

Regards,
Bob

joesaliba

Quote from: RGV250 on May 22, 2021, 01:31 PMHi,
I can see the point of a warning but don't see why you need to complicate things by having a format for the constant. If you don't apply any decimal places it will be treated as an integer, if you apply decimal places it will be treated as a float.
1 = Integer
1.0 = float

Regards,
Bob

That is what I tried to say in post #9.

top204

#19
A constant created by the Symbol directive will not use any RAM space in the device because it creates symbols within the compiler itsef, at compile time. These are then automatically converted to the value or the alias name when they are used within the program's code.

A floating point value cannot be shifted left or right because floating point is a form of compression to a value and not a binary value itself. So to replicate a "<< 1", simply multiply by 2, and "<< 2" is multiply by 4, and ">> 1" is divide by 2 etc... It is the C and C++ languages that always do the shifting because the original incarnations of the languages did not have "bit" variables, or references to bits within other variables, because the microprocessors that the languages were originally created for did not have bit manipulating assembler mnemonics, so the bits had to be masked and shifted, which wastes time and RAM when used directly with variables. Later incarnations of the languages had "unions" that could imitate bits, but they still used masking and shifts under the hood, and some C and C++ languages for microcontrollers still do!

With C and C++ languages, there is a lot of copy and paste, so programs that were originally written for microprocessors get transfered to microcontrollers, and this wastes a lot of code memory, RAM and time, because most microcontrollers have bit manipulating assembler mnemonics.

To shift a constant value, make sure it is not a floating point type. i.e. Remove a decimal point, then it will operate just like any other integer value and shifts, and other boolean logic operators will work on it.

However, because of the thread, I have added 2 'optional' keywords for the next compiler's upgrade that will make a constant value a floating point or integer, regardless of the values within the Symbol parameter, just to keep users content. :-) These will be:

Symbol MyConst = Integer 1.3  ' Create an integer value of 1.3. i.e. A value of 1

or

Symbol MyConst = Float 1  ' Create a floating point value from 1. i.e. A value of 1.0

or

Symbol MyConstExp = Integer (MyConst + 3.14' Create an integer value from the floating point expression. i.e. A value of 4