SP
This is an archived, read-only copy of the United-TI subforum , including posts and topic from May 2003 to April 2012. If you would like to discuss any of the topics in this forum, you can visit Cemetech's z80 & ez80 Assembly subforum. Some of these topics may also be directly-linked to active Cemetech topics. If you are a Cemetech member with a linked United-TI account, you can link United-TI topics here with your current Cemetech topics.

This forum is locked: you cannot post, reply to, or edit topics. Z80 & 68k Assembly => z80 & ez80 Assembly
SP
Author Message
darkstone knight


Advanced Member


Joined: 07 Sep 2008
Posts: 438

Posted: 18 Oct 2008 01:43:43 pm    Post subject:

is there anny way to save SP in a register? LD HL,SP doesnt work Neutral
Back to top
Ed H


Member


Joined: 30 Nov 2007
Posts: 138

Posted: 18 Oct 2008 01:53:23 pm    Post subject:

Maybe if you split that into two load instructions?
Back to top
darkstone knight


Advanced Member


Joined: 07 Sep 2008
Posts: 438

Posted: 18 Oct 2008 02:02:25 pm    Post subject:

nope -.-

i want to save sp so you dont have to clean the stack at the end of the program (saves space...and time, my programs stack usage is a mess)
Back to top
calc84maniac


Elite


Joined: 22 Jan 2007
Posts: 770

Posted: 18 Oct 2008 02:35:46 pm    Post subject:

Just do something like:

Code:
savesp .equ appbackupscreen
ld (savesp),sp
;...code...
;...more code...
ld sp,(savesp)
ret


Edit: Or, if you really want it in HL, do ld hl,0 \ add hl,sp. The other way around, ld sp,hl, is valid.


Last edited by Guest on 22 Jul 2010 12:30:37 pm; edited 1 time in total
Back to top
darkstone knight


Advanced Member


Joined: 07 Sep 2008
Posts: 438

Posted: 18 Oct 2008 02:43:07 pm    Post subject:

it does work

but wabbitemu still crashes when i exit my prog Cool
Back to top
Spencer


Advanced Newbie


Joined: 06 Nov 2005
Posts: 99

Posted: 18 Oct 2008 05:37:24 pm    Post subject:

In the long run you'll be much better off making sure your stack usage is correct. As for the way you're currently doing it, make sure you didn't change the value of IY. If you did, make sure to save and restore it like you did SP.
Back to top
darkstone knight


Advanced Member


Joined: 07 Sep 2008
Posts: 438

Posted: 19 Oct 2008 05:50:10 am    Post subject:

i probaly did someting wrong in storing data...

thanks annyway

btw, my stack usage is probaly correct, just to be sure the mistake isnt in my stack
Back to top
tr1p1ea


Elite


Joined: 03 Aug 2003
Posts: 870

Posted: 19 Oct 2008 06:44:04 am    Post subject:

You arent using interrupts at all?
Back to top
darkstone knight


Advanced Member


Joined: 07 Sep 2008
Posts: 438

Posted: 19 Oct 2008 08:42:19 am    Post subject:

no... its my first asm prog ever (check sig)
Back to top
magicdanw
pcGuru()


Calc Guru


Joined: 14 Feb 2007
Posts: 1110

Posted: 19 Oct 2008 01:45:28 pm    Post subject:

I don't really see how this can/should work. If you push data onto the stack, you must always pop it off. If you just reset the stack pointer without popping stuff off the stack, at the very least it'll cause a memory leak, and at most it'll crash the calculator.


You should never be messy about stack usage, especially the SYSTEM stack! If you're just storing data you want to get back later, use a safe area of ram, not the stack. It'll be much easier, trust me.
Back to top
brandonw


Advanced Member


Joined: 12 Jan 2007
Posts: 455

Posted: 19 Oct 2008 01:55:52 pm    Post subject:

magicdanw wrote:
I don't really see how this can/should work.  If you push data onto the stack, you must always pop it off.  If you just reset the stack pointer without popping stuff off the stack, at the very least it'll cause a memory leak, and at most it'll crash the calculator.


That's wrong. When you remove data off the stack, all you're doing is moving SP closer to the original value when you started the program, and it puts the values that were on the stack into registers. It doesn't matter if you put them into registers, all you need to do is have SP back at where it was.

Saving and restoring SP is not the proper solution to dealing with stack issues...you should figure out where you're going wrong in your code.
There are instances where you do want to restore the stack (the OS does this extensively when moving from one context to another), but not in your typical assembly program.
Back to top
darkstone knight


Advanced Member


Joined: 07 Sep 2008
Posts: 438

Posted: 19 Oct 2008 02:11:16 pm    Post subject:

it works now, turns out i POP'ed whoutout a PUSH first, eh....

edit: when the program exits, it stays at the graph screen... i want i to return to the home screen, how?


Last edited by Guest on 19 Oct 2008 02:12:38 pm; edited 1 time in total
Back to top
DarkerLine
ceci n'est pas une |


Super Elite (Last Title)


Joined: 04 Nov 2003
Posts: 8328

Posted: 19 Oct 2008 06:09:50 pm    Post subject:

Use bcall(_Disp) to display the home screen (i.e. the ASCII characters stored at (textShadow)). This will not clear the graph buffer, though, you'll have to do that yourself.

Last edited by Guest on 22 Jul 2010 12:30:15 pm; edited 1 time in total
Back to top
darkstone knight


Advanced Member


Joined: 07 Sep 2008
Posts: 438

Posted: 20 Oct 2008 04:06:08 pm    Post subject:

thanks :)

program is nearly done, but for some reason, it has memory leaks -.-
Back to top
magicdanw
pcGuru()


Calc Guru


Joined: 14 Feb 2007
Posts: 1110

Posted: 20 Oct 2008 04:11:12 pm    Post subject:

If you need help figuring out what's wrong, you can post code here, and we'll take a look at it.
Back to top
darkstone knight


Advanced Member


Joined: 07 Sep 2008
Posts: 438

Posted: 20 Oct 2008 04:38:17 pm    Post subject:

the code (first prog, again) kinda sucks

i promise i will make my next program better Smile

comments are dutch btw... most of em

to draw a "box" the box is simpely inverted


Code:
.nolist
#include "ti83plus.inc"
#include "ti83plusasm.inc"
#define    ProgStart    $9D95 -2
_GetKeyRetOff .EQU $500B
.list
.org    ProgStart
    .db    t2ByteTok, tAsmCmp

savesp  .equ  appbackupscreen
#define   yoffset   4;deze worden niet gebruikt.. maargoed
#define   xoffset   8              ;not used btw...

;BCALL(_runindicoff)
BCALL(_clrLCDfull)

 ld (savesp),SP

 LD   B,0
 LD   C,12*4
 LD   HL,plotsscreen
 ADD   HL,BC
 LD   C,8      ;this draws the field on the screen
horiz:
 CALL   lines
 DEC   C
 JR   Z,skip
 CALL   lines2
 JR   horiz
skip:

;
;
;

;making the user chose a level...


picklvl:
 CALL   b2b
 LD   C,0
 CALL   drawfield
getkeyfield:
 BCALL(_grbufcpy)
 LD  HL,2
 LD  (penrow),HL
 LD  HL,80
 LD   (pencol),HL
 LD  A,(level)
 bcall(_SetXXOP1)
 LD  A,3
 bcall(_dispOP1a)
 BCALL(_getkey)
 push   AF
 LD   C,0
 CALL   drawfield
 POP   AF
 cp   kENTER
 JR   Z,mainloopstart
 CP   kUP
 JR   Z,increase
 CP   kDOWN
 JR   Z,decrease
 CP   kCLEAR
 JP   Z,exit
 jr   getkeyfield



increase
 LD   A,(level)       
 CP   99
 JR   Z,picklvl
 INC   A
 LD   (level),A
 JR   picklvl

Decrease
 LD   A,(level)       
 CP   0
 JR   Z,picklvl
 DEC   A
 LD   (level),A
 JR   picklvl



;   x-------------------------------------------x
;   |          bit -> byte conversion   |
;   x-------------------------------------------x
b2b:
 LD   A,(level);level nummer ...
 LD   H,0
 LD   L,A
 LD   D,0
 LD   E,A
 ADD   HL,DE;maal 7
 ADD   HL,DE
 ADD   HL,DE
 ADD   HL,DE
 ADD   HL,DE
 ADD   HL,DE
 LD   DE,leveldata
 ADD   HL,DE
 EX   DE,HL;DE bevat nu het adres van compressed lvl (in theorie.....)

   ld  B,7; hier aantal bytes die in binair zijn uitgedrukt
   ld  HL,map
Bytebinairloop:
   ld  A,(DE)
   PUSH  BC
   ld  B,8;8 bits in een byte
Bitshiftloop:
   ld  (HL),0
   SLA  A
   JR  NC,skip0;als de carry nul is, laat de byte dan ook maar nul; als de carry 1 is, maak de byte 1
   INC  (HL)
skip0:
   INC  HL
   DJNZ  Bitshiftloop
   POP  BC
   INC  DE
   DJNZ  Bytebinairloop
 RET


;----------------------------------------


;   ______________________________________
;   |   field drawing routine   |
;   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
drawfield:
 LD   DE,map
 LD   B,7*8
 LD   HL,$0101
drawloop:
 LD   A,(DE)
 CP   1
 push   DE
 push   BC
 CALL   z,invert
 POP   BC
 POP   DE
 INC   DE
 INC   H
 LD   A,H
 CP   9
 CALL   Z,nextline
 DJNZ   drawloop  
 RET

nextline:
 LD   H,1
 INC   L
 RET



;     _____________________________
;     |  main loop  |
;     ***********************************
mainloopstart
 CALL   drawfield
;POP   HL
 LD   H,1;TOP LEFT CORNER
 LD   L,1


mainloop:
 PUSH   HL
 JR   wincheck
wincheckret:
 POP   HL
 PUSH   HL
 CALL   muis;plaatsen
 BCALL(_grbufcpy)
 BCALL(_getkeyretoff)
 POP   HL
 PUSH   AF
 CALL   muis;wissen
 POP   AF
 cp   kup
 jr   Z,keyup
 cp   kdown
 jr   Z,keydown
 cp   kleft
 jr   Z,keyleft
 cp   kright
 jr   Z,keyright
 CP   kCLEAR
 JP   Z,exit
 jr  invert2;bij al het andere: veranderen


;     _____________________________
;     |   wincheck  |
;     ************************************
wincheck:
 LD   HL,map-1
 LD   B,7*8+1
winloop:
 DEC   B;eerst controleren of b 0 is
 LD   A,B
 CP   0
 JR   Z,exit0;als b 0 is, heeft hij alles gecheckt en is hij geen 1 tegengekomen
 INC   HL
 LD   A,(HL);daarna kijken of de bit 1 is
 CP   1;als de bit 1 is, exiten naar main (RET)
 JR   Z,winloop  
 JR   wincheckret   

exit0:;dat houd in, exiten
  ;stack legen... prut sp
 POP   HL   
 JP   exit;<s>stack hoeft niet leeg omdat sp is opgeslagen</s>

 




;     _____________________________
;     |   key routines   |
;     ***********************************
  
;-------------up--------------
keyup:  
 LD   A,L   
 CP   1   
 JR   Z,fail1
 DEC   L   
fail1:
 JR   mainloop
;------------down----------
keydown:
 LD   A,L
 CP   7
 JR   Z,fail2
 INC   L
fail2:
 JR  mainloop
;---------------left-----------
keyleft:
 LD   A,H
 CP   1
 JR   Z,fail3
 DEC   H
fail3:
 JR  mainloop
;-------------right--------
keyright:
 LD   A,H
 CP   8
 JR   Z,fail4
 INC   H
fail4:
 JR  mainloop

;     ___________________________________________________________
;     |       main invert routine    (ik weet dat dit een slechte naam is..)    |
;     ************************************************************************
invert2:  ;extreem onoverzichtelijk, maar ach....
 PUSH   HL
 CALL   invert
 CALL   updatemap
 CALL   UP;   ___________
 CALL   LEFT;   |      <      |      |
 CALL   DOWN;   | V  |___ |  ^ |
 CALL   DOWN;   |      >      >     |
 CALL   RE;   ''''''''''''''''''''''''
 CALL   RE
 CALL   UP
 CALL   UP
 POP   HL
 JP   mainloop


;     x___________________________x
;     |   updatemap   |
;     ^^^^^^^^^^^^^^^^^^^^^^^^^^^


updatemap:;ik hoop dat hij bij 0 begint met tellen.... edit: niet dus...
 PUSH   DE;geen idee of er iets instaat..
 push   HL;hl moet natuurlijk niet gedelite worden...
 DEC   H
 DEC   L
 SLA   L;L=ycoord, maal 8 (is maximaal 7, dus er is nooit overflow)
 SLA   L
 SLA   L

 LD   A,H
 ADD   A,L
 LD   L,A
 LD   H,0

 LD   DE,map;40957---------------------------------------------------------------------
 ADD   HL,DE
 LD   A,(HL);lees de byte in map
 CP   1
 JR   Z,map1
 LD   (HL),1
 JR   map0
map1:
 LD   (HL),0
map0:
 POP   HL
 POP   DE
 RET

up:
 DEC   L
 LD   A,L
 CP   0
 RET   Z
 LD   A,H
 CP   9
 RET   Z
 CALL   updatemap
 CALL   invert
 RET

left:
 DEC   H
 LD   A,H
 CP   0
 RET   Z
 LD   A,L
 CP   0
 RET   Z
 CALL   updatemap
 CALL   invert
 RET

down:
 INC   L
 LD   A,L
 CP   8
 RET   Z
 LD   A,H
 CP   0
 RET   Z
 CALL   updatemap
 CALL   invert
 RET

re:
 INC   H
 LD   A,H
 CP   9
 RET   Z
 LD   A,L
 CP   8
 RET   Z
 CALL   updatemap
 CALL   invert
 RET
 

;     ______________________
;     |       invert box at (H,L)    |
;     ***************************

muis:
 LD   C,2
invert:  ;H=xpos L=ypos
 PUSH   HL
 DEC   H
 DEC   L
getbyte:   
 PUSH   HL;save x
 LD   H,0;DE=y
 ADD   HL,HL;mult. by 12, then by 8, and add 5*12
 ADD   HL,HL;4
 PUSH   HL
 POP   DE
 ADD   HL,HL;8
 ADD   HL,DE;12, and repeat
 ADD   HL,HL;2
 ADD   HL,HL;4
 ADD   HL,HL;8
 LD   DE,5*12
 ADD   HL,DE
 POP   DE
 LD   A,D
 LD   D,0
 LD   E,A
 ADD   HL,DE
 LD   DE,plotsscreen
 ADD   HL,DE
;------------------------correct byte is now in HL----------


 LD   B,7
c:
 LD   DE,data2
 LD   A,C
 CP   2
 JR   NZ,d
 INC   DE
 INC   DE

d:
 
 CALL   PUTXOR
 INC   DE
 CALL   PUTXOR
 INC   HL;10 times
 INC   HL
 INC   HL
 INC   HL
 INC   HL
 INC   HL
 INC   HL
 INC   HL
 INC   HL
 INC   HL
 DJNZ   c
 POP   HL
 LD   C,0
 RET

putXOR:
 LD   A,(DE)
 XOR   (HL)
 LD   (HL),A
 INC   HL
 RET



;     ________________________________
;     |   line drawing routine        |
;     ***************************************

lines:
 LD   B,7
 LD   DE,data
 CALL   put
 INC   DE
a:
 CALL   put
 DJNZ   a
 INC   DE
 CALL   put
 INC   HL
 INC   HL
 INC   HL
 RET

lines2:
 LD   B,7
 INC   DE
xxx:
 CALL   put
 CALL   put
 CALL   put
 CALL   put
 CALL   put
 CALL   put
 CALL   put
 CALL   put
 CALL   put
 INC   HL
 INC   HL
 INC   HL
 DJNZ   xxx
 RET


put:
 LD   A, (DE)
 LD   (HL),A
 INC   HL
 RET

win:
exit:
 ld sp,(savesp)
 RET


 LD   HL,map
 LD   B,7*8
exitloop:
 push hl
 LD   HL,0
 LD   (currow),HL
 LD   (curcol),HL
 pop hl
 push hl
 LD   A,(HL)
 LD   L,A
 LD   H,0
 BCALL(_DISPHL)
 BCALL(_GETKEY)
 pop hl
 inc hl
 DJNZ exitloop

 ld sp,(savesp)
 RET

;   _____________________________
;   |   game              |
;   |   data  |     
;   '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


level:
.db   0; 0<x<100

data:
.db   %00011111
.db   %11111111
.db   %11110000
.db   %00010000
data2:
.db   %00001111
.db   %11100000
.db   %00000100
.db   %01000000

map:  
.db   0,0,0,0,0,0,0,0
.db   0,0,0,0,0,0,0,0
.db   0,0,0,0,0,0,0,0  
.db   0,0,0,0,0,0,0,0  
.db   0,0,0,0,0,0,0,0  
.db   0,0,0,0,0,0,0,0
.db   0,0,0,0,0,0,0,0  

leveldata:
;data of levels, 7 bytes per level

.end
.end


data for levels (if you want to test the game


Code:


leveldata:
.db  %11111111;helemaal zwart
.db  %11111111
.db  %11111111
.db  %11111111
.db  %11111111
.db  %11111111
.db  %11111111

.db  %00100100;smiley
.db  %00100100
.db  %00100100
.db  %00100100
.db  %10100101
.db  %10000001
.db  %11111111

.db   %01111110;zwat met wit hoekje
.db   %11111111
.db   %11111111
.db   %11111111
.db   %11111111
.db   %11111111
.db   %01111110

.db   %01111110;idem, met wit midden
.db   %11111111
.db   %11111111
.db   %11100111
.db   %11111111
.db   %11111111
.db   %01111110

.db   %10000001;een X
.db   %01000010
.db   %00100100
.db   %00011000
.db   %00100100
.db   %01000010
.db   %10000001

.db   %10000001
.db   %00000000
.db   %01000010
.db   %00100100
.db   %01000010
.db   %00000000
.db   %10000001

.db   %01111110;box
.db   %10000001
.db   %10000001
.db   %10000001
.db   %10000001
.db   %10000001
.db   %01111110

.db   %00011000
.db   %00011000
.db   %00011000
.db   %11111111
.db   %00011000
.db   %00011000
.db   %00011000

.db   %00000011
.db   %00000110
.db   %00001100
.db   %00011000
.db   %00110000
.db   %01100000
.db   %11000000

.db   %00011000
.db   %00000000
.db   %10111101
.db   %00100100
.db   %10111101
.db   %00000000
.db   %00011000

.db   %01111110
.db   %11000011
.db   %10100101
.db   %10011001
.db   %10100101
.db   %11000011
.db   %01111110

.db   %11100111
.db   %11100111
.db   %11100111
.db   %00000000
.db   %11100111
.db   %11100111
.db   %11100111

.db   %11111111
.db   %01111110
.db   %00111100
.db   %00011000
.db   %00111100
.db   %01111110
.db   %11111111

.db   %00011000
.db   %10000001
.db   %11000011
.db   %11100111
.db   %11000011
.db   %10000001
.db   %00011000

.db   %00011000
.db   %00011000
.db   %01111110
.db   %10011001
.db   %00111100
.db   %01000010
.db   %10000001

.db   %11100111
.db   %00011000
.db   %00011000
.db   %11111111
.db   %00011000
.db   %00011000
.db   %11100111

.db   %00000011
.db   %00000110
.db   %00111100
.db   %00100100
.db   %00111100
.db   %01100000
.db   %11000000

.db   %11100111
.db   %11111111
.db   %10111101
.db   %00100100
.db   %10111101
.db   %11111111
.db   %11100111

.db   %01111110
.db   %11000011
.db   %10000001
.db   %10011001
.db   %10000001
.db   %11000011
.db   %01111110

.db   %10000001
.db   %00000000
.db   %00000000
.db   %00011000
.db   %00000000
.db   %00000000
.db   %10000001

.db   %11111111
.db   %01000010
.db   %00100100
.db   %00011000
.db   %00100100
.db   %01000010
.db   %11111111

.db   %01111110
.db   %10000001
.db   %10011001
.db   %11111111
.db   %10011001
.db   %10000001
.db   %01111110

.db   %10001001
.db   %00000000
.db   %01000010
.db   %11100111
.db   %01000010
.db   %00000000
.db   %10010001

.db   %01111000
.db   %00011001
.db   %00011001
.db   %11111111
.db   %10011000
.db   %10011000
.db   %00011110;ja dit lijkt op een hakenkruis, maar bedoel er niet mee  :lol:

.db   %11111111
.db   %00000110
.db   %00001100
.db   %00011000
.db   %00110000
.db   %01100000
.db   %11111111

.db   %10011001
.db   %10000001
.db   %10111101
.db   %00100100
.db   %10111101
.db   %10000001
.db   %11111111


;-----------------------------------alfabet--------------

.db  %00011000;A
.db  %00100100
.db  %01000010
.db  %01111110
.db  %01000010
.db  %01000010
.db  %01000010

.db  %01111100;B
.db  %01000010
.db  %01000010
.db  %01111100
.db  %01000010
.db  %01000010
.db  %01111100

.db  %00011100;C
.db  %00100010
.db  %01000000
.db  %01000000
.db  %01000000
.db  %00100010
.db  %00011100

.db  %01111100;D
.db  %01000010
.db  %01000010
.db  %01000010
.db  %01000010
.db  %01000010
.db  %01111100

.db  %01111110;E
.db  %01000000
.db  %01000000
.db  %01111100
.db  %01000000
.db  %01000000
.db  %01111110

.db  %01111110;F
.db  %01000000
.db  %01000000
.db  %01111000
.db  %01000000
.db  %01000000
.db  %01000000

.db  %00011100;G
.db  %00100010
.db  %01000000
.db  %01000000
.db  %01000110
.db  %00100010
.db  %00011100

.db  %01000010;H
.db  %01000010
.db  %01000010
.db  %01111110
.db  %01000010
.db  %01000010
.db  %01000010

.db  %00011000;I
.db  %00000000
.db  %00011000
.db  %00011000
.db  %00011000
.db  %00011000
.db  %00011000

.db  %00000110;J
.db  %00000000
.db  %00000110
.db  %00000110
.db  %00000110
.db  %00110110
.db  %00011100

.db  %01000100;K
.db  %01001000
.db  %01010000
.db  %01100000
.db  %01010000
.db  %01001000
.db  %01000100

.db  %01000000;L
.db  %01000000
.db  %01000000
.db  %01000000
.db  %01000000
.db  %01000000
.db  %01111110

.db  %00100100;M
.db  %01011010
.db  %01011010
.db  %01000010
.db  %01000010
.db  %01000010
.db  %01000010

.db  %01100010;N
.db  %01010010
.db  %01010010
.db  %01011010
.db  %01001010
.db  %01001010
.db  %01000110

.db  %00011000;O
.db  %00100100
.db  %01000010
.db  %01000010
.db  %01000010
.db  %00100100
.db  %00011000

.db  %01111100;P
.db  %01000010
.db  %01000010
.db  %01111100
.db  %01000000
.db  %01000000
.db  %01000000

.db  %00111110;Q
.db  %01000010
.db  %01000010
.db  %00111110
.db  %00000010
.db  %00000010
.db  %00000010

.db  %01111100;R
.db  %01000010
.db  %01000010
.db  %01111100
.db  %01011000
.db  %01001100
.db  %01000110

.db  %00111110;S
.db  %01000000
.db  %01000000
.db  %00111100
.db  %00000010
.db  %00000010
.db  %01111100

.db  %01111110;T
.db  %00011000
.db  %00011000
.db  %00011000
.db  %00011000
.db  %00011000
.db  %00011000

.db  %01000010;U
.db  %01000010
.db  %01000010
.db  %01000010
.db  %01000010
.db  %01000010
.db  %01111110

.db  %01000010;V
.db  %01000010
.db  %01000010
.db  %00100100
.db  %00100100
.db  %00100100
.db  %00011000

.db  %01000010;W
.db  %01000010
.db  %01000010
.db  %01011010
.db  %01011010
.db  %01011010
.db  %00111100

.db   %01000010; X
.db   %01000010
.db   %00100100
.db   %00011000
.db   %00100100
.db   %01000010
.db   %01000010

.db   %01000010; Y
.db   %01000010
.db   %00100100
.db   %00011000
.db   %00011000
.db   %00011000
.db   %00011000

.db   %01111110; Z
.db   %00000110
.db   %00001100
.db   %00011000
.db   %00110000
.db   %01100000
.db   %01111110
.end
.end



edit: fix'd tag

yes i know my frog is a push/pop minefield lol

edit: no hide-able code blocks???

[codebox]-ified! –Goose


Last edited by Guest on 22 Oct 2008 01:05:48 am; edited 1 time in total
Back to top
magicdanw
pcGuru()


Calc Guru


Joined: 14 Feb 2007
Posts: 1110

Posted: 20 Oct 2008 07:08:35 pm    Post subject:

Hmm. I don't see anything that would obviously cause a memory leak, but one thing I notice is that you don't have very good structure of routines. I'd like to suggest structuring the code that it is obvious what sections are part of specific routines, and use routines that can be called and return, rather than specific ones that you jump to and then proceed to jump somewhere else. This makes it rather hard to follow program flow and to debug the problem.
Back to top
DarkerLine
ceci n'est pas une |


Super Elite (Last Title)


Joined: 04 Nov 2003
Posts: 8328

Posted: 20 Oct 2008 11:23:20 pm    Post subject:

Some additional tips on routines: I'm going to talk about "wincheck", but what I'm saying may apply to other places in your code too.

First of all, it's good practice to use the "call" instruction when you're calling a routine. Yes, you can sometimes do it with jumps the way you do (although in that case, it's also unclear why make it a routine in the first place, it might as well have been inserted inline). But supposing you want it to be a routine for clarity, it's best to call it using "call", and exit using "ret".

With that in mind, it's often not a good idea for the routine itself to handle things like "exit if there is a win". A better way to approach the problem is to return "did the player win?" in a flag such as Z/NZ, and handle that when the routine exits. For example:


Code:
; returns with Z flag set if there is a win
wincheck:
    ld hl, map-1
    ld b, 7*8+1
winloop:
    dec b
    ret z    ;WIN
    inc hl
    ld a, (hl)
    cp 1
    jr z, winloop
    ret      ;NO WIN

(notice that in the second case, the Z flag is always reset - if it were set, we would have jumped back to winloop)

Then, the code that calls wincheck can decide what to do with the information:

Code:
mainloop:
    push hl
    call wincheck
    pop hl
    jr z, exit      ;exit the game if player wins
    ...

In more complicated routines, the carry flag is usually used instead, simply because it can be manipulated easily; there's no other reason to pick one over the other.


Last edited by Guest on 22 Oct 2008 08:34:55 am; edited 1 time in total
Back to top
darkstone knight


Advanced Member


Joined: 07 Sep 2008
Posts: 438

Posted: 21 Oct 2008 01:35:31 pm    Post subject:

mhh... thanks

the next time i try to make a game, i organize all small routines BEFORE i start programing
Back to top
asdf


Advanced Newbie


Joined: 17 Aug 2008
Posts: 73

Posted: 22 Oct 2008 03:46:36 am    Post subject:

@DarkerLine: Btw, "cp 0" and "ld a,b" are unnecessary because "dec b" will set the Z flag if b=0 after decrementing. Also, "ld a,hl" isn't a valid instruction, i think you mean "ld a,(hl)"? And in your mainloop label, be careful with the stack. That code is fine, but take notice that you push hl then jump somewhere else before popping the value back off the stack. The exit label will need to adjust the stack to make sure the program doesn't have stack trouble.
Back to top
Display posts from previous:   
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
SP
    » Goto page 1, 2  Next
» View previous topic :: View next topic  
Page 1 of 2 » All times are UTC - 5 Hours

 

Advertisement