Author Topic: I don't know what's wring with this code D:  (Read 2835 times)

0 Members and 1 Guest are viewing this topic.

Offline Yeong

  • Not a bridge
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3739
  • Rating: +278/-12
  • Survivor of Apocalypse
    • View Profile
I don't know what's wring with this code D:
« on: October 30, 2011, 09:08:07 am »
In my opinion, this should work. After I compile it, it worked for while and it sharted to go haywire and crashed my calc. What did I do wrong?
Code: [Select]
.org userMem-2
.db $BB, $6D
   BCALL ClrLCDFull
   CALL INITCOOR
   LD HL,PIE
   BCALL PutS
LOOP:
   BCALL getKey
   CP kUp
   CALL Z,GOUP
   CP kDown
   CALL Z,GODOWN
   CP kRight
   CALL Z, GORIGHT
   CP kLeft
   CALL Z,GOLEFT
   CP kClear
   RET Z
   BCALL ClrLCDFull
   CALL INITCOOR
   LD HL,PIE
   BCALL PutS
   JR LOOP
PIE:
.db "pi_symbol",0
COOR:
.db 0,0
INITCOOR:
   LD HL,COOR
   LD A,(HL)
   LD (CURROW),A
   LD HL,COOR+1
   LD A,(HL)
   LD (CURCOL),A
   RET
GOUP:
   LD HL,COOR
   LD A,(HL)
   DEC A
   LD (HL),A
   RET
GODOWN:
   LD HL,COOR
   LD A,(HL)
   INC A
   LD (HL),A
   RET
GOLEFT
   LD HL,COOR+1
   LD A,(HL)
   DEC A
   LD (HL),A
   RET
GORIGHT:
   LD HL,COOR+1
   LD A,(HL)
   INC A
   LD (HL),A
   RET
Sig wipe!

Offline Keoni29

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2466
  • Rating: +291/-16
    • View Profile
    • My electronics projects at 8times8
Re: I don't know what's wring with this code D:
« Reply #1 on: October 30, 2011, 09:43:40 am »
In my opinion, this should work. After I compile it, it worked for while and it sharted to go haywire and crashed my calc. What did I do wrong?
Code: [Select]
.org userMem-2
.db $BB, $6D
   BCALL ClrLCDFull
   CALL INITCOOR
   LD HL,PIE
   BCALL PutS
LOOP:
   BCALL getKey
   CP kUp
   CALL Z,GOUP
   CP kDown
   CALL Z,GODOWN
   CP kRight
   CALL Z, GORIGHT
   CP kLeft
   CALL Z,GOLEFT
   CP kClear
   RET Z
   BCALL ClrLCDFull
   CALL INITCOOR
   LD HL,PIE
   BCALL PutS
   JR LOOP
PIE:
.db "pi_symbol",0
COOR:
.db 0,0
INITCOOR:
   LD HL,COOR
   LD A,(HL)
   LD (CURROW),A
   LD HL,COOR+1
   LD A,(HL)
   LD (CURCOL),A
   RET
GOUP:
   LD HL,COOR
   LD A,(HL)
   DEC A
   LD (HL),A
   RET
GODOWN:
   LD HL,COOR
   LD A,(HL)
   INC A
   LD (HL),A
   RET
GOLEFT
   LD HL,COOR+1
   LD A,(HL)
   DEC A
   LD (HL),A
   RET
GORIGHT:
   LD HL,COOR+1
   LD A,(HL)
   INC A
   LD (HL),A
   RET
You forgot a  :
There is supposed to be a  :  after GOLEFT
Even though I don't know shit about asm XD
If you like my work: why not give me an internet?








Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: I don't know what's wring with this code D:
« Reply #2 on: October 30, 2011, 09:57:35 am »
(note: we talked on IRC XD)
Here is a version that is a little more optimised. I could do another optimisation, but that would probably just cause some confusion XD And it only saves 2 bytes, anyway...
Code: [Select]
Loop:
     bcall(_ClrScrnFull)      ;Clears the homescreen
     ld hl,(Coor)
     ld (CurRow),hl
     ld hl,PIE
     bcall(_PutS)
     bcall(_getkey)
     ld hl,(Coor)           ;obtains the current cursor coordinates (l=row, h=col)
     ld d,h \ ld e,l
TestRightKey:
     dec a            ;if a was 1, it becomes 0 and sets the z flag
     jr nz,TestLeftKey
       inc d
TestLeftKey:
     dec a
     jr nz,TestUpKey
       dec d
TestUpKey:
     dec a
     jr nz,TestDownKey
       dec e
TestDownKey:
     dec a
     jr nz,TestClearKey
       inc e
TestClearKey:
;At this point, "a" has been decremented 4 times, so we test kClear-4
     cp kClear-4 \ ret z
CheckBounds:
     ld a,e \ and 7
     ld l,a
     ld a,d \ cp 8
     jr nc,NotOutOfXBound
       ld h,d
NotOutOfXBound
     ld (Coor),hl
     jr Loop
PIE:
.db "pi_symbol",0
Coor:
.dw 0

Offline Yeong

  • Not a bridge
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3739
  • Rating: +278/-12
  • Survivor of Apocalypse
    • View Profile
Re: I don't know what's wring with this code D:
« Reply #3 on: October 30, 2011, 10:21:14 am »
In my opinion, this should work. After I compile it, it worked for while and it sharted to go haywire and crashed my calc. What did I do wrong?
Code: [Select]
.org userMem-2
.db $BB, $6D
   BCALL ClrLCDFull
   CALL INITCOOR
   LD HL,PIE
   BCALL PutS
LOOP:
   BCALL getKey
   CP kUp
   CALL Z,GOUP
   CP kDown
   CALL Z,GODOWN
   CP kRight
   CALL Z, GORIGHT
   CP kLeft
   CALL Z,GOLEFT
   CP kClear
   RET Z
   BCALL ClrLCDFull
   CALL INITCOOR
   LD HL,PIE
   BCALL PutS
   JR LOOP
PIE:
.db "pi_symbol",0
COOR:
.db 0,0
INITCOOR:
   LD HL,COOR
   LD A,(HL)
   LD (CURROW),A
   LD HL,COOR+1
   LD A,(HL)
   LD (CURCOL),A
   RET
GOUP:
   LD HL,COOR
   LD A,(HL)
   DEC A
   LD (HL),A
   RET
GODOWN:
   LD HL,COOR
   LD A,(HL)
   INC A
   LD (HL),A
   RET
GOLEFT
   LD HL,COOR+1
   LD A,(HL)
   DEC A
   LD (HL),A
   RET
GORIGHT:
   LD HL,COOR+1
   LD A,(HL)
   INC A
   LD (HL),A
   RET
You forgot a  :
There is supposed to be a  :  after GOLEFT
Even though I don't know shit about asm XD
That was a typo <_<
I do have a colon after that because otherwise, Mimas won't let me compile

(note: we talked on IRC XD)
Here is a version that is a little more optimised. I could do another optimisation, but that would probably just cause some confusion XD And it only saves 2 bytes, anyway...
Code: [Select]
Loop:
     bcall(_ClrScrnFull)      ;Clears the homescreen
     ld hl,(Coor)
     ld (CurRow),hl
     ld hl,PIE
     bcall(_PutS)
     bcall(_getkey)
     ld hl,(Coor)           ;obtains the current cursor coordinates (l=row, h=col)
     ld d,h \ ld e,l
TestRightKey:
     dec a            ;if a was 1, it becomes 0 and sets the z flag
     jr nz,TestLeftKey
       inc d
TestLeftKey:
     dec a
     jr nz,TestUpKey
       dec d
TestUpKey:
     dec a
     jr nz,TestDownKey
       dec e
TestDownKey:
     dec a
     jr nz,TestClearKey
       inc e
TestClearKey:
;At this point, "a" has been decremented 4 times, so we test kClear-4
     cp kClear-4 \ ret z
CheckBounds:
     ld a,e \ and 7
     ld l,a
     ld a,d \ cp 8
     jr nc,NotOutOfXBound
       ld h,d
NotOutOfXBound
     ld (Coor),hl
     jr Loop
PIE:
.db "pi_symbol",0
Coor:
.dw 0
What is .dw?
Sig wipe!

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: I don't know what's wring with this code D:
« Reply #4 on: October 30, 2011, 11:19:03 am »
.dw sets a little-endian word (2 bytes) I don't know if Mimas supports that, if not, do .db 0,0

For example, .dw 5 is 0500 in memory

Offline NanoWar

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 140
  • Rating: +18/-6
    • View Profile
Re: I don't know what's wring with this code D:
« Reply #5 on: October 30, 2011, 11:29:47 am »

Offline chickendude

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 817
  • Rating: +90/-1
  • Pro-Riot Squad
    • View Profile
Re: I don't know what's wring with this code D:
« Reply #6 on: October 30, 2011, 03:42:39 pm »
Ok, so it's been a few months since i've touched z80, but i think you could do something like this:
Code: [Select]
cp 3 ;check if we are going horizontally or vertically, i might have this backwards
 jr c,skip
  inc hl
skip:
 rla
 rla
 jr nc,skip2 ;if no carry (ie. down or right was pressed)
 dec (hl)
 jr skip3
skip2:
 inc (hl)
skip3:
Here are your equates:
kDown .equ    04h
kUp .equ    03h
kLeft .equ    02h
kRight .equ    01h

10000000 right
01000000 left
11000000 up
00100000 down

after first shift we have:
00 r
10 l
10 u
01 d
which is more usable, as with r/d we need to increment and with l/u we need to decrement. Now another shift lets us use the carry flag. To do boundary checking, you could just use b instead of acting directly on hl:
Code: [Select]
ld b,1
 cp 3
 jr c,skip
  inc hl
skip:
 rla
 rla
 jr nc,skip2
 dec b
 dec b
skip2:
 ld a,(hl)
 add a,b
;boundary checking
 ld (hl),a
You'd probably also want to test that a>0 & a<5, or at least that a isn't 0. I'm not sure if the code i gave you will actually work as i just wrote it off the top of my head, but maybe it'll give you something to think about even if it's completely useless :)
EDIT: Oh, and of course you would ld hl,Coor beforehand (vs ld hl,(Coor)).
EDIT2: Ah! And one more thing, i'd check for kClear before the other tests.
EDIT3: I imagine you've already discussed it, but the problem with your original code is probably your calls. You check a key, change the value of "a",  then return to check for another key. You are almost certainly calling multiple labels at a time at each keypress (especially since many of the X/Y values match the getkey values)
« Last Edit: October 30, 2011, 03:51:27 pm by chickendude »