2

I just started a larger project completely coded in Assembly. Having multiple subroutines, macros, memory tables etc. I am wondering how I can best split the code up in multiple files. The goal is obvious a better structure and a better readability.

Currently I use an .inc-file containing all my constant definitions (.equ) but how to handle my many subroutines? If I want to put them in another file, do I need to use an .asm-file (since it contains executable code)? More general: what is the difference between .asm- and .inc-files, especially concerning including them into one main file in the end?

And once I have split up the file how can I handle multiple dependencies (i.e. file A includes file B and C of which file B includes file C as well). Since apparently my current environment, Atmel Studio 6, does not like double inclusions.

lorenzli
  • 620
  • 1
  • 10
  • 31
  • 1
    I am not the slightest bit familiar with atmega. However in other assembly languages, you can define global symbols (sometimes in conjunction with 'extern') that get resolved by the linker. As for 'double inclusions,' if your assembler supports it, what about using a variation on C's approach: `#ifndef file1 \n #define file1`? My apologies if atmega doesn't support these (or anything like them). But since you weren't getting any other responses, I thought I'd offer what help I could. – David Wohlferd Sep 30 '14 at 08:32
  • Sorry for the late answer but there is indeed something similar: .IFDEF and .ENDIF as seen here: [avr-asm-tutorial.net](http://www.avr-asm-tutorial.net/avr_en/beginner/DIREXP.html) I will go with this for the moment, thank you! – lorenzli Oct 07 '14 at 06:23

1 Answers1

0

There is more complicated problem and it's register allocation. Since you are making all code you may get in trouble if more functions need particular register and one function call the other one. C compiler is clever enough and can handle it according optimization settings. So surprisingly large ASM project may be less efficient than C code.

TMa
  • 723
  • 1
  • 5
  • 14
  • I agree concerning the optimization but sine some modules are time critical there is no choice concerning the language. For the registers I use name definitions in order to differentiate them, I do not think that this will be a problem. – lorenzli Oct 07 '14 at 06:17
  • But unless you include asm code with differentiated registers more times then registers are blocked globally. I personally using gcc inline assembler in C code for time critical stuff. It leaves register optimization on compiler, if there is a choice. You need know what register/constant can be used in particular opcode. asm directive uses rather cryptic syntax but it's feasible. – TMa Oct 07 '14 at 06:28
  • Pardon but what do you mean by "include asm code with differentiated registers more times then registers are blocked globally"? – lorenzli Oct 07 '14 at 07:45
  • You said that you "name definitions in order to differentiate them", i.e. I got you have *#define/.equ* directives to declare registers used in your modules (and subroutines). If the subroutines are called from other module then you need unique registers for each module (which can be changed by routine) unless you PUSH/POP them before/after each subroutine call. Inline asm can expand asm code more times in context of neigbour code. Price is flash memory consumption. You can't maintain such optimizations manually so I called it "registers are blocked=dedicated globally" if speed is important. – TMa Oct 07 '14 at 09:03
  • Programmers used to assembly are well aware of register allocation. I keep a "register-map" in the comments to document globally used registers and each subroutine also have documented each register parameter, what register is being overwritten by the subroutine and what registers return the results. C compilers are not clever, they just have a framewrok assigning a reserved set of regs for params and result. scratch regs are pushed/poped to/from the stack, even if it's not needed. Hence it might be more effective to write some parts as inline asm. A pure asm project don't have these issues. – Max Kielland Jun 20 '20 at 22:16