• ASM Optimized routines 5 1
Currently:

### Author Topic: ASM Optimized routines  (Read 70015 times)

0 Members and 1 Guest are viewing this topic.

#### Xeda112358

• they/them
• Moderator
• LV12 Extreme Poster (Next: 5000)
• Posts: 4684
• Rating: +718/-6
• Calc-u-lator, do doo doo do do do.
##### Re: ASM Optimized routines
« Reply #45 on: May 02, 2012, 09:07:45 pm »
Oh, wow, awesome! I cannot believe I didn't see that, that is a source of some of my other optimisations from the original code o.O

#### calc84maniac

• eZ80 Guru
• Coder Of Tomorrow
• LV11 Super Veteran (Next: 3000)
• Posts: 2898
• Rating: +467/-17
##### Re: ASM Optimized routines
« Reply #46 on: May 02, 2012, 10:25:44 pm »
Here's a very optimized way to convert a 16-bit signed number into an 8-bit signed number in a with overflow handling (if hl<-128, a=-128; if hl>127, a=127). Two added bonus to being super small and super fast are that it destroys nothing and that you could easily modify it to make the input a 16-bit register other than hl.

Code: [Select]
Signed16To8: ld a,l add a,a sbc a,a sub h ld a,l ret z ld a,h add a,a sbc a,a xor %01111111 ret

Implied challenge accepted!
Code: [Select]
Signed16To8: ld a,l add a,a ld a,h adc a,l cp l ret z ld a,$7F ret p inc a ret Edit: Whoops, misteak Edit2: This routine is a failure, disregard its failtasticness « Last Edit: May 02, 2012, 10:32:44 pm by calc84maniac » "Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman #### Xeda112358 • they/them • Moderator • LV12 Extreme Poster (Next: 5000) • Posts: 4684 • Rating: +718/-6 • Calc-u-lator, do doo doo do do do. ##### Re: ASM Optimized routines « Reply #47 on: July 03, 2012, 12:13:46 pm » Necroedit: For a much better routine, please try the routines at the end of this post! I created this last night for my next project: Code: [Select] PseudoRandWord:;Outputs:; BC was the previous pseudorandom number; HL is the pseudorandom number;f(n+1)=(241f(n)+257) mod 65536 ;65536;181 cycles, add 17 if called ld hl,(randSeed) ld c,l ld b,h add hl,hl add hl,bc add hl,hl add hl,bc add hl,hl add hl,bc add hl,hl add hl,hl add hl,hl add hl,hl add hl,bc inc h inc hl ld (randSeed),hl retThere are a few other nice features, too. For example, every 16-bit value is hit if you run this 65536 times. Or, if you only read 1 byte (for example, H from the output), it will hit every 8-bit number once if you run this 256 times. Plus, it can be seeded, which has its own uses. This can be modified to be smaller, too, if you know what you are doing, but I just like the numbers 241 and 257. Anyways, it produces some nice results P.S.-I used this in a routine called "ShuffleDeck" and it works very well. « Last Edit: August 28, 2019, 02:41:06 pm by Xeda112358 » #### chickendude • LV8 Addict (Next: 1000) • Posts: 816 • Rating: +90/-1 • Pro-Riot Squad ##### Re: ASM Optimized routines « Reply #48 on: July 04, 2012, 11:19:47 am » I don't understand the theory behind that algorithm, but you could save a couple clocks with SMC And is ShuffleDeck a hint at what your next project might be? #### Xeda112358 • they/them • Moderator • LV12 Extreme Poster (Next: 5000) • Posts: 4684 • Rating: +718/-6 • Calc-u-lator, do doo doo do do do. ##### Re: ASM Optimized routines « Reply #49 on: July 09, 2012, 08:08:46 am » Yes, you can use SMC to save at least 6 cycles for RAM programs My next mini project is an app with some small games (including card games). I don't have my computer with me, but I will post a working sound routine next time I get a chance. The way it works is that we are using mod 216, so I selected two numbers relatively prime to 65536 (so any odd number, in this case). There are a few other conditions dealing with the Euler phi function, I believe, but I got lucky with the numbers I chose, so I didn't need to look it up. If you check, I chose prime numbers, specifically, because I figured those would give me the best shot. If you choose the wrong values, you will get cycles of 2n. I am not sure how familiar you are with group theory, but essentially, you will be creating sub groups and the order (size) of a subgroup will always divide the order of the main group. So some values will make cycles of 32768, 16384, and other smaller powers of 2. (gah, there is so much cool theory behind this, but I don't have much time). EDIT: ooh, here is a useful routine Code: [Select] FindNumPages:;Inputs:; The app base page is loaded in MemBank1;Outputs:; c flag set if the field was found; nc means the app header subfield was not found; A is the number of app pages; B is 0; (HL) is the number of app pages ld hl,4000h ld bc,128 ld a,c or aFNPLoop: cpir ret po ret nz inc a cp (hl) jr z,$+5     dec a     jr FNPLoop     inc l     ld a,(hl)     scf     retI made that to be a faster alternative to using a bcall
« Last Edit: July 09, 2012, 08:24:53 am by Xeda112358 »

#### Runer112

• Moderator
• LV11 Super Veteran (Next: 3000)
• Posts: 2289
• Rating: +639/-31
##### Re: ASM Optimized routines
« Reply #50 on: July 09, 2012, 02:45:26 pm »
Optimized a bit. The largest optimization was removing end checking, because it's impossible for an application not to have a number of pages field. I also optimized the search loop by rearranging it a bit to remove the unconditional jump.

Code: [Select]
FindNumPages:;Inputs:;     The app base page is loaded in MemBank1;Outputs:;     A, (HL) is the number of app pages     ld hl,4000h     ld a,81h     ld c,aFNPLoop:     dec a     cpir     inc a     cp (hl)     jr nz,FNPLoop     inc l     ld a,(hl)     ret
« Last Edit: July 09, 2012, 02:46:21 pm by Runer112 »

#### thepenguin77

• z80 Assembly Master
• LV10 31337 u53r (Next: 2000)
• Posts: 1591
• Rating: +823/-5
• The game in my avatar is bit.ly/p0zPWu
##### Re: ASM Optimized routines
« Reply #51 on: July 09, 2012, 02:46:04 pm »
There are actually rare cases where that routine could fail. Of course I would assume it will work 99.9% of apps, if someone changed the order of the header and put the time stamp key in front of the number of pages, it could theoretically contain $80,$81.

But, now that I think about it, this is so rare that it will never happen.
zStart v1.3.013 9-20-2013
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
-Runer112

#### calc84maniac

• eZ80 Guru
• Coder Of Tomorrow
• LV11 Super Veteran (Next: 3000)
• Posts: 2898
• Rating: +467/-17
##### Re: ASM Optimized routines
« Reply #52 on: July 09, 2012, 04:54:29 pm »
Isn't that routine searching for $80,$81 anyway?

Edit: Oh, I see what you're saying. The time stamp data could contain $80,$81.
« Last Edit: July 09, 2012, 06:47:23 pm by calc84maniac »
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

#### NanoWar

• LV4 Regular (Next: 200)
• Posts: 140
• Rating: +18/-6
##### Re: ASM Optimized routines
« Reply #53 on: July 12, 2012, 06:02:41 pm »
Has anybody got a good rectangle function? It should use variable width by pixel, not byte... Here's my ugly code:
Code: [Select]