Assembly Basics: Keeping your code small
#1
TGO Supporter
Thread Starter
Join Date: Jul 2000
Posts: 812
Likes: 0
Received 1 Like
on
1 Post
Engine: 5.7L V8
Transmission: 700R4
Assembly Basics: Keeping your code small
The more stuff you can squeeze into a chip, the better. I'm hoping to make this post into a reference for some tips on how to shrink your code down when you can.
I'm home sick on a Saturday night
, so forgive me if I make any mistakes, my head is a bit fuzzy.
My first tip is to pay attention to what the assembler generates with certain instructions, because when you're writing in assembly you tend to forget what bytes are being output.
Use this page for reference:
http://home.earthlink.net/~tdickens/...tructions.html
Let's say you're making a routine that uses Indirect Addressing and you want to decide which index register to use, X or Y.
Well, have a look in the Pink Book or the page I listed above for the ADCA instruction, Add with Carry to register A.
You'll see that to use register X, it will generate an opcode that has 2 bytes, the opcode (E9) and then a single data byte.
To use register Y for the index, you'll see that it uses 2 bytes for the opcode (18E9), then another data byte, for a total of 3 bytes. It also takes 5 clock cycles, which is 1 more than using X.
So, as a general rule, use index register X if you can, and only use Y if the X register is already being used for something else.
I'm home sick on a Saturday night
![Frown](https://www.thirdgen.org/forums/images/smilies/frown.gif)
My first tip is to pay attention to what the assembler generates with certain instructions, because when you're writing in assembly you tend to forget what bytes are being output.
Use this page for reference:
http://home.earthlink.net/~tdickens/...tructions.html
Let's say you're making a routine that uses Indirect Addressing and you want to decide which index register to use, X or Y.
Well, have a look in the Pink Book or the page I listed above for the ADCA instruction, Add with Carry to register A.
You'll see that to use register X, it will generate an opcode that has 2 bytes, the opcode (E9) and then a single data byte.
To use register Y for the index, you'll see that it uses 2 bytes for the opcode (18E9), then another data byte, for a total of 3 bytes. It also takes 5 clock cycles, which is 1 more than using X.
So, as a general rule, use index register X if you can, and only use Y if the X register is already being used for something else.
#2
I love this assembly basics series, maybe cause I am in a Mototrola HC12 class right now. Another good prctice to use is indexed addressing. Althought it might not make your code much smaller it does makes it easier to write your code and understand.
Here is the simulator we use.
http://www.hc11.demon.nl/thrsim11/thrsim11.htm
Here is the simulator we use.
http://www.hc11.demon.nl/thrsim11/thrsim11.htm
#3
Supreme Member
![](https://www.thirdgen.org/forums/images/ranks/tgo10.gif)
I just got done writing an assembler for a class and you had to go make this post. Painful memories ...
![Smilie](https://www.thirdgen.org/forums/images/smilies/smile.gif)
#4
Supreme Member
![](https://www.thirdgen.org/forums/images/ranks/tgo10.gif)
iTrader: (2)
Join Date: Jan 2002
Location: Moorestown, NJ
Posts: 9,962
Likes: 0
Received 3 Likes
on
3 Posts
Car: 88 Camaro SC
Engine: SFI'd 350
Transmission: TKO 500
Axle/Gears: 9-bolt w/ 3.23's
I love assembly. Its the most efficient way to program. There is so many little tricks you can do to cut down on execution time and software size.
#5
Junior Member
Join Date: Oct 2004
Location: Austin, TX
Posts: 17
Likes: 0
Received 0 Likes
on
0 Posts
Originally posted by dimented24x7
I love assembly. Its the most efficient way to program. There is so many little tricks you can do to cut down on execution time and software size.
I love assembly. Its the most efficient way to program. There is so many little tricks you can do to cut down on execution time and software size.
Still, it's fun to rewrite the bootstrap loader and device drivers when you switch to a 32 bit version of a processor from an older 16 bit version. That is, until you realise you're the first (and only) customer to use this chip and they haven't really qual-tested the assembler and you have all sorts of problems with op codes getting truncated or overlapping each other, etc. Then you add the C compiler that generates assembler files incorrectly for a number of reasons.
Some days, I wish I was working on PC code...
#6
Supreme Member
![](https://www.thirdgen.org/forums/images/ranks/tgo10.gif)
iTrader: (1)
Join Date: Jan 2002
Location: garage
Posts: 4,432
Likes: 0
Received 1 Like
on
1 Post
Engine: 3xx ci tubo
Transmission: 4L60E & 4L80E
If you are having assembler / compiler problems I recommend GCC. It has been around a long time and the source is available which allows you to make quick fixes if you find a problem.
Give MINGW and GTK+ a try on the PC with GCC. GCC will compile atmel AVR, Freescale 68HC_stuff & DSPs, Analog Devices DSP.
Some say that assembly is the most efficient.......well, that assumes you write efficient assebly to begin with. C might compile to something more efficient. You don't know until you try. Writing C to compile well (efficient) is a matter of coding style. It takes time to learn how to make the compiler happy by looking at the assembly it creates.
Give MINGW and GTK+ a try on the PC with GCC. GCC will compile atmel AVR, Freescale 68HC_stuff & DSPs, Analog Devices DSP.
Some say that assembly is the most efficient.......well, that assumes you write efficient assebly to begin with. C might compile to something more efficient. You don't know until you try. Writing C to compile well (efficient) is a matter of coding style. It takes time to learn how to make the compiler happy by looking at the assembly it creates.
#7
Junior Member
Join Date: Oct 2004
Location: Austin, TX
Posts: 17
Likes: 0
Received 0 Likes
on
0 Posts
Originally posted by junkcltr
If you are having assembler / compiler problems I recommend GCC. It has been around a long time and the source is available which allows you to make quick fixes if you find a problem.
Give MINGW and GTK+ a try on the PC with GCC. GCC will compile atmel AVR, Freescale 68HC_stuff & DSPs, Analog Devices DSP.
Some say that assembly is the most efficient.......well, that assumes you write efficient assebly to begin with. C might compile to something more efficient. You don't know until you try. Writing C to compile well (efficient) is a matter of coding style. It takes time to learn how to make the compiler happy by looking at the assembly it creates.
If you are having assembler / compiler problems I recommend GCC. It has been around a long time and the source is available which allows you to make quick fixes if you find a problem.
Give MINGW and GTK+ a try on the PC with GCC. GCC will compile atmel AVR, Freescale 68HC_stuff & DSPs, Analog Devices DSP.
Some say that assembly is the most efficient.......well, that assumes you write efficient assebly to begin with. C might compile to something more efficient. You don't know until you try. Writing C to compile well (efficient) is a matter of coding style. It takes time to learn how to make the compiler happy by looking at the assembly it creates.
Last edited by derf; 03-15-2005 at 10:16 PM.
Trending Topics
#8
Supreme Member
![](https://www.thirdgen.org/forums/images/ranks/tgo10.gif)
iTrader: (2)
Join Date: Jan 2002
Location: Moorestown, NJ
Posts: 9,962
Likes: 0
Received 3 Likes
on
3 Posts
Car: 88 Camaro SC
Engine: SFI'd 350
Transmission: TKO 500
Axle/Gears: 9-bolt w/ 3.23's
Originally posted by junkcltr
Some say that assembly is the most efficient.......well, that assumes you write efficient assebly to begin with. C might compile to something more efficient. You don't know until you try. Writing C to compile well (efficient) is a matter of coding style. It takes time to learn how to make the compiler happy by looking at the assembly it creates.
Some say that assembly is the most efficient.......well, that assumes you write efficient assebly to begin with. C might compile to something more efficient. You don't know until you try. Writing C to compile well (efficient) is a matter of coding style. It takes time to learn how to make the compiler happy by looking at the assembly it creates.
With my old C3, the proc is slow as hell so the only way to get more advanced features in without overwhelming it is to have elegant, efficient coding. I dont doubt that C compilers can be efficient, but now and then there will be little time saving things that only a human eye could spot.
Of course, there are limits. I could make code thats optimized for min instruction time, but interacting with the code when tuning would be far too cumbersome. Good example is the MAF I added in. Using scalars, mutiple tables and such would make for the most efficient code. But, all these seperate tables make calibrating the maf a pain. Instead I opted to add an improved lookup routine so I could have one large 16 bit table to store the airflow in. Same thing with the AFR. Having one consolidated table uses more instructions code wise, but makes changing the AFR as easy as typing in a value. I do like efficiency, but at the same time, I also want to see a clean, user friendly interface (like, no biases, scalars, multipliers, modifiers, tweakers, etc.) when Im tuning.
Thread
Thread Starter
Forum
Replies
Last Post
Fast355
DFI and ECM
14
12-02-2016 06:33 PM
ambainb
Camaros for Sale
11
04-25-2016 09:21 PM
BurlyChev
Tech / General Engine
14
08-22-2015 01:52 PM