Implementing DECFSZ or INCFSZ

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Implementing DECFSZ or INCFSZ

Post by xor » Mon Nov 06, 2006 4:46 am

I would love to see a compiler with a format that translates into using DECFSZ or INCFSZ. I see the next best thing TSTFSZ using the following:

Code: Select all

 Dim cnt As Byte

 cnt = 8                                
 Repeat                                   
    Dec(cnt)
 Until cnt = 0
I like that WREG in SFBasic is very usuable and handled properly in assembler. I was having fun until I ran out of room with the RAM limit rather quickly in a relatively small program that used an 11 character string, a single long integer, and a couple bytes... :(

TimB
Posts: 262
Joined: Wed Oct 04, 2006 7:25 am
Location: London UK

Post by TimB » Mon Nov 06, 2006 8:21 am

To give you some idea of what goes on underneath.

SF allocates 26 system variables for starters even if you just compile an empty prog. The reason for this is so interrupts can be handled with the most flexibility. They started as being allocated as per usage but after a lot of thought decided on pre declaring the lot then they knew exactly where they stood when it came to saving them in when you do a SAVE(0)

An undefined string is 20 odd byes long if you do not define the size in advance.

If you post the code perhaps David can comment of the reason for the rest.

BTW Dec(cnt) and until are 2 separate commands I would not be happy with a compiler that messed around with my code interpreting stuff like that into one command.

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Mon Nov 06, 2006 1:29 pm

I was having fun until I ran out of room with the RAM limit rather quickly in a relatively small program that used an 11 character string, a single long integer, and a couple bytes...
Perhaps you could post or email the code that is causing the problem. At least then I could give an explanation as to why this might be happening and hopefully suggest some workarounds...

xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Mon Nov 06, 2006 2:24 pm

Let me suggest something like the following. The last line might be interpreted as DECFSZ. Right now using dec(cnt) in that line produces an error:

Code: Select all

cnt = 8
REPEAT
   inc(LATB)
UNTIL dec(cnt) = 0
In mB I can compile a statement like this. It decrements and then does the test, except missing a great optimization opportunity:

Code: Select all

cnt = 8
DO
  inc(LATB)
LOOP UNTIL dec(cnt) = 0

TimB
Posts: 262
Joined: Wed Oct 04, 2006 7:25 am
Location: London UK

Post by TimB » Mon Nov 06, 2006 7:33 pm

If your trying to get really small then try this

Public Inline Sub DecUntilZero(pval As Byte)
ASM
DECFSZ pval
End ASM
End Sub

Dim cnt As Byte

cnt = 8
Repeat
Inc(LATB)
DecUntilZero(cnt)
Until 1 = 2

xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Mon Nov 06, 2006 9:16 pm

This also appears to work:

Code: Select all

cnt = 8
Repeat
   Inc(LATB)
   ASM
      decfsz   cnt, 1, 0
   End ASM
Until 1=2
There must be a reason but I find the assembler variable naming conventions hard to track relative to the source code. Part of my education in a compilers ability is to examine the compiled result. So far very good....except these are killing me.....this is what "cnt" looks like in the assembler:

Code: Select all

M0_U08 EQU 25

TimB
Posts: 262
Joined: Wed Oct 04, 2006 7:25 am
Location: London UK

Post by TimB » Mon Nov 06, 2006 9:39 pm

While this will not go down well with the establishment I have to agree. It does my head in!

xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Mon Nov 06, 2006 10:46 pm

David Barker wrote:Perhaps you could post or email the code that is causing the problem. At least then I could give an explanation as to why this might be happening and hopefully suggest some workarounds...
It was my mistake. I got confused with the array and string dimensioning. The is what I did that caused the problem:

Code: Select all

dim target(11) as string
TimB had mentioned that an undimensioned string is assigned 20 characters. I'm assuming I was dimensioning 11x20. Doing the following kept me under the RAM limitation:

Code: Select all

dim target as string(11)

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Tue Nov 07, 2006 9:46 am

There must be a reason but I find the assembler variable naming conventions hard to track relative to the source code...except these are killing me.....
I can understand these being difficult at first. Obviously, some means of 'name mangling' is required, as Swordfish is highly scoped and the same names can appear in different places. For example, 'Index' as a local variable, parameter or at the module level. It's made slightly more difficult in that modules can share the same function names. For example, USART.Write and LCD.Write. In addition, subs and functions can be overloaded. The Swordfish approach is just one of a few possibilities, designed primarily to support debugging in the future. However, here is how the variable in question decodes...

M0_U08

'M' - module level variable. You may also see 'F' for frame, for example a local var
'0' - virtual address in the module or frame allocation block.
'U' - unsigned. You may also see 'S' (signed) or 'F' (floating point)
'08' - bit size (1 byte)

You also have unique labels, which identify the actual high level source (these will also be used later for debugging support). For example,

Code: Select all

I0_F1_000005 ; L#MK CNT = 8
        MOVLW 8
        MOVWF M0_U08,0
here you can see that CNT = 8 breaks down to

Code: Select all

       
    MOVLW 8
    MOVWF M0_U08,0
It was my mistake. I got confused with the array and string dimensioning.

Code: Select all

dim target(11) as string
Yes, that is correct. This will dimension an array of strings, with each string dimensioned by default to 24 (23 char + null). It's the same as writing

Code: Select all

dim target(11) as string(24)
which is 264 bytes.

Post Reply