Omnimaga

Calculator Community => TI Calculators => ASM => Topic started by: Halifax on August 27, 2007, 04:11:00 am

Title: GAME: Optimize this! (z80 assembly)
Post by: Halifax on August 27, 2007, 04:11:00 am
Basically everything can be optimized about this code. Post your optimized code with timings and bytes and we will see who has the most optimized code in the end!

c1-->
CODE
ec1


   xor a
   ld hl,0
   ld d,e
   ld l,h
mainloop:
   ld a,$FF
   out (1),a
   ld a,$FE
   out (1),a
   nop
   nop
   in a,(1)

   cp '1'
   call z,is_1
   cp '2'
   call z,is_2
   cp '3'
   call z,is_3
   cp '4'
   call z,is_4
   cp '5'
   call z,is_5
   cp 0
   jp z,mainloop
   jp mainloop

is_1:
   ld de,no_label1
   ld b,5
   call convert_num
   ret
is_2:
   ld de,no_label2
   ld b,5
   call convert_num
   ret
is_3:
   ld de,no_label3
   ld b,5
   call convert_num
   ret
is_4:
   ld de,no_label4
   ld b,5
   call convert_num
   ret
is_5:
   ld de,no_label5   
   ld b,5
   call convert_num
   ret




convert_num:
   ld hl,0
conv_loop:
   ld a,(de)
   inc de
   cp '1'
   ccf
   adc hl,hl
   djnz conv_loop
   ret
c2
ec2
Title: GAME: Optimize this! (z80 assembly)
Post by: DJ Omnimaga on August 27, 2007, 06:37:00 am
let's hope CoBB from MC won't find this topic because he's instant winner by default then O_Oshocked2.gif
Title: GAME: Optimize this! (z80 assembly)
Post by: Halifax on August 27, 2007, 01:15:00 pm
These optimizations are all easily findable, whether it is something I have said before, or something recently posted on MaxCoderz.
Title: GAME: Optimize this! (z80 assembly)
Post by: DJ Omnimaga on August 27, 2007, 01:36:00 pm
ok, now if i knew assembly. I hope an assembly coder like iambian or jon passes by
Title: GAME: Optimize this! (z80 assembly)
Post by: JonimusPrime on August 27, 2007, 03:44:00 pm
I fail at asm but even I can find Optimizations in that (too bad I'm too lazy to go through and type them).
Title: GAME: Optimize this! (z80 assembly)
Post by: Fallen Ghost on August 27, 2007, 04:15:00 pm
Using TASM syntax, not including timings or bytes (sorry, too lazy). But what the hell is that code doing? I'm assuming that you are looking for only 1 arrow at a time being pressed. I'm also assuming you want to optimize for speed, but not enough to uber-speed loop decompression. And I also noticed you kinda made a mistake by either not letting us know what the equates were for the 'x' (where x is the number) or by treating them as litteral and not including cpl. I'm assuming that 'x' is an equate of value less than 128 (you can't get more than that from a cpl'ed port 1 anyway).

Did you want me to throw that in or just say it is a code used on a hardware with other port settings (so let the port thing away and giving us the equates)? I'm assuming no. I'm also assuming that the no_labelX are not equally spaced/not in order in memory.

c1-->
CODE
ec1
ret
c2
ec2

And it may not be very speedy though, but I'm kinda too tired to do better.

Please answer my questions and you'll get better code, as now it wouldn't work for arrow keypress detection.

But as you see, the code, as you put it, has no output other than adding, and then does nothing with it, so for overall usefulness, then my code is the best (can't really beat 10T / 1Byte).
Title: GAME: Optimize this! (z80 assembly)
Post by: DJ Omnimaga on August 27, 2007, 04:39:00 pm
idk what he meant to do really, iirc it was meant to be a piece of code like the basic code samples in the 83+ manual, which does nothing, since its just some kind of excercise anyway  
Title: GAME: Optimize this! (z80 assembly)
Post by: Fallen Ghost on August 27, 2007, 04:42:00 pm
Yeah, but I need to know if he made errors in his above code or only optimize it. And also if the 'X' things are equates (reread my code now)
Title: GAME: Optimize this! (z80 assembly)
Post by: DJ Omnimaga on August 27, 2007, 05:03:00 pm
I'm trying to ask him on IRC atm but he seems idle hopefully he'll check this topic again soon
Title: GAME: Optimize this! (z80 assembly)
Post by: Halifax on August 27, 2007, 05:07:00 pm
Just optimize it. It is just an exercise. Do high-level and algorithm optimization. Nothing on how memory is accessed.'

It is all pretty simple although I am sure that there is one optimization people may not find.

EDIT: Actually there are 2 optimizations that I think won't be found.
Title: GAME: Optimize this! (z80 assembly)
Post by: Jon on August 27, 2007, 08:14:00 pm
c1-->
CODE
ec1
mainloop:
ld a,$FE
out (1),a
ld a,(de);7 t-state delay
in a,(1)

cp '1'
call z,is_1
cp '2'
call z,is_2
cp '3'
call z,is_3
cp '4'
call z,is_4
cp '5'
call z,is_5
jr mainloop

is_1:
ld de,no_label1
ld b,5
jr convert_num
is_2:
ld de,no_label2
ld b,5
jr convert_num
is_3:
ld de,no_label3
ld b,5
jr convert_num
is_4:
ld de,no_label4
ld b,5
jr convert_num
is_5:
ld de,no_label5
ld b,5
jr convert_num




convert_num:
ld hl,0
conv_loop:
ld a,(de)
inc de
cp '1'
ccf
adc hl,hl
djnz conv_loop
ret
c2
ec2
74 bytes
453 t-states if B=5
I don't see what the hell the point of this routine is....
Next time i'd put in something that actually has a function.  I was really confused :/confused.gif
Firstly, $FE is the keygroup for the 4 arrow keys, not the number keys.  Secondly, the hex value returned by port 1 is not '1', etc. but rather hex values based on the Direct Input table ($FE,$FD,$FB,$BF,$7F)
Title: GAME: Optimize this! (z80 assembly)
Post by: Halifax on August 27, 2007, 09:59:00 pm
Haha yeah, maybe I should but there are a lot of algorithm wise things that you missed to improve the code.
Title: GAME: Optimize this! (z80 assembly)
Post by: Fallen Ghost on August 27, 2007, 11:37:00 pm
bytes: 48
timing if B=5: 315 T states, 255 T if you unroll the conv_loop

c1-->
CODE
ec1mainloop2:
ld a,$FF
mainloop:
   out (1),a
   dec a
   out (1),a
   ld d,0
   in a,(1)
dec a
jr c,mainloop
add a,a
ld e,a
ld hl,table-(('1')*2)+2
add hl,de
ld a,(hl)
inc hl
ld l,(hl)
ld h,a
ld b,5
ld a,$FF
conv_loop:
   ld c,(hl)
   inc hl
   dec c
   adc a,a
Title: GAME: Optimize this! (z80 assembly)
Post by: Halifax on August 28, 2007, 01:13:00 am
Very good. Still conv_loop is not optimized.
Title: GAME: Optimize this! (z80 assembly)
Post by: Fallen Ghost on August 28, 2007, 02:31:00 pm
Well, registers have been changed and comparison too. Now unless you unroll it up (which I don't assume you think as a good idea), I don't see what we can do. That said, I don't know what exactly you can throw away in doing a more limited task (something only you knows).

[Edit]Just took out 17 clocks by using the cpl instead of the ccf
Oh, and by most optimized, you talk about speed only or bytes too?

[Edit #2]Yeah, well, look at it now
Title: GAME: Optimize this! (z80 assembly)
Post by: calc84maniac on August 28, 2007, 03:35:00 pm
What's with the "ld c,(hl) \ inc hl \ dec c"? You never use c for any reason whatsoever.
Title: GAME: Optimize this! (z80 assembly)
Post by: Fallen Ghost on August 28, 2007, 03:59:00 pm
Well, what's the point of putting hl to 0 prior to loop and then using it as a counter like I use the acc. instead?

There's none, it's just a piece of code to optimize. If you want my opinion, this routine could be replaced by "ret" and it would output the same, but it seems we have to care about whatever the routine does.  
Title: GAME: Optimize this! (z80 assembly)
Post by: DJ Omnimaga on August 28, 2007, 04:17:00 pm
Please quit complaining lol, fyi we just started those contests up, and they were planned in about 30 minutes (including the BASIC contest), and this is the very first piece of code. Just don't try to kill the contest project on its first piece of code lol, give it a chance. On next piece of code we'll take your suggestions into account
Title: GAME: Optimize this! (z80 assembly)
Post by: Halifax on August 28, 2007, 06:18:00 pm
On the next piece of code someone else can make up something. As DJ_Omnimaga said this was done in > 30 minutes. It was at least more activity, then what was being output prior to our contests. Next time someone else can do it. If you think this is bad, then you should try being in my Computer Concepts class where you write programs that are meant to do nothing and you can't even build them to test them. This contest was meant to be an algorithmic contest as I said before. Better algorithms should be recognized even on non-working code.

@Fallen_Ghost: Obviously yes your routine is the best, and what I consider the most optimized.

Fallen_Ghost wins!
Title: GAME: Optimize this! (z80 assembly)
Post by: DJ Omnimaga on August 28, 2007, 06:21:00 pm
COngrats!