Author Topic: Whats wrong with my dividing script? ASM TI 83/84+  (Read 5492 times)

0 Members and 1 Guest are viewing this topic.

Offline Jerros

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 137
  • Rating: +9/-0
    • View Profile
Whats wrong with my dividing script? ASM TI 83/84+
« on: May 09, 2010, 11:23:32 am »
What I want the next piece of scrip to do is:
-Increase (score) by 1
-Divide (score)/16 (lets just call the result "P")
-I only care about WHOLE numbers, I don't need the remainder.
-Do: 63-P (which will result, lets say: "Q")
-Load Q into (scoreMeter)

Code: [Select]
LD A, (Score)
INC A
LD (Score), A
LD HL, (score)
ADD HL, HL
ADD HL, HL
ADD HL, HL
ADD HL, HL
LD DE, $100
ADD HL, DE
LD A, H
NEG
LD B, 63
ADD A, B
LD (ScoreMeter), A

The idea is that there's a meter to show you you're score.
The value in (scoremeter) will be used as an Y-coordinate for the sprite of the meter.
But the above script doesnt really work (which you of course find obvious n_n).

Any help would be apreciated!
Thank you in advance.


79% of all statistics are made up randomly.

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: Whats wrong with my dividing script? ASM TI 83/84+
« Reply #1 on: May 09, 2010, 12:17:54 pm »
"Score" and "score" are not the same variable right?  Because you when you inc the score, you're only inc-ing the low byte.  So if your score was 255, then the inc would make it zero again.  Do this instead:

Code: [Select]
LD HL, (Score)
INC HL
LD (Score),HL

And then the score is already in HL for the rest of it.  On the other hand, if your score is a 1 byte variable, you never want to load it into HL directly becasue the high byte will be random.  Do this instead:

Code: [Select]
LD A, (Score)
INC A
LD (Score), A
LD H,0
LD L,A

Also, when you do those add hl,hl that multiples it by 2, it doesn't divide by 2.  If you want to divide by 2, you can do a bunch of these instead:

Code: [Select]
SRL H
RR L

This divides HL by 2.
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline Jerros

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 137
  • Rating: +9/-0
    • View Profile
Re: Whats wrong with my dividing script? ASM TI 83/84+
« Reply #2 on: May 10, 2010, 01:22:13 am »
Ok, I'll try that, thanks.
Oh, and "Score" and "score" are the same variable, just a typo from me >.<
The Idea was that ny multiplying, I would get the Division into H:

Code: [Select]
    LD    HL, (Score)      ; Score holds any number from 0 to 992 (16*62)

; 256 ÷ 16 = 16, find (Score) × 16
; Lets say (score) = 139, so I want 8 as my answer:
    ADD    HL, HL    ; HL = $0116
    ADD    HL, HL    ; HL = $022C
    ADD    HL, HL    ; HL = $0458
    ADD    HL, HL    ; HL = $08B0      ; Tadaa! H holds 8 right? which is 139/16!

LD DE, $100
ADD HL, DE       ; HL = $09B0
LD A, H          ; A = 9
NEG                       ; A = -9
LD B, 63
ADD A, B         ; A = 54
LD (ScoreMeter), A      ; Scoremeter = 54! Exept that it isn't for some reason...

Somewhere's a flaw, but I dont see it...
Anyway, I'll try your code, it seems shorter, faster and thus better, so if that works it's not really important why my script doesnt work.
Thanks again Quigibo, this is the 3rd time already you've helped me  ;D


79% of all statistics are made up randomly.

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: Whats wrong with my dividing script? ASM TI 83/84+
« Reply #3 on: May 10, 2010, 02:12:03 am »
Yeah, that division will work as long as HL is in the range 0 - 4095.  You didn't post a range earlier so I was unsure if the problem you were getting was becasue you were outside this range.  Here, try this:

Code: [Select]
LD HL,(Score)
INC HL
LD (Score),HL
ADD HL,HL
ADD HL,HL
ADD HL,HL
ADD HL,HL
LD A,63
SUB H
LD (ScoreMeter),A

I am assuming ScoreMeter is a one byte variable here since it should only hold values between 0-64 correct?  Which also means you should never load it into HL directly anywhere in your code.  If you need it in HL at some point, you have to do this:
Code: [Select]
LD A,(ScoreMeter)
LD H,0
LD L,A
I suspect this might be the problem part.
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline Jerros

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 137
  • Rating: +9/-0
    • View Profile
Re: Whats wrong with my dividing script? ASM TI 83/84+
« Reply #4 on: May 11, 2010, 01:34:18 am »
Thanks, there should be nothing wrong with the division now, so (Scoremeter) should hold 63-((score)/16).
This means that when the score is
0-15, (scoremeter) holds 63
16-31, (scoremeter) holds 62
32-47, (scoremeter) holds 61
ect.
This still isn't working, but at least now the problem isn't this part of the script, but I still can't spot anything why my script still isnt working...
There's 2 things not workingin my script, this and something else, so there might be a connection.
I don't want to post the whole thing, since I cant ask from anyone to quickly correct quite a big piece of code.

Anyway, thanks for your help.
I hope I can figure things out myself, but otherwhise I might need your help once more >.<
My game is nearly finished, and it hurts to see things not working properly when I'm so close to the finish line...
So thanks for helping me with the division part!


79% of all statistics are made up randomly.

Offline tr1p1ea

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 647
  • Rating: +110/-0
    • View Profile
Re: Whats wrong with my dividing script? ASM TI 83/84+
« Reply #5 on: May 11, 2010, 04:48:34 am »
Is 'score' only 1 byte?

You could try using RLD sorta like:

Code: [Select]
    ld hl,score
    inc (hl)
    ld e,(hl)
    rld
    ld (hl),e
    and %00001111
    sub 63
    neg
    ld (scoreMeter),a

Maybe?
"My world is Black & White. But if I blink fast enough, I see it in Grayscale."


Offline Jerros

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 137
  • Rating: +9/-0
    • View Profile
Re: Whats wrong with my dividing script? ASM TI 83/84+
« Reply #6 on: May 11, 2010, 06:45:00 am »
Thanks for your reply, tr1p1ea, but I stick with waht Quigibo told me.
That looks like it works fine, so my problem must be somewhere else.

Can't I delete my topic once I've had my answer? :O


79% of all statistics are made up randomly.

Offline tr1p1ea

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 647
  • Rating: +110/-0
    • View Profile
Re: Whats wrong with my dividing script? ASM TI 83/84+
« Reply #7 on: May 11, 2010, 07:31:24 am »
Maybe you could post more of your code if you're unsure where the problem lies? We will do our best to help you out any way we can.

Also i wouldnt recommend deleting the topic since it could prove helpful to others in the future.
"My world is Black & White. But if I blink fast enough, I see it in Grayscale."


Offline Jerros

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 137
  • Rating: +9/-0
    • View Profile
Re: Whats wrong with my dividing script? ASM TI 83/84+
« Reply #8 on: May 11, 2010, 08:56:33 am »
Ok, good reason not to delete ^^
I found WHAT is making the lot crash, but I'm really really puzzled on WHY its making things crash.
Heres the thing:
(this is just a small piece, there's more but I dont want to bother you with it unless you think it might be usefull to post)

Code: [Select]

             CALL gamescreen
CALL newsprr1n1
LD A, 9
LD B, A
tbdn1:
CALL hidemoveshowncount
djnz tbdn1
CALL newsprr2n2
LD A, 9
LD B, A
tbdn2:
CALL hidemoveshowncount
djnz tbdn2
CALL newsprr3n3
LD A, 9
LD B, A
tbdn3:
CALL hidemoveshowncount
djnz       tbdn3
ect. ect. ect. ect. untill:
CALL newsprr4n14
LD A, 9
LD B, A
tbdn14:
CALL hidemoveshowncount
djnz tbdn14
CALL newsprr5n15
LD A, 9
LD B, A
tbdn15:
CALL hidemoveshowncount
djnz tbdn15
CALL newsprr1n16       ; THIS is where it crashes. Works fine up to this point. It doesnt matter what number I use, newsprr@@n$$ (with @@ being a number between 1-5 and $$ is 1-16) wil ALWAYS result in ram-cleared.

To help you help me, the "CALL   newsprr@@n$$" (with @@ being a number between 1-5 and $$ is 1-16) is this:

Code: [Select]
newsprr@@n$$:
CALL newrow@@call    <-- here
CALL newspr$$call ; THIS doesnt ever get executed, so the problem must be   ^
CALL putnewcall
ret

The error must be in    "CALL   newrow@@call", because If I remove the rest, the script still fails:

Code: [Select]
tbdn15:
CALL hidemoveshowncount
djnz tbdn15
JP thebluedanubescrn  ; WORKS! it jumps and all is OK.


tbdn15:
CALL hidemoveshowncount
djnz tbdn15
CALL newrow@@call ; FAILS, the jump doesnt get executed and the program crashes.
JP thebluedanubescrn

So what's in "newrow@@call" ?
This is:
(remember, @@ = 1 to 5, but it doesnt matter what I use, it always fails)

Code: [Select]
newrow@@call:
LD A, "insert any number here"
LD (xpos), A
ret

Now , lets remove the call So I can see what causes the trouble:

Code: [Select]
tbdn15:
CALL hidemoveshowncount
djnz tbdn15
LD A, "insert any number here" ; = CALL newrow@@call
LD (xpos), A ; = CALL newrow@@call
JP thebluedanubescrn ; Doesnt get executed, ram-reset here ^


The above fails, So I remove 1 line to close in the Problem:

Code: [Select]
tbdn15:
CALL hidemoveshowncount
djnz tbdn15
LD A, "insert any number here"
; Removed "LD (xpos), A"
JP thebluedanubescrn ; Still doesnt get executed, ram-resets.

So now we've got:

Code: [Select]
[code]
tbdn15:
CALL hidemoveshowncount
djnz tbdn15
JP thebluedanubescrn ; Works!

tbdn15:
CALL hidemoveshowncount
djnz tbdn15
LD A, "insert any number here" ; FAILS HERE
JP thebluedanubescrn ; doesnt get executed, ram-resets.

So the problem in the line --LD      A, "insert random number"---
WHY does loading something into A make my ram reset?
And, whats even stranger, WHY doesnt it fail the 15 times before that?

This is why I didnt post so many details earlier, i dont wanna bother anyone...
But still, ANY help would be greatly appreciated :P
It's just that I'm getting really close to finish the game, but I'm puzzled by this error...[/code]


79% of all statistics are made up randomly.

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: Whats wrong with my dividing script? ASM TI 83/84+
« Reply #9 on: May 11, 2010, 12:13:10 pm »
I don't recommend deleting the topic as tr1p1ea said, since it could be useful to others in the future.

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: Whats wrong with my dividing script? ASM TI 83/84+
« Reply #10 on: May 11, 2010, 01:05:16 pm »
I notice you're doing this a lot:

Code: [Select]
ld A,<some number>
ld B,A

You can load it directly instead.
Code: [Select]
ld B,<some number>
Secondly, you use djnz around a lot of subroutines.  Are you absolutely sure none of those subroutines are using the b register?  Because if any of them are, it will completely screw up the counting for the djnz.  If you use any bcalls in that routine, that also counts becasue bcalls generally mess up all the registers unless it specifically says it does not.

Code: [Select]
tbdn15:
CALL hidemoveshowncount
djnz tbdn15

If you can't avoid getting rid of b from the subroutine, you can save it before calling the subroutine and then restore it after with a push and pop.

Code: [Select]
tbdn15:
PUSH BC
CALL hidemoveshowncount
POP BC
djnz tbdn15
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline Jerros

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 137
  • Rating: +9/-0
    • View Profile
Re: Whats wrong with my dividing script? ASM TI 83/84+
« Reply #11 on: May 12, 2010, 01:25:53 am »
Loading directly into B is much smarter ofcourse, I do that everywhere.
I'm not sure why I didn't do that here :O
But you can't do:

   LD   B, (label)

Right?
That would still need the:

   Ld   A, label
   Ld   B, A

Apart from that, the    CALL   hidemoveshowncount    Started with:
Code: [Select]
hidemoveshowncount:
LD A, B
LD (counter), A
           ---code with lots of stuff, calls ect.---
LD A, (counter)
LD B, A
ret
But I'm using puch/pop now.
push/pop is smarter, thnx, that works perfectly fine.

Anyway, the main problem was:
Why did the 16th "   LD   A, *number*" caused a crash?
Do I have to post the big "hidemoveshowcount" routine for that?
It's quite large...

I suppose it doesnt really matter now that I'm avoiding that command and just load things directly into B, but I'm still curious, and it might error again.
I need to test some more things, but for now I'm avoiding the problem, rather then fixing it.

Oh, and one last question, is it possible to do something like:
Code: [Select]
djnz "CHANGING LABEL"
I mean having a Jump command to a different label each time it passes that line?

Once again, BIG Thank-You! to the people that helped me here (especcially Quigibo again n_n)
« Last Edit: May 12, 2010, 05:00:24 am by Jerros »


79% of all statistics are made up randomly.

Offline tr1p1ea

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 647
  • Rating: +110/-0
    • View Profile
Re: Whats wrong with my dividing script? ASM TI 83/84+
« Reply #12 on: May 14, 2010, 08:14:02 am »
Yes it is possible to change where a djnz will jump to ... but with djnz, that might be dangerous.

Since 'code' is simply made up of bytes in RAM, you can change them just as you would any other byte.
"My world is Black & White. But if I blink fast enough, I see it in Grayscale."