header files in Assembler

Started by matthewbuza1 April 5, 2006
I am currently using the msp4301612, coding in assembler and my 
program just topped 3500 lines. I would like to start and offload 
some of the bulk. I have a couple of questions. First, is it 
possible to create a #include file that can be called as a function, 
doing some type of computation and then returning some type of 
value? How would i go about setting that up. I have been looking 
through the manual regarding the XLib and Xlink linker. Is this the 
correct path to approach this problem. Second, which is  much like 
the first, could I have a file that just stores values such as my 
constants other critical memory locations. And have these locations 
and values be set initially, instead of having all the clutter i do 
at the beginning of my program. My main consern is that I will have 
to build a virtual map of the US maybe north america, of which i 
need to know if my device is in an authorized location or not, if 
the device is deamed to be in an invalid location a constant needs 
to be set, then I need to return back to the program and execute 
depending on that value. If there is a short example where an 
assembler program calls some function pulling values from the main 
program and returns a value back to the main function, i would love 
to see that example. Thanks.

-Matthew Buza





Beginning Microcontrollers with the MSP430

matthewbuza1 wrote:
> I am currently using the msp4301612, coding in
assembler and my 
> program just topped 3500 lines. I would like to start and offload 
> some of the bulk. I have a couple of questions. First, is it 
> possible to create a #include file that can be called as a function, 
> doing some type of computation and then returning some type of 
> value? How would i go about setting that up. I have been looking 
> through the manual regarding the XLib and Xlink linker. Is this the 
> correct path to approach this problem. Second, which is  much like 
> the first, could I have a file that just stores values such as my 
> constants other critical memory locations. And have these locations 
> and values be set initially, instead of having all the clutter i do 
> at the beginning of my program. My main consern is that I will have 
> to build a virtual map of the US maybe north america, of which i 
> need to know if my device is in an authorized location or not, if 
> the device is deamed to be in an invalid location a constant needs 
> to be set, then I need to return back to the program and execute 
> depending on that value. If there is a short example where an 
> assembler program calls some function pulling values from the main 
> program and returns a value back to the main function, i would love 
> to see that example. Thanks.
> 
> -Matthew Buza

There are many ways to organize your program into different modules.

The traditional way is to have more than one source file. Each file is 
assembled separately and linked together by the linker.

Header files, i.e. files included by #include, normally should not 
contain code. Instead they should contain macros and constants used by 
the source files.

When calling a function from assembler you will need something like this:

     MODULE MY_FIRST_MODULE
     EXTERN my_func
     RSEG   CODE:CODE:REORDER:NOROOT(1)

     CALL   #my_func

     ...

     ENDMOD

The function that should be called need to do:

     MODULE MY_SECOND_MODULE
     PUBLIC my_func
     RSEG   CODE:CODE:REORDER:NOROOT(1)

my_func:

     ...

     ENDMOD


Also, have you ever considered writing your application in C? It 
contains support for initializing global variables at startup, all you 
have to do is to write them like this:

     int correct_location = 1;

Also, returning values and checking them is trivial, your main program 
would look like this:

     int main()
     {
        if (is_location_correct())
        {
          // Ok...
        }
        else
        {
          // The user has placed the device somewhere bad...
        }
        ...
     }


     -- Anders Lindgren, IAR Systems
-- 
Disclaimer: Opinions expressed in this posting are strictly my own and
not necessarily those of my employer.


Thanks so much for your help, i began to look into it and i think i 
found another path to acheive the same thing. What I did was the to 
have the include statement at the top of my main program. 

#include  "msp430x16x.h"
#include "test_reg.s43"



In The mainloop i put a call in to the label in the included file.



Mainloop    nop
            call    #test_reg


Then I made a small file to test if the idea works. There were 
problems where the stackpointer was getting lost, and it was because 
the IAR loader was putting the included file at the beginning of 
memory, somewhere in the interrupt area. So the ORG 04000h statement 
was added so IAR knew where to put the program in flash.
;--------------------------------
            ORG     04000h                  ; Progam Start 
;--------------------------------


test_reg   nop
           mov.b #00h,R14
           ret



I know this is a crude method, and know i have to keep track so that 
my included files dont overlap with my main program in the flash 
memory, but i think its a simple way to link multiple programs. I 
was having problems working with the IAR figuring the Xlinker and 
Xlib the documentation was just not clear. Anyway thanks for your 
help Anders, i appreciate it. 

Best,

Matthew

--- In msp430@msp4..., Anders Lindgren <Anders.lindgren@...> 
wrote:
>
> matthewbuza1 wrote:
> > I am currently using the msp4301612, coding in assembler and my 
> > program just topped 3500 lines. I would like to start and 
offload 
> > some of the bulk. I have a couple of
questions. First, is it 
> > possible to create a #include file that can be called as a 
function, 
> > doing some type of computation and then
returning some type of 
> > value? How would i go about setting that up. I have been looking 
> > through the manual regarding the XLib and Xlink linker. Is this 
the 
> > correct path to approach this problem.
Second, which is  much 
like 
> > the first, could I have a file that just
stores values such as 
my 
> > constants other critical memory locations.
And have these 
locations 
> > and values be set initially, instead of
having all the clutter i 
do 
> > at the beginning of my program. My main
consern is that I will 
have 
> > to build a virtual map of the US maybe north
america, of which i 
> > need to know if my device is in an authorized location or not, 
if 
> > the device is deamed to be in an invalid
location a constant 
needs 
> > to be set, then I need to return back to the
program and execute 
> > depending on that value. If there is a short example where an 
> > assembler program calls some function pulling values from the 
main 
> > program and returns a value back to the main
function, i would 
love 
> > to see that example. Thanks.
> > 
> > -Matthew Buza
> 
> There are many ways to organize your program into different 
modules.
> 
> The traditional way is to have more than one source file. Each 
file is 
> assembled separately and linked together by the
linker.
> 
> Header files, i.e. files included by #include, normally should not 
> contain code. Instead they should contain macros and constants 
used by 
> the source files.
> 
> When calling a function from assembler you will need something 
like this:
> 
>      MODULE MY_FIRST_MODULE
>      EXTERN my_func
>      RSEG   CODE:CODE:REORDER:NOROOT(1)
> 
>      CALL   #my_func
> 
>      ...
> 
>      ENDMOD
> 
> The function that should be called need to do:
> 
>      MODULE MY_SECOND_MODULE
>      PUBLIC my_func
>      RSEG   CODE:CODE:REORDER:NOROOT(1)
> 
> my_func:
> 
>      ...
> 
>      ENDMOD
> 
> 
> Also, have you ever considered writing your application in C? It 
> contains support for initializing global variables at startup, all 
you 
> have to do is to write them like this:
> 
>      int correct_location = 1;
> 
> Also, returning values and checking them is trivial, your main 
program 
> would look like this:
> 
>      int main()
>      {
>         if (is_location_correct())
>         {
>           // Ok...
>         }
>         else
>         {
>           // The user has placed the device somewhere bad...
>         }
>         ...
>      }
> 
> 
>      -- Anders Lindgren, IAR Systems
> -- 
> Disclaimer: Opinions expressed in this posting are strictly my own 
and
> not necessarily those of my employer.
>






there are 2 options:
1. 1 big source (include *.s43) and leave the asm to work
2. use a linker (i think Anders can explain again)

Anders,
i believe your explanations are more valuable than mine because i am
not a genuine software guy.

avram



--- matthewbuza1 <matthewbuza1@matt...> wrote:

> Thanks so much for your help, i began to look into
it and i think i 
> found another path to acheive the same thing. What I did was the to 
> have the include statement at the top of my main program. 
> 
> #include  "msp430x16x.h"
> #include "test_reg.s43"
> 
> 
> 
> In The mainloop i put a call in to the label in the included file.
> 
> 
> 
> Mainloop    nop
>             call    #test_reg
> 
> 
> Then I made a small file to test if the idea works. There were 
> problems where the stackpointer was getting lost, and it was because 
> the IAR loader was putting the included file at the beginning of 
> memory, somewhere in the interrupt area. So the ORG 04000h statement 
> was added so IAR knew where to put the program in flash.
> ;--------------------------------
>             ORG     04000h                  ; Progam Start 
> ;--------------------------------
> 
> 
> test_reg   nop
>            mov.b #00h,R14
>            ret
> 
> 
> 
> I know this is a crude method, and know i have to keep track so that 
> my included files dont overlap with my main program in the flash 
> memory, but i think its a simple way to link multiple programs. I 
> was having problems working with the IAR figuring the Xlinker and 
> Xlib the documentation was just not clear. Anyway thanks for your 
> help Anders, i appreciate it. 
> 
> Best,
> 
> Matthew
> 
> --- In msp430@msp4..., Anders Lindgren <Anders.lindgren@...> 
> wrote:
> >
> > matthewbuza1 wrote:
> > > I am currently using the msp4301612, coding in assembler and my 
> > > program just topped 3500 lines. I would like to start and 
> offload 
> > > some of the bulk. I have a couple of questions. First, is it 
> > > possible to create a #include file that can be called as a 
> function, 
> > > doing some type of computation and then returning some type of 
> > > value? How would i go about setting that up. I have been looking 
> > > through the manual regarding the XLib and Xlink linker. Is this 
> the 
> > > correct path to approach this problem. Second, which is  much 
> like 
> > > the first, could I have a file that just stores values such as 
> my 
> > > constants other critical memory locations. And have these 
> locations 
> > > and values be set initially, instead of having all the clutter i 
> do 
> > > at the beginning of my program. My main consern is that I will 
> have 
> > > to build a virtual map of the US maybe north america, of which i 
> > > need to know if my device is in an authorized location or not, 
> if 
> > > the device is deamed to be in an invalid location a constant 
> needs 
> > > to be set, then I need to return back to the program and execute 
> > > depending on that value. If there is a short example where an 
> > > assembler program calls some function pulling values from the 
> main 
> > > program and returns a value back to the main function, i would 
> love 
> > > to see that example. Thanks.
> > > 
> > > -Matthew Buza
> > 
> > There are many ways to organize your program into different 
> modules.
> > 
> > The traditional way is to have more than one source file. Each 
> file is 
> > assembled separately and linked together by the linker.
> > 
> > Header files, i.e. files included by #include, normally should not 
> > contain code. Instead they should contain macros and constants 
> used by 
> > the source files.
> > 
> > When calling a function from assembler you will need something 
> like this:
> > 
> >      MODULE MY_FIRST_MODULE
> >      EXTERN my_func
> >      RSEG   CODE:CODE:REORDER:NOROOT(1)
> > 
> >      CALL   #my_func
> > 
> >      ...
> > 
> >      ENDMOD
> > 
> > The function that should be called need to do:
> > 
> >      MODULE MY_SECOND_MODULE
> >      PUBLIC my_func
> >      RSEG   CODE:CODE:REORDER:NOROOT(1)
> > 
> > my_func:
> > 
> >      ...
> > 
> >      ENDMOD
> > 
> > 
> > Also, have you ever considered writing your application in C? It 
> > contains support for initializing global variables at startup, all 
> you 
> > have to do is to write them like this:
> > 
> >      int correct_location = 1;
> > 
> > Also, returning values and checking them is trivial, your main 
> program 
> > would look like this:
> > 
> >      int main()
> >      {
> >         if (is_location_correct())
> >         {
> >           // Ok...
> >         }
> >         else
> >         {
> >           // The user has placed the device somewhere bad...
> >         }
> >         ...
> >      }
> > 
> > 
> >      -- Anders Lindgren, IAR Systems
> > -- 
> > Disclaimer: Opinions expressed in this posting are strictly my own 
> and
> > not necessarily those of my employer.
> >
> 
> 
> 
> 
> 
> 


__________________________________________________