• ASM Optimized routines 5 1
Currently:  

Author Topic: ASM Optimized routines  (Read 108155 times)

0 Members and 1 Guest are viewing this topic.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: ASM Optimized routines
« Reply #15 on: April 30, 2010, 09:25:11 am »
how much space would it take compared to the old routine?

Offline Galandros

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1140
  • Rating: +42/-10
    • View Profile
Re: ASM Optimized routines
« Reply #16 on: April 30, 2010, 01:15:29 pm »

  • Signed division by any nontrivial constant, other than 2, including negative numbers?
  • Modulus with any constant that is not a power of 2?
Constants need to be 16-bit?
Hobbing in calculator projects.

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: ASM Optimized routines
« Reply #17 on: April 30, 2010, 01:54:53 pm »
Those multiplications by 128 won't work. Multiplying 256*128 would give 0.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Galandros

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1140
  • Rating: +42/-10
    • View Profile
Re: ASM Optimized routines
« Reply #18 on: April 30, 2010, 02:23:29 pm »
Those multiplications by 128 won't work. Multiplying 256*128 would give 0.
True. I tested small numbers. Works up to 255.
At least gives a good a*128 routine.

EDIT:
I got one solution that works on all numbers that fits in hl, 7 bytes but faster than conventional way:
Code: [Select]
xor a ; resets carry and register
 rr h
 rr l   ; divide hl by 2
 rra
 ld h,l
 ld l,a ; multiply hl by 256 (moving low byte to high byte trick)
;8 bytes, 32 clocks
;conventional way is 7 bytes and 77 clocks
;it does in less than half the time with only 1 byte cost, substantial speed increase with only 1 byte cost

Axe Parser could improve with a option to make it optimize for speed instead of the size default.
« Last Edit: April 30, 2010, 03:02:58 pm by Galandros »
Hobbing in calculator projects.

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: ASM Optimized routines
« Reply #19 on: April 30, 2010, 03:47:57 pm »
Axe Parser could improve with a option to make it optimize for speed instead of the size default.
Yeah, I'm planning to do that eventually.

Actually, I found out my optimized signed division by 2 doesn't work, so that's up in the air too now.  Also, would multiplying by negative 2 be do-able in 6 bytes?  It can certainly be done in 7 so it seems possible maybe using some trick?

EDIT: also, is the daa instruction ever useful for optimizations? I can't think of how I would use it other than floating point math.
« Last Edit: April 30, 2010, 03:48:49 pm by Quigibo »
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: ASM Optimized routines
« Reply #20 on: April 30, 2010, 03:51:56 pm »
Axe Parser could improve with a option to make it optimize for speed instead of the size default.
Yeah, I'm planning to do that eventually.

Actually, I found out my optimized signed division by 2 doesn't work, so that's up in the air too now.  Also, would multiplying by negative 2 be do-able in 6 bytes?  It can certainly be done in 7 so it seems possible maybe using some trick?

EDIT: also, is the daa instruction ever useful for optimizations? I can't think of how I would use it other than floating point math.
Signed division by 2 is just sra h \ rr l. And I have only found daa useful (other than its intended use) when converting between hex digits and ASCII, I think.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Galandros

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1140
  • Rating: +42/-10
    • View Profile
Re: ASM Optimized routines
« Reply #21 on: April 30, 2010, 04:36:12 pm »
And I have only found daa useful (other than its intended use) when converting between hex digits and ASCII, I think.
Yes, it is true. Strange how it works...

; this code is not documented...
cp 10
ccf
adc a, 30h
daa

;but this I know that converts the low nibble to ASCII char
   and  $0F
   add  a,$90
   daa
   adc  a,$40
   daa
Hobbing in calculator projects.

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: ASM Optimized routines
« Reply #22 on: April 30, 2010, 08:31:55 pm »
Signed division by 2 is just sra h \ rr l.

But what about -1/2?  Shouldn't that give 0?  Because if you do that operation to %11111111 11111111 it remains -1.  Or does this routine always round up in magnitude instead of down?  It might throw some people off since it would be inconsistent with my regular unoptimized signed division routine.
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: ASM Optimized routines
« Reply #23 on: April 30, 2010, 08:56:24 pm »
True, most people in BASIC are used to rounding stuff down with Int(), like for example 1.1 or 1.9999 will be rounded down to 1. The decimal part is completly truncated. It makes things much easier IMHO when programming.  (assuming this is what you mean?)

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: ASM Optimized routines
« Reply #24 on: April 30, 2010, 09:12:31 pm »
Signed division by 2 is just sra h \ rr l.

But what about -1/2?  Shouldn't that give 0?  Because if you do that operation to %11111111 11111111 it remains -1.  Or does this routine always round up in magnitude instead of down?  It might throw some people off since it would be inconsistent with my regular unoptimized signed division routine.
It rounds down, just like the int() command that everyone is used to. int(-1/2)=-1. That's how it works.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: ASM Optimized routines
« Reply #25 on: April 30, 2010, 09:32:43 pm »
Ooooh, okay, then that means my regular signed division routine is wrong then x.x  I'll have to fix it somehow.

I'm just going to put a copy of it here in its current state if anyone sees any optimizations I can make.  I felt pretty ashamed when I made it because it feels like such a dirty work around.  There has to be some real algorithm that exists to perform signed division non-naively right?

Code: [Select]
;Divides HL by DE and stores to HL
SDiv:
ld a,h
add a,a
jr nc,$+9
xor a
sub l
ld l,a
sbc a,a
sub h
ld h,a
scf

rra
xor d
push af
bit 7,d
jr z,$+8
xor a
sub e
ld e,a
sbc a,a
sub d
ld d,a

call Divide

pop af
add a,a
ret nc

xor a
sub l
ld l,a
sbc a,a
sub h
ld h,a
ret
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: ASM Optimized routines
« Reply #26 on: April 30, 2010, 09:55:24 pm »
I was just searching around and it looks like your method is the generally-used one.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: ASM Optimized routines
« Reply #27 on: April 30, 2010, 10:09:03 pm »
Yeah, that's what the first 5 pages of Google said when I was looking, but I still have a feeling that there might be one that is rare or undiscovered that can do it faster.  I'm going to try experimenting.  Maybe I can use some SMC to sometimes add the quotient to the accumulator and sometimes subtract it depending on the sign of the numbers.

EDIT: Oooo! I just tried messing around with this!  It works!  I just need to work on a few details first.
« Last Edit: April 30, 2010, 10:33:36 pm by Quigibo »
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: ASM Optimized routines
« Reply #28 on: April 30, 2010, 10:37:57 pm »
I thought you were trying to avoid SMC, though.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: ASM Optimized routines
« Reply #29 on: April 30, 2010, 10:41:52 pm »
I am.  But I found I can actually use a bit from one of the registers instead to hold the state of the operation instead of using SMC or even an app flag if I run out of registers.
___Axe_Parser___
Today the calculator, tomorrow the world!