Omnimaga

Calculator Community => Major Community Projects => The Axe Parser Project => Topic started by: SamTebbs33 on January 22, 2014, 11:51:59 am

Title: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: SamTebbs33 on January 22, 2014, 11:51:59 am
Hi!

I would like to apply a bitwise left rotation to the each of the two bytes of a number and found that there was a command in asm that does this.

Is it possible to execute this command (RLC) from within the Axe syntax? If not, is there an asm program that I can execute to do this?

If none of the above are possible, does anyone have any knowledge of a routine that applies a left rotation to the first 8 bits ad then the last 8 bits of a two byte number?

Thanks!
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: Sorunome on January 22, 2014, 11:54:03 am
To awnser the first part of your question:
Quote from: Readme
Asm(HEX): Native assembly code written in hexadecimal is inserted at the current position.
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: Matrefeytontias on January 22, 2014, 01:17:14 pm
Shifting left is literally *2. I can't see any point to using rotation instructions since you don't have control on the carry flag anyway.
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: fb39ca4 on January 22, 2014, 01:40:54 pm
Shifting left always inserts zeros, while rotating left inserts the same bit that was removed on the other side.
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: Matrefeytontias on January 22, 2014, 01:42:05 pm
-.- Yes, and this removed bit goes in the carry flag.
I can't see any point to using rotation instructions since you don't have control on the carry flag anyway.
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: Iambian on January 22, 2014, 01:51:46 pm
-.- Yes, and this removed bit goes in the carry flag.
I can't see any point to using rotation instructions since you don't have control on the carry flag anyway.
Aren't you thinking of RR/RL? Those instructions are a 9-bit shift/rotate, the 9th bit being the carry flag.
The instruction mentioned in the OP is RLC, which while it does do stuff with the carry flag, it does not by any means use it for anything. It is an 8-bit rotation and the bit that leaves appears immediately on the other side.

Now, extending this idea into 16 bits is a bit ... trickier, but can be accomplished with a short ASM routine, none of which ever leaves the routine, so the whole issue about carry is moot.

Code: [Select]
;Left rotate HL circular, cheap method
 ld a,L
 add hl,hl
 rla
 ld L,a

;Right rotate HL circular

 ld a,h
 rr h
 rr L
 rra
 ld h,a
It's up to you to convert that to hex, tho.

EDIT: Problem with the first routine after the next post was posted. It should've been an RLA.
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: Matrefeytontias on January 22, 2014, 01:54:09 pm
Hum actually you're right. Here's the first routine : Asm(7D291F6F) ; and the second one : Asm(7CCB1CCB1D1F67).
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: Iambian on January 22, 2014, 01:59:58 pm
EDIT: Mat isn't here to edit his post. There was a problem in what I posted before and the converted first routine should have actually used RLA instead of RRA. The corrected routine is thus: Asm(7D29176F)

Many apologies
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: calc84maniac on January 22, 2014, 02:10:31 pm
I'm still not sure whether he wants each byte to be rotated independently or not. It's not really clear. Should the entire 16-bit number be rotated, or just within each byte?
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: Iambian on January 22, 2014, 02:13:19 pm
If individually, the answer becomes fairly trivial. That would be an RLC/RRC ... something on each byte. As a combined unit, you'll have to have a loop of RL/RR's traversing the area that needs to be rotated with the very first byte of it being saved someplace so when the loop ends, you can rotate the trailing bit into that and reload that as the first bit to simulate a complete rotation.
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: calc84maniac on January 22, 2014, 02:15:47 pm
The right rotate can be simplified as:
Code: [Select]
ld a,l
rra
rr h
rr l
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: SamTebbs33 on January 22, 2014, 04:10:32 pm
Thanks for the input guys. @calc84maniac Sorry for not being clear, I would like to rotate each byte left and then add them together. So for example, using 1011011010010110, I'd like to rotate 0000000010010110 (255 and 1011011010010110), then rotate 1011011000000000 (65280 and 1011011010010110) and then add them together.

Is Asm(7D291F6F) still the correct routine for a left rotation?

Since I haven't learnt about asm yet (I'm planning to), what would I input to the routine and how?

Thanks!
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: Iambian on January 22, 2014, 04:22:20 pm
The routine for a 16-bit left rotation is:Asm(7D29176F)
The routine for a 16-bit right rotation is: Asm(7D1FCB1CCB1D) (courtesy of calc84maniac)

By "add them together", do you mean the arithmetic operation "add" or the combining of the two binary strings (concatenate) ? From what you're saying, you want each to be rotated individually in the absence of each other? If that's the case, then you'll want something completely different than what we've supplied.

You'll want the individual opcodes for RLC L / RRC L / RLC H / RRC H. For further information, try looking at this opcode reference: http://www.ticalc.org/pub/text/z80/opcodes.txt

What is it that you're trying to accomplish?
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: SamTebbs33 on January 23, 2014, 02:45:52 am

By "add them together", do you mean the arithmetic operation "add" or the combining of the two binary strings (concatenate) ? From what you're saying, you want each to be rotated individually in the absence of each other? If that's the case, then you'll want something completely different than what we've supplied.

What is it that you're trying to accomplish?

Yes, that's what I would like to do I'd like to rotate the first 8 bits of the two byte number by 1 bit and then rotate the last 8 bits of the two byte number (adding zeros for the first 8 bits) and then add (+) the two rotations together.

What I'm doing is making a remake of an old game from the 80's. This game used procedural generation to generate 8 galaxies and it got the generations seeds for each one by rotating each starting seed by one bit, on the 8th rotation it would return to the fist seed, hence sending you back t the first galaxy.
For clarification, the first value of 1 of the 3 seeds is 231114 and the first rotation will set it to 46228.

I have some axe code that works on my calculator (below), but it doesn't function the same in SC3 on Cemetech.net

Code: [Select]
.A
23114->A. Seed 1
584->B.Seed 2
46931->C.Seed 3

For(E,0,8)
ClrHome
A->D
sub(ROT).D is input, W is output
W->A
B->D
sub(ROT)
W->B
C->D
sub(ROT)
W->C
Disp A>Dec,[i],B>Dec,[i],C>Dec
Pause 10400
End
Return

Lbl ROT
(D and 255)->U.Getting the first 8 bits
(D and 65280)->V.Getting the last 8 bits
(2*U) or (U/e^(7))->W
(2*V) or (V/e^(15))+W->W
Return
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: Hayleia on January 23, 2014, 10:16:18 am
(D and 255)->U.Getting the first 8 bits
(D and 65280)->V.Getting the last 8 bits
I fear those don't work since "and" is a 8-bit operation IIRC. To do a 16-bit operation you need the "." (which is somewhere in Catalog, not the "." you'd put between 3.14). But since you want to take the 8 first bits and the 8 last bits, you can also do {°D} and {°D+1}.

I have some axe code that works on my calculator (below), but it doesn't function the same in SC3 on Cemetech.net
That doesn't really surprise me. SC3 is pretty new and SC2 didn't support Axe. I suggest you to use TokenIDE instead.
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: SamTebbs33 on January 23, 2014, 10:53:52 am
(D and 255)->U.Getting the first 8 bits
(D and 65280)->V.Getting the last 8 bits
I fear those don't work since "and" is a 8-bit operation IIRC. To do a 16-bit operation you need the "." (which is somewhere in Catalog, not the "." you'd put between 3.14). But since you want to take the 8 first bits and the 8 last bits, you can also do {°D} and {°D+1}.

I have some axe code that works on my calculator (below), but it doesn't function the same in SC3 on Cemetech.net
That doesn't really surprise me. SC3 is pretty new and SC2 didn't support Axe. I suggest you to use TokenIDE instead.

Wow, I can't believe I forgot that and is an 8 bit operation! thanks!

I would use TokenIDE but it only works on Windows :/ I have a mac.

EDIT: What does the ° symbol do?
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: Hayleia on January 23, 2014, 10:59:18 am
Basically, the ° symbol gets the pointer to the following variable. So °D is the pointer to D. So {°D} is D's first byte and {°D+1} is D's second byte.
Note that you can also do the contrary, like L5→°MyVar, and then you have a variable in L5 (and L5+1, it's 2 bytes). And you can do 1→MyVar, MyVar++, etc.
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: SamTebbs33 on January 23, 2014, 11:22:08 am
Aah, I see.

Does anyone know of an IDE that supports Axe and works on a mac?
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: Xeda112358 on January 23, 2014, 11:30:19 am
Okay, say your 16-bit number has digits "abcdefghijklmnop"
If you want to rotate the whole thing left, "bcdefghijklmnopa" I would do this which averages a half cycle faster for the same size:
Code: [Select]
Asm(2930012C)
In assembly:
    add hl,hl
    jr nc,$+3
    inc l

If you want to rotate the bytes separately, bcdefgha jklmnopi and then add them together to get the addition of the two 8-bit numbers: bcdefgha+jklmnopi : back into Axe's "Ans"
Code: [Select]
Asm(7C07CB05856F2600CB14)
    ld a,h
    rlca
    rlc l
    add a,l
    ld l,a
    ld h,0
    rl h
If you just want the 16-bit number bcdefghajklmnopi:
Code: [Select]
Asm(CB04CB05)
    rlc h
    rlc l

The right rotations for all of those:
Code: [Select]
;abcdefghijklmnop->pabcdefghijklmno
Asm(7C0FCB1DCB1C)
    ld a,h
    rrca
    rr l
    rr h
Code: [Select]
;abcdefghijklmnop->habcdefg+pijklmno
Asm(7C0FCB0D856F2600CB14)
    ld a,h
    rrca
    rrc l
    add a,l
    ld l,a
    ld h,0
    rl h
Code: [Select]
;abcdefghijklmnop->habcdefgpijklmno
Asm(CB0CCB0D)
    rrc h
    rrc l

I hope I got these correct.
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: SamTebbs33 on January 24, 2014, 02:37:46 pm
Thanks!

How would I input the numbers I want to rotate?
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: Hayleia on January 25, 2014, 02:13:28 am
(You double posted by accident, you may want to delete one of your posts).

That depends on how you want your input routine: easy to code or easy to use ? :P
Easy to code is basically "press up and down until the number is the one you want, then press enter".
Easy to use is "to get 123, press 1 then 2 then 3".

The latter is easily codable but hard to get easy to use, you have to make keys respond at the right pace, not too slow, not too fast, you have to support the "backspace" key, etc.

Hence why I say the first one is a lot easier to do :P
It is just that:

.AA
0->A
While 1
 Text(0,,A+getKey(4)-getKey(1)->A>Dec)
 Pause 200
EndIf getKey(54)
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: SamTebbs33 on January 25, 2014, 05:09:56 am
Oh no, I meant how would I send the numbers to the asm routine :p sorry for not being clear.

For example, would I set A to the number to rotate?
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: MGOS on January 25, 2014, 05:18:32 am
Just put them in HL (Axe's Ans) - that is usually the last value you worked with. Just write the expression (or the single varibale) in the line before the routine. The same way (with the sto-> operator) you can retrieve the value returned by the routine.
Code: [Select]
:A
:Asm(STUFF)
:->A
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: Xeda112358 on January 25, 2014, 08:29:46 pm
Oh no, I meant how would I send the numbers to the asm routine :p sorry for not being clear.

For example, would I set A to the number to rotate?
What MGOS said is indeed the way to pass the value to the routine:
Just put them in HL (Axe's Ans) - that is usually the last value you worked with. Just write the expression (or the single varibale) in the line before the routine. The same way (with the sto-> operator) you can retrieve the value returned by the routine.
Code: [Select]
:A
:Asm(STUFF)
:->A
Title: Re: Is it possible to execute asm within Axe/Is there a bit rotation asm program?
Post by: SamTebbs33 on January 26, 2014, 03:01:54 pm
Thanks guys! I will be sure to our these to good use