[TI ASM] Optimizations

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

DarkAuron
Maxcoderz Staff
Posts: 1349
Joined: Sat 18 Dec, 2004 6:53 pm

[TI ASM] Optimizations

Post by DarkAuron »

This topic is for all optimizations you guys know in assembly.. from simple opcode tricks to complicated self-modifying code. These should be more geared to common encounters, so keep that in mind. I don't see another topic like this so I thought it'd be a good idea to start it. I think if each one was presented and given a brief explanation then maybe we could all learn from them. Anyhow, another thing I need to say is that any off-topic posts will most likely be REMOVED.. you have been warned.

Now, to start this off and give an example layout, I'll give the most obvious one that pretty much every Z80 programmer knows (or seriously needs to).

Optimizes for speed AND size (3 clock cycles and 1 byte):

Code: Select all

ld a,0
to

Code: Select all

xor a
Good: Commonly used and is a quick, easy optimization.

Bad: Clears the C and N flags, so if you really need them, this optimization might not work out. Generally it'll be good for ya though.
[Gridwars Score] - E: 1860037 M: 716641 H: 261194
Andy_J
Calc Master
Posts: 1110
Joined: Mon 20 Dec, 2004 10:01 pm
Location: In the state of Roo Fearing
Contact:

Post by Andy_J »

Code: Select all

or a
Not

Code: Select all

cp 0
Another one everyone needs to know. :)
ImageImage
Image
koolmansam375
Extreme Poster
Posts: 479
Joined: Fri 17 Dec, 2004 11:09 pm
Contact:

Post by koolmansam375 »

load values into register pairs . Ex:
Instead of:

Code: Select all

ld b,$56
ld c,$47
do

Code: Select all

ld bc,$5647
[/code]
Image

Pongwars shall live!

blog is down atm. :-(
Andy_J
Calc Master
Posts: 1110
Joined: Mon 20 Dec, 2004 10:01 pm
Location: In the state of Roo Fearing
Contact:

Post by Andy_J »

I keep getting my big/little-endian stuff confused... Would that work like that?
ImageImage
Image
User avatar
tr1p1ea
Maxcoderz Staff
Posts: 4141
Joined: Thu 16 Dec, 2004 10:06 pm
Location: I cant seem to get out of this cryogenic chamber!
Contact:

Post by tr1p1ea »

Yes, the z80 is little endian.

If you dont like hex, you can also do:

Code: Select all

ld bc,(b_num * 256) + c_num
b will equal b_num and c will equal c_num.

This is using tasm mind you.
"My world is Black & White. But if I blink fast enough, I see it in Grayscale."
Image
Image
DarkAuron
Maxcoderz Staff
Posts: 1349
Joined: Sat 18 Dec, 2004 6:53 pm

Post by DarkAuron »

Speed optimization but hurts a bit in the space department: unrolling.

What this is, is copying the same code over and over again instead of using a loop, the speed gain is from not having to jump back for the loop, but the multiplied code takes away precious space. Do this only when speed is desperately needed.
[Gridwars Score] - E: 1860037 M: 716641 H: 261194
Spencer
Extreme Poster
Posts: 346
Joined: Mon 17 Jan, 2005 8:56 am
Location: Indiana

Post by Spencer »

Code: Select all

call xxxx
ret
to

Code: Select all

jp xxxx
Andy_J
Calc Master
Posts: 1110
Joined: Mon 20 Dec, 2004 10:01 pm
Location: In the state of Roo Fearing
Contact:

Post by Andy_J »

tr1p: Yes, I know it's little-endian, I just meant I keep getting how the data is stored in the source confused.

Spencer: Actually, that's not identical. The first one, you're branching to xxxx then returning to whatever called your routine. In the second one, you're going to xxxx and won't go back to whatever called you.
ImageImage
Image
Spencer
Extreme Poster
Posts: 346
Joined: Mon 17 Jan, 2005 8:56 am
Location: Indiana

Post by Spencer »

Yes it will. Only loss is one push-pop of the stack.
User avatar
Jim e
Calc King
Posts: 2457
Joined: Sun 26 Dec, 2004 5:27 am
Location: SXIOPO = Infinite lives for both players
Contact:

Post by Jim e »

What spencer did, for most purposes, should work. though it is not the same thing.
And as far as little endian goes, B will always have the MSB. Even though it is stored LSB -> MSB.


Oh and heres an unrolling I don't see often.

Code: Select all

;instead of

	ld hl,gbuf
	ld de,pic
	ld bc,768
	ldir

;try

	ld hl,gbuf
	ld de,pic
	ld bc,768
copyfast:
	ldi \ ldi \ ldi \ ldi
	ldi \ ldi \ ldi \ ldi   ;bc must be divisible by 16
	ldi \ ldi \ ldi \ ldi
	ldi \ ldi \ ldi \ ldi
	jp pe,copyfast
Last edited by Jim e on Mon 25 Apr, 2005 1:44 pm, edited 1 time in total.
Image
Andy_J
Calc Master
Posts: 1110
Joined: Mon 20 Dec, 2004 10:01 pm
Location: In the state of Roo Fearing
Contact:

Post by Andy_J »

Err, yeah, I guess that will work... I think I should go to sleep, ehh?
ImageImage
Image
User avatar
Dwedit
Maxcoderz Staff
Posts: 579
Joined: Wed 15 Dec, 2004 6:06 am
Location: Chicago!
Contact:

Post by Dwedit »

/me shuts his mouth...
Last edited by Dwedit on Mon 25 Apr, 2005 6:20 am, edited 1 time in total.
You know your hexadecimal output routine is broken when it displays the character 'G'.
DarkAuron
Maxcoderz Staff
Posts: 1349
Joined: Sat 18 Dec, 2004 6:53 pm

Post by DarkAuron »

LDIR takes 21 clock cycles per LDI, LDI itself is just 16 clock cycles. Thus theres a 5 cycle gain per LDI, or 5*16-10 (70) cycles boost per jump.. total of 3360 clock cycles gained. Unless I'm missing something here. And wait, shouldn't that be jp po,copyfast? And where'd that ret come from? :D
[Gridwars Score] - E: 1860037 M: 716641 H: 261194
User avatar
Jim e
Calc King
Posts: 2457
Joined: Sun 26 Dec, 2004 5:27 am
Location: SXIOPO = Infinite lives for both players
Contact:

Post by Jim e »

hehe... The ret is my mistake. :oops:

Oh but parity even is when the p/v flag is set, in ldi bc is zero when it's reset.
Image
User avatar
tr1p1ea
Maxcoderz Staff
Posts: 4141
Joined: Thu 16 Dec, 2004 10:06 pm
Location: I cant seem to get out of this cryogenic chamber!
Contact:

Post by tr1p1ea »

I am speechless .... O_O.
"My world is Black & White. But if I blink fast enough, I see it in Grayscale."
Image
Image
Post Reply