flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Optimizations, Tips & Tricks

Author
Thread Post new topic Reply to topic
gdev



Joined: 04 Mar 2007
Posts: 13
gdev
As a lot of persons here, I'm working on my O/S (which is still reduced to the
boot loader for the moment...nevermind) and as I looked at code samples
posted in this forum and in various tutorials available on the web, I was
quite surprised by assembler 'tricks' allowing to optimize things or simply to
reduce the size of the program image (I'm thinking of the 512 bytes OS or 256
bytes demos). As I'm not an assembler expert I did not understood lots of them
or more probably, miss most of them, hence this thread:
is it possible to gather those tips (with some explanations)?
It may concern simple tips like usage of xor reg,reg to reset a register instead of
using mov reg,0 or more complicated things (about drivers, etc.) ...

Thanks in advance for anyone willing to share those tricks or personal tips!

NOTE: I may have missed a document having already done that...if someone has
pointers...
Post 04 Mar 2007, 13:44
View user's profile Send private message Reply with quote
HyperVista



Joined: 18 Apr 2005
Posts: 691
Location: Virginia, USA
HyperVista
gdev - first, welcome to the fasm community.

As for a central location for tips and tricks, Tomasz (father of fasm) created this thread sometime ago. This is a good place to start Cool

http://board.flatassembler.net/topic.php?t=4816
Post 04 Mar 2007, 15:24
View user's profile Send private message Visit poster's website Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u
When i was working on my 512byte compo entry, i coded what i wanted to fit in 512bytes and than started optimising for size, if in doubt, i try differant instructions or reg size etc, and assemble it and see if its smaller and still works.
Then if you get a supprize because something is smaller, you can find out why, so will help you learn.
Post 04 Mar 2007, 15:56
View user's profile Send private message Reply with quote
gdev



Joined: 04 Mar 2007
Posts: 13
gdev
HyperVista wrote:
gdev - first, welcome to the fasm community.

Thank you.

Quote:

As for a central location for tips and tricks, Tomasz (father of fasm) created this thread sometime ago. This is a good place to start Cool

http://board.flatassembler.net/topic.php?t=4816

Ah, ok, sorry for my useless thread, it seems this link is what I was looking for.
Post 04 Mar 2007, 15:58
View user's profile Send private message Reply with quote
gdev



Joined: 04 Mar 2007
Posts: 13
gdev
Dex4u wrote:
When i was working on my 512byte compo entry, i coded what i wanted to fit in 512bytes and than started optimising for size, if in doubt, i try differant instructions or reg size etc, and assemble it and see if its smaller and still works.
Then if you get a supprize because something is smaller, you can find out why, so will help you learn.

That's indeed a logical approach, but it requires already a good knowledge of the
instruction set to be usable so it's quite difficult. For the moment I prefer work
with a small subset of the instructions with the exception of some well known
optimizations. But the list of those optimizations is not easy to found. I'm going
to read the material pointed by the link above. This should be a good starter.
Post 04 Mar 2007, 16:05
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo
Okay, here's some simple (somewhat DOS-related) suggestions:


  • xor ax,ax (instead of mov ax,0 BUT xor changes the flags SO pushf xor ax,ax popf is worse than mov ax,0)
  • cmp al,10 sbb al,69h das (converts hex nibble in AL to ASCII)
  • int 29h (DOS: output ASCII value in AL to STDERR)
  • make sure all jumps are short! (probably only when not using FASM, for some reason)
  • es lodsb instead of mov al,byte [es:si] (but first incs si)
  • push es and pop ds instead of mov ax,es and mov ds,ax
  • xchg ax,bx instead of mov ax,bx or xchg cx,bx (usually 1 byte when xchg w/ ax BUT xchg al,dl is 2 bytes!)
  • sub ah,2 instead of dec ah, dec ah (BUT sub di,2 is bigger than dec di, dec di)
  • use aam instead of div (if possible)
  • movsw, movsb instead of mov cx,3 and rep movsb
  • mov [si],5 instead of mov [bp],5
  • use reg that's already == 0 for displacement instead of raw 0
  • and al,0DFh (to uppercase), or al,20h (to lowercase), xor al,20h (toggle case)
  • quit your DOS .COM w/ ret instead of int 20h or mov ah,4Ch and int 21h Smile
  • don't forget jcxz, xlat, not, neg, cwd, rol, shl, imul, adc, sbb, test
  • use lea wherever possible (lea si,[di+bp] instead of mov si,di and add si,bp OR lea cx,[di-1] BUT lea ax,[my_label] is bigger than mov ax,my_label)
  • don't compute values at runtime if you can do so at compile time
  • don't forget .bss section (uninitialized data not included in .EXE size)
  • go with the flow! (don't fight default return values or data locations like default DTA for int 21h,4Eh/4Fh at PSP:80h or byte [cmdline_len] also at PSP:80h AND avoid too many seg overrides or displacements if possible!)
  • ask someone smart (vid or ATV, not me!) to look at and improve your code Smile
Post 04 Mar 2007, 19:56
View user's profile Send private message Visit poster's website Reply with quote
gdev



Joined: 04 Mar 2007
Posts: 13
gdev
Thanks, those are good points I will keep in mind. This is the type of short straightforward list I like Smile
Post 04 Mar 2007, 23:13
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.