How to alias Float variable to Byte array members?

Started by trastikata, Mar 13, 2022, 03:22 PM

Previous topic - Next topic

trastikata

Hi,

I have a large byte array, which is used in one of the Subs in my program, the rest of the time I am not using it and I'd like to repurpose the byte array members to hold some other variables, including larger than Byte variables to save RAM.

Is this the correct way?

Dim MemoryBuffer[256] As Byte

Dim fMyFloat1 As Float At MemoryBuffer#0
Dim fMyFloat2 As Float At MemoryBuffer#4

I am asking because there seems to be a bug in some of the FP assembler subs where a variable is being called but not previously declared.

P.s. The  Compiler  Version is 4.0.1.5

top204

I will need more details on the program that is causing the assembler error, because I cannot re-create it here. All system variables are created, and the code compiles and works correctly in all the floating point subtractions I have tried (Note. A floating point subtraction is actually an addition of a negative value).

For example, I tried the program below:

Declare Reminders = Off
    Device = 18F25K20
    Declare Xtal = 16
'
' Setup USART1
'   
    Declare Hserial_Baud = 9600
    Declare HRSOut1_Pin = PORTC.6
'
' Create some variables
'  
    Dim MyByteArray[20] As Byte
   
    Dim FloatIn1 As Float At MyByteArray#0  
    Dim FloatIn2 As Float At MyByteArray#4       
    Dim FloatOut As Float At MyByteArray#8
   
'--------------------------------------------------------------------
Main:   
    FloatIn1 = 1234.1234
    FloatIn2 = 0.1
    
    FloatOut = FloatIn1 - FloatIn2
    HRSOutLn Dec FloatOut

What can cause it, is if you are using a third party library file that calls the floating point library subroutine, but has not created the correct system variables for it. Or a floating point function of the compiler is not creating the "_BEXP" system variable.

The _BEXP system variable can be created in the program itself by:

Dim _BEXP As Byte System

The above listing also shows the creation of variables from the Byte array, and the Assembler listing (Press the F2 button) shows they are in the correct RAM locations:

MyByteArray equ 0x22
variable MyByteArray#0=0x22,MyByteArray#1=0x23,MyByteArray#2=0x24,MyByteArray#3=0x25
variable MyByteArray#4=0x26,MyByteArray#5=0x27,MyByteArray#6=0x28,MyByteArray#7=0x29
variable MyByteArray#8=0x2A,MyByteArray#9=0x2B,MyByteArray#10=0x2C,MyByteArray#11=0x2D
variable MyByteArray#12=0x2E,MyByteArray#13=0x2F,MyByteArray#14=0x30,MyByteArray#15=0x31
variable MyByteArray#16=0x32,MyByteArray#17=0x33,MyByteArray#18=0x34,MyByteArray#19=0x35
; ADDRESSED VARIABLES
FloatIn1 equ 0x22
FloatIn1H equ 0x23
FloatIn1HH equ 0x24
FloatIn1HHH equ 0x25
FloatIn2 equ 0x26
FloatIn2H equ 0x27
FloatIn2HH equ 0x28
FloatIn2HHH equ 0x29
FloatOut equ 0x2A
FloatOutH equ 0x2B
FloatOutHH equ 0x2C
FloatOutHHH equ 0x2D

trastikata

#2
Les, thank you for your answer.

After some digging, I narrowed the problem and has nothing to do with the aliasing.

Looks like that if there is only subtraction between FP variables,  then the system variable is not being created. However if there had been another operation with FP variables before that, then the system variable is being created. Two simple programs - the first program doesn't work, and the second works.

This doesn't work:
Device = 18F14K50
Declare Xtal = 4

Dim MemoryBuffer[256] As Byte
Dim fMyFloat1 As Float At MemoryBuffer#0
Dim fMyFloat2 As Float At MemoryBuffer#4
Dim fMyFloat3 As Float At MemoryBuffer#8

'fMyFloat3 = fMyFloat2 + fMyFloat1   
fMyFloat3 = fMyFloat2 - fMyFloat1   

This works:
Device = 18F14K50
Declare Xtal = 4

Dim MemoryBuffer[256] As Byte
Dim fMyFloat1 As Float At MemoryBuffer#0
Dim fMyFloat2 As Float At MemoryBuffer#4
Dim fMyFloat3 As Float At MemoryBuffer#8

fMyFloat3 = fMyFloat2 + fMyFloat1   
fMyFloat3 = fMyFloat2 - fMyFloat1


top204

Many thanks for the code snippets. You are correct, the system variable is not being created for subtraction alone. It was the "HRSOutLn Dec FloatOut" line in my test programs that created the "_BEXP" system variable, because it uses additions and subtractions, as well as other routines, to convert the value to ASCII.

It's crazy the very subtle things that can cause anomalies to occur. The correction will be a single line of C code that creates the system variable: "Create_SystemVariable("_BEXP");", in the floating point subtraction functions of the compiler. All my past tests will always have had some form of display to test the calculation results, so I never noticed it, and neither has anyone else, and it has been there since the creation of floating point within the compilers, about 17 years ago!!!!