Initializations in assembly or C : which is favourable ? why ?

Hello all

Im new to the world of embedded systems. I had a basic query.

Im working on a project that requires me to migrate the code for a 16- bit DSP Processor to a 32-bit DSP Processor.

All the initializations of the processor peripherals, registers have beeen written in Assembly. My predicament is whether i should re-write all these assembly routines in C for my new 32-bit processor of just re-write them in assembly ???

Is there a thumb rule that initializations should be done only in assembly ??? Please give me your opinions ...

regards

techie

Reply to
techie.embedded
Loading thread data ...

Loader , then kernel . Intialization is

invisible , its in the loader .

Loader has a debugger , so you never

have trouble loading any software .

Its tiny and resident .

All applications are integrated into the

kernel .

Because the kernel "reuses"

modular threads , the application are also

tiny . Spreadsheet is 50KB . No more Files

nor Folders , kernel manages all objects , and

this increases effeciency so much , it actually

reduces the size of the kernel !

No more FAT32 , NTFS , Hyperterminals ,

No more USB , no more C++ , no more

Linux , no GCC ...

A few ARM mcu's with 100KB SRAM and 8MB

external PSRAM can do everything a notebook

PC can do . uses 4 watts .

Software is easy , elegant , simple ,

if you do it yourself .

I will give free , an OpSystem for ARM .

No English , no text , a true G.U.I. , it needs

no manuals , there is no learning curve ,

anyone , anywhere can simply watch the

images on LCD , and hit buttons and

the kernel shows you with images and icons,

what will happen .

No touch screen needed , there are

SoftKeys close to LCD . When you see

an icon next to that key , you know what the

key does . Same as touch screen ...

Reply to
werty

Anything that needs to be initialised before C can run must be done in assembler, i.e. memory- related chip selects, stack initialisation etc. But once you've got that, the choice is yours. Since architectures are likely to differ in details of registers etc, what you gain from writing the setup routines in C is that you don't have to learn the assembly language for that processor. It's also more easily maintained for other people, as there's only one FM to read. I haven't bothered to learn the assembler for several processors that I've used, and I haven't come unstuck (so far).

Paul Burke

Reply to
Paul Burke

If it were me I would want to keep as much in C as I can because it makes it so much easier for anyone coming along after you. I'm doing this very task at the moment - converting old Z80 assembly into C and I've been at it for weeks because it is a horrible fiddly awkward job.

Reply to
Tom Lucas

Yes, Paul is correct.

Using most C compliers for embedded... they run an assembler "startup" file/program 1st to configure the mciro, and get the data structures ready for C variables and stack etc.

For simple programs you let the startup file do it's job.... but some times additional hardware many need initialized before C code entry... this is wher you may have add this assembler code in the assembler startup file. BUT sometimes you can initialize as the 1st bit of C code.... (some people name this function hardware_init(); for hardwarfe initalization).

Joe

Reply to
Joe G (Home)

It's always good to learn to read and understand the assembly for processors that you use, even if you don't learn it well enough to write efficient assembly code. An understanding of the underlying architecture, and the ability to read and understand compiler-generated assembly, is important when you want to get the best out of your chip.

The very first few instructions run after reset are typically written in assembly. But most of the C pre-main startup code can be written in C - you just have to be a little careful about what you are doing. It's fine to write things like the bss clearing loops in C, and even things like setting up memory and chip selects - you just have to be careful to understand what is possible at any given time. For example, don't call any functions until you've got your stack in order. Many C crt0 routines contain hooks for running additional configuration functions, in C or assembly, to give you a chance to do early setup.

Reply to
David Brown

C.

No.

You usually need a little bit of startup code in assembly, but initializing peripherals is done in C for all the projects I've seen in the past 20 years.

--
Grant Edwards                   grante             Yow!  Civilization is
                                  at               fun! Anyway, it keeps
 Click to see the full signature
Reply to
Grant Edwards

The sample code and the examples in the hardware manuals are usually in assembler. I have seen quite a lot of code which was just copy pasted from there.

Vladimir Vassilevsky

DSP and Mixed Signal Design Consultant

formatting link

Reply to
Vladimir Vassilevsky

... snip ...

Then I suspect that the assembly code is poorly organized, and you would probably be well advised to write the application from scratch.

--
Chuck F (cbfalconer at maineline dot net)
   Available for consulting/temporary embedded and systems.
Reply to
CBFalconer

I agree and we have discussed this previously in another thread. However, I expect to complete the conversion either today or tomorrow and, hopefully, the whole ugly business will be behind me. However, with regard to the OP, then I would maintain that well structured and laid out assembly code is certainly workable but well structured and laid out C is better from a maintainability point of view.

Reply to
Tom Lucas

... snip ...

I disagree. I have never had any problem maintaining my own assembly code [1]. However, I have had problems porting it to new architectures and CPUs.

[1] Which is well structured, by definition :-)
--
Chuck F (cbfalconer at maineline dot net)
   Available for consulting/temporary embedded and systems.
Reply to
CBFalconer

Surely when assessing the maintainability of a code base the author(s) should not be involved :-) One should assume that the orginal authors will be unavailable, uncooperative or too expensive.

Peter

Reply to
Peter Dickerson

The machine is for the man, not the man is for the machine.

Only if you are mastering it. For a previously unknown processor, it will take at least a week to get to this level of coding skill. What for?

but it

When writing a project in assembler, the bigger is the code the more it starts resembling the compiler. The only way to keep the project manageable is to get it structured. Soon you will have the agreements on the function prologues and epilogues and such. So the efficiency advantage of the assembler will be traded off for the structure.

How long would it take to develop the "Hello World" on the graphics LCD in assemler?

Vladimir Vassilevsky

DSP and Mixed Signal Design Consultant

formatting link

Reply to
Vladimir Vassilevsky

If the developer knows C and assembler equally well it is better to write the initialization in assembler because assembler is closer to the machine. Assembler will produce faster, tighter code, but it takes longer to develop and can be tougher to debug than C. Personally, if there is not much code my personal preference is assembler.

Reply to
SWDeveloper

hi vlaidimir, nice article you wrote in IEEE SIGNAL PROCESSINGMAGAZINE..GOOD keep it up

formatting link

regards particlereddy

Reply to
PARTICLEREDDY (STRAYDOG)

Why would writing in assembly produce faster, tighter code for an initialisation routine? There are certainly cases where a good assembly programmer can beat a C compiler, but that's usually either on a small and limited microcontroller, or on very specific tasks in which the assembly programmer can take advantage of cpu features unused by the compiler. A typical initialisation routine consists of a bunch of writes of constant data to peripherals, and maybe some loops to clear buffers. A dumb compiler will generate the same code an assembler programmer would write, while a good compiler may generate better code - it can take advantage of commonality between the writes that an assembler programmer cannot (assuming he wants to write readable and maintainable code).

You sometimes need to use assembly for functions that cannot be expressed in C, such as accessing special registers - that's what inline assembly is for. And (depending on the compiler and target) there may be issues with function prologues at the start of your first C function. But outside of these issues, initialisation routines are part of your normal program, and should be written in the style and language of the rest of the program.

Reply to
David Brown

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.