Square root of an integer number

Page 3/5
1 | 2 | | 4 | 5

By ARTRAG

Enlighted (6323)

ARTRAG's picture

16-12-2008, 14:54

My dope now is 1k WHACK
I want to understand and translate its maze generator, its line of sight way to unveil of the map and its elegance and compactness in the code.

I really regret to not having never studied 6502 before...

Anyone with knowledge of this processor willing to support in this adventure ?

By pitpan

Prophet (3131)

pitpan's picture

16-12-2008, 15:01

As I said, I *had* such knowledge, 'cause I coded a 6502 assembler for NES and a really buggy emulator. The worst part of translating such code is that the 6502 CPU has many addressing modes that had little relation with our good old Z80 CPU. I'm afraid that I cannot help you ATM (all knowledge forgotten, all spare time devoted). But hey, you've got my support: good luck!

By ARTRAG

Enlighted (6323)

ARTRAG's picture

16-12-2008, 19:47

In the end the final version is:

; in hl
; out a = sqrt(hl)

    ld  de,1
    xor  a
1:  sbc hl,de
    ret c
    inc de
    inc de
    inc a
    jp  1b

By wolf_

Ambassador_ (9789)

wolf_'s picture

16-12-2008, 19:54

Ok, so I'm an asm-n00b, but what's with the 'jp 1b' vs the label '1:' ?

By Edwin

Paragon (1182)

Edwin's picture

16-12-2008, 20:00

A way to avoid a jump to address $0001.

By dvik

Prophet (2200)

dvik's picture

16-12-2008, 20:12

Isn't it some kind of sjasm feature to do jump back and forward, like.

1:    nop
       jp      1f
       jp      1b
1:    nop

Personally I prefer labels that mean something, but in small routines like this I guess its quite obvious what label to jump to.

By wolf_

Ambassador_ (9789)

wolf_'s picture

16-12-2008, 20:36

mm, wouldn't a letter (like an 'L' for 'loop'), while being a local variable, be more practical and/or readable?

By sjoerd

Hero (593)

sjoerd's picture

16-12-2008, 23:54

In the end the final version is:

; in hl
; out a = sqrt(hl)

    ld  de,1
    xor a
1:  sbc hl,de
    ret c
    inc de
    inc e
    inc a
    jp  1b

Maybe Question

mm, wouldn't a letter (like an 'L' for 'loop'), while being a local variable, be more practical and/or readable?
No Tongue

This feature is from the time one had to come up with a unique global labelname for every loop and jump in sjasm, which resulted in lots of meaningless names like lus1 to lus674 in my code. So this helped a lot to make the code readable. For me anyway.

Personally I don't use it anymore, but I still re-use old code were it is used. Hmm, that code is over ten years old now Smile

By ARTRAG

Enlighted (6323)

ARTRAG's picture

17-12-2008, 15:53

sjoerd, I think your code works, as D can be incremented only when DE passes from even to odd.
Yes another small gain :-)
BTW
for square root of numbers larger than 128 or 255, i think that the other method is faster..

Anyone willing to measure the cycles ?
:-)

By GhostwriterP

Hero (527)

GhostwriterP's picture

18-12-2008, 20:00

You could still add a little compare test like eh:
if hl > 121 then hl = hl - 121 a=sqrt (121) de=a*2+1 --> routine
if hl > 256 then hl = hl - 256 a=sqrt (256) de=a*2+1 --> routine
...

Page 3/5
1 | 2 | | 4 | 5