The shoutbox is currently out of service. Join us on Discord instead.
You can help CodeWalrus stay online by donating here.

[TI-84+CE] ICE Compiler

Started by PT_, March 25, 2016, 08:14:17 am

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

DJ Omnimaga

Yeah my main worry about a non-ASM OS would be the extremely large size. :P But of course if your language is designed to work well on the CE then the size wouldn't be that bad. I think I'll stick to making games, though. :P

PT_

Hey guys, I'm struggling with a code snippet in ASM for representing the BASIC "xor". The output should in register A and is 1 if A xor B is true, and 0 if false. Of course, it's not super hard, but my code is yet 18 bytes and 80cc, and I'm 99% sure it can be shorter/faster. Can anyone give it a try? Thanks! :)

My code:
ld a, (address_A)
sub a, 1
sbc a, a
inc a
ld b, a
ld a, (address_B)
sub a, 1
sbc a, a
inc a
xor b


Note: Just one simple "xor (hl)" or so doesn't work :(

DJ Omnimaga

* DJ Omnimaga wonders if @Cumred_Snektron might be able to help since he's also working on a language? I can't help, though

Snektron

@PT_  what about

ld a, (Address_A)
cp (Address_B)
ld a, 0
jp nz, _
inc a
_:
Legends say if you spam more than DJ Omnimaga, you will become a walrus...


JWinslow23

I know nothing about z80 asm, but can't you just do an xor, but convert a nonzero output to 1? Like maybe:

Load (HL) with (A xor B)
Is (HL) != 0 ?
If so, then Load (HL) with 1
Return (HL)


That's just pseudocode (I don't know asm), but I'm thinking that should work.

MateoConLechuga

May 26, 2016, 04:42:43 am #65 Last Edit: May 26, 2016, 04:53:49 am by MateoConLechuga
The best that I can do is by having varB directly after varA, saving 2 bytes.

ld hl,varA
ld a, (hl)
sub a,1
sbc a,a
inc hl
ld b,a
ld a, (hl)
add a,-1
sbc a,a
xor a,b
inc a

PT_

May 26, 2016, 07:12:16 am #66 Last Edit: May 26, 2016, 07:14:27 am by PT_
My answer to all of you is no ;) For Cumred and Ivoah: what if A=1 and B=2? Xor would give 3 which is false. Mateo: A and B was only as an example, it can be any variable I (the user) want. It is useful though in some cases.

Snektron

If A = 1 and B = 2 woul;d give you


ld a, (Address_A)
cp (Address_B) ; 1 - 2 = -1, so nz
ld a, 0
jp nz, _
inc a     ; set a to 1
_:
; outcome: a = 1


Is that not what you want? else im not entirely sure what you want
Legends say if you spam more than DJ Omnimaga, you will become a walrus...


PT_

In your case: what if both A and B are 1? Then cp a, b will be zero, en thus (according to you) the outcome is 1, while it should be 0.  But if A and B > 0 but not equal, the output is 0, which is right. So your code fails if A=B

Snektron

Eh, i think i used the wrong flag, jp nz, _  should be jp z, _. (It was late yesterday)
now if A != B cp 0 will reset z (so nz) and it will not jump past inc a, so the end result is 1 in a. If A == B then cp 0 will set Z, jumping past inc and the outcome would be a = 0.
Legends say if you spam more than DJ Omnimaga, you will become a walrus...


JWinslow23

May 27, 2016, 01:47:32 am #70 Last Edit: May 27, 2016, 01:49:05 am by JWinslow23
Quote from: PT_ on May 26, 2016, 07:12:16 am
My answer to all of you is no ;) For Cumred and Ivoah: what if A=1 and B=2? Xor would give 3 which is false. Mateo: A and B was only as an example, it can be any variable I (the user) want. It is useful though in some cases.

Well, then can't you just turn each input into a 1 or 0 and do it like that? Like (pseudocode):
Is A != 0?
If so, then load A with 1
Is B != 0?
If so, then load B with 1
Load (HL) with (A xor B)
Return (HL)

MateoConLechuga

Quote from: PT_ on May 26, 2016, 07:12:16 am
My answer to all of you is no ;) For Cumred and Ivoah: what if A=1 and B=2? Xor would give 3 which is false. Mateo: A and B was only as an example, it can be any variable I (the user) want. It is useful though in some cases.

Well, fine, the best I can save is just 1 byte then :P

ld a, (varA)
sub a,1
sbc a,a
ld b,a
ld a, (varB)
add a,-1
sbc a,a
xor a,b
inc a

Snektron

I think you can save another one by replacing add a, -1 with dec a ;)
Unless that add is intended to set the carry flag at the same time which it probably is
Legends say if you spam more than DJ Omnimaga, you will become a walrus...


PT_

I'm very happy to say that parsing a mathematical expression is almost done! :)
It now 'chains' operators, i.e. A*B+3, instead of the seperate routines for A*B and A+3 for example. Out of the 14 booleans/operators (+ - * / or xor and -> => <= > < = !=) I've finished 10. I only need >= <= > < to do, and after that, I'm ready with parsing such string (yet without functions). I haven't implemented auto-opt yet, but I will definitely do. Here is an example of what it can do:
String = A+4*B/(1-C)+3
Output = ld a, ($D05301)    ; B
add a, a
add a, a
push af
ld a, 1
ld hl, $D05302  ; C
sub a, (hl)
pop hl
ld l, 1
mlt hl
call _DivHLByA
ld a, l
ld hl, $D05300     ; A
add a, (hl)
add a, 3
ret
The only good optimization I see is replacing the "push af" with "ld h, a" and remove the "pop hl".

MateoConLechuga

May 28, 2016, 09:11:27 pm #74 Last Edit: May 28, 2016, 09:15:29 pm by MateoConLechuga
Quote from: PT_ on May 28, 2016, 08:29:16 am
I'm very happy to say that parsing a mathematical expression is almost done! :)
It now 'chains' operators, i.e. A*B+3, instead of the seperate routines for A*B and A+3 for example. Out of the 14 booleans/operators (+ - * / or xor and -> => <= > < = !=) I've finished 10. I only need >= <= > < to do, and after that, I'm ready with parsing such string (yet without functions). I haven't implemented auto-opt yet, but I will definitely do. Here is an example of what it can do:
String = A+4*B/(1-C)+3
Output = ld a, ($D05301)    ; B
add a, a
add a, a
push af
ld a, 1
ld hl, $D05302  ; C
sub a, (hl)
pop hl
ld l, 1
mlt hl
call _DivHLByA
ld a, l
ld hl, $D05300     ; A
add a, (hl)
add a, 3
ret
The only good optimization I see is replacing the "push af" with "ld h, a" and remove the "pop hl".


ld ix,$D05300
ld a,(ix+1)    ; B
add a, a
add a, a
sbc hl,hl
ld l,a
ld a,1
sub a,(ix+2) ; C
call _Div16By8 ; this changed names in the latest equate file
ld a, l
add a,(ix) ; A
add a,3
ret

Optimized by using an offset pointer sort of like a stack frame used in C. Probably not applicable, but meh, it's an idea.

Powered by EzPortal