This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

shared variable slots

The feature of shared variables in C is a wonderful saver of DATA space. Can the same be implemented in assembler ?

Erik

  • I presume you're talking about Overlaying?

    You can specify OVERLAYABLE as a Segment Relocation Type - see Chapter 4, Assembler Directives, in the manual

  • Here is an idea. Take your assembly routines and write the headers in C. Include parameter and local variables, but nothing else. Then have the compiler emit assembly code for this framework. Now merge in the left out assembly code, and your done. The linker will then overlay variables just like C objects.

  • I think you just need to specify your Segments as OVERLAYABLE (where appropriate) and the Linker just does it?
    No need for 'C'!

  • You still need to follow the C51 naming conventions exactly to use this. So first learn the conventions. Then apply then very carfully. Overlap errors are real hard to find. Or, just let the C compiler generate it for you. Being quit dumb, I choose the later. For the smarter types just do it in assembly.

  • Do you really?
    Surely, it's the Linker which does the overlaying, and all it cares about is whether the segment has the 'Overlayable' attribute or not?

    The Linker doesn't know (or care) about the naming conventions of C51, A51, PL/M, Pascal, or whatever source language you choose to use; the naming conventions are only of interest to you as the programmer if you want to mix these languages?

    I've not actually tried this on the Keil tools, so I could be totally wrong.
    Again ;-)

  • I thought, that certain information like a function data segement is associated with a particular function, is passed to the linker by the "Naming Convention". I could be wrong.

  • I think that this is not the case:
    the name is simply what the Linker uses to identify an object; all the other information about the attributes of the object are conveyed in the Object File format (eg, whether it's a Segment or a variable; whether it's overlayable or not,...)

    But I could be wrong; maybe someone from Keil could clarify?

  • Hi Erik!

    Well, I think, you just have to use your memoryspace carefully.

    What I do is being restrictive with reserving space for global or static variables. I store all the local stuff in registers and the first thing I do in a subroutine is to save all used registers on stack.

    This requires commenting the souce code, though, because the variables are just named R0..R7 and don't have names that describe their purpose.

    I also use temporary storage like a place to store the result of 32bit or floating point calculation, but that has to be used very carefully. That means, I cannot be sure that the content of that location in memory will survive a subroutine call.

    Sure, a linker will be able to generate a more compact memory usage from C code, but I have done some pretty big projects in just assembler before I had the C compiler and it all worked out well. Programming assembler requires lotsa discipline and commenting. I think, I am faster with C, but will still do a couple of things in assembler.

    Take care
    Sven.

  • One comment that Sven made about using registers as variables. In Keil, you can equate variable names with registers so that you can, within a local program segment, have meaningful names for variables stored in registers.

    testvar  equ  R5
    testvar2 equ  R6
    
    tp1:
        mov  A,testvar
        mov  B,testvar2
        add  A,B
    
        ret
    

    This is a nice feature as it makes reading the code more straitforward.

  • Hi Tom!

    That's an excellent idea. Have never seen that before and actually never thought about it, since when I write the programm, I know the purpose I use the registers for anyway and I usually comment my programms a lot.

    Your method is much better.

    Take care
    Sven