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
Author Message
Tyraniek


Member


Joined: 07 Jun 2003
Posts: 133

Posted: 20 Jun 2003 11:00:25 am    Post subject:

How do you quit a program ? In fact, I know we just have to write "ret" where we want to exit, but in my case, it does something like a "ret" in a routine : in other works, it does not exit the program....
Maybe it is because I often use "JP" and "JR", but we have to in a "big" program.

I also put a label at the end of my program, and I send the program to this adress when I want to exit, but it cleans the RAM....
And when I add "ret" (so that the machine code ends with C9), it does always the same bug : the processor think he is in a subroutine and then it justs return somewhere in the program....

Do you know how I could do ? If you want, I send you my program...
Back to top
Shoester


Newbie


Joined: 02 Jun 2003
Posts: 8

Posted: 20 Jun 2003 12:54:54 pm    Post subject:

It sounds like you need to get out of a call first before you try and exit the program. To get out of a call just type in a ret to go back to the place where you called the routine from. Once you've gotten out of all of your calls another ret should exit the program. Hope that helps.
Back to top
Tyraniek


Member


Joined: 07 Jun 2003
Posts: 133

Posted: 20 Jun 2003 02:38:19 pm    Post subject:

Yes that's it ; but then, we have to tell the program that we've use a ret in order to exit the program : that's why I think I'll create a kind of "boolean variable", which will be equal to 1 if we wanna exit, 0 else.
Then, when we get out of the subroutine, we scan this variable, and if it is equal to 1, we JUMP (we don't call) somewhere where there is a "ret".
I don't know how to do otherwise.
Back to top
Shoester


Newbie


Joined: 02 Jun 2003
Posts: 8

Posted: 20 Jun 2003 03:35:44 pm    Post subject:

Yes that should work, but there might be better way. Though i don't really know without seeing your program. O and to change the variable from 1 to 0 and visa versa you could do this:


Code:
ld a,(boolean)
xor 1
ld (boolean),a


This works either way so if you want to change the variable just call this code.
Back to top
Tyraniek


Member


Joined: 07 Jun 2003
Posts: 133

Posted: 20 Jun 2003 03:56:53 pm    Post subject:

Hey, thanks ! That's something to know ! Smile

But this is interesting too, isn't it ?
Quote:
Finally, the stack pointer can be used to terminate the program like C's exit().
You know from the last section that RET is like a POP PC. Since RET is used to end a program, you can infer that the TI-OS uses CALL $9D95 to execute a program. If the value of SP was saved at the start of the program, it could be later restored and followed with RET to stop the program:

;Start of program
LD (save_SP),SP
.
.
.
;Somewhere within the program
LD SP,(save_SP)
RET
This could be useful if, say, you had to exit from deep within the program and you're unable to remove all the stack pushes, such as from within a subroutine.


I found it in "learn asm in 28 days", in day 16
I'm juste testing it !
Back to top
Tyraniek


Member


Joined: 07 Jun 2003
Posts: 133

Posted: 20 Jun 2003 04:03:31 pm    Post subject:

Works perfectly !
Thank you Sean McLaughlin !
so that's another thing to know Cool
Back to top
NETWizz
Byte by bit


Bandwidth Hog


Joined: 20 May 2003
Posts: 2369

Posted: 22 Jun 2003 04:17:38 am    Post subject:

Not only will call mess set a new ret point, but if you push anything onto the stack, you must pop it off (or reset sp) before returning to TI-OS.
Back to top
CoBB


Active Member


Joined: 30 Jun 2003
Posts: 720

Posted: 30 Jun 2003 09:22:04 am    Post subject:

Saving the stack pointer is certainly a working solution, but it's more of a dirty hack than a good approach to the problem. It might be good for "teacher key" situations, but shouldn't be applied under "ordinary" circumstances. A harder but better way is forcing yourself to write structured code. With a clean structure you can exit with a simple ret. Example for a main program:

Start: ; The entry point is here
call Intro ; Displaying a splash screen
MainLoop:
ld hl,MainMenu ; Pointer to the data describing the main menu
call Menu ; Generic menu routine
cp 5 ; Assuming that the 5th menu item was "quit game"
ret z ; Returning to the TIOS if the accumulator is 5
ld hl,MainMenu
call LaunchItem ; Generic launcher
jr MainLoop ; Starting over again after everything's done

In this program there are two routines used to handle selection:

Menu: draws a menu on the screen using the data at HL and lets the user choose an item; returns the number of the item chosen in A

LaunchItem: launches the Ath item of the menu at HL

The actual implementation of these routines is immaterial now, they are there just to illustrate my point.
Back to top
Tyraniek


Member


Joined: 07 Jun 2003
Posts: 133

Posted: 30 Jun 2003 02:41:52 pm    Post subject:

the problem is that my program is something like that :


Code:
normal code (use ret to return to TI-OS)
call routine
stuff

label:
this RET should return to TI-OS


routine:
something
and now, I don't use RET, but JP LABEL if a condition is true for exemple


that's why the ret will only return to the program, as if it was still in the routine, you know what I mean ?
so it would be a lot much difficult to detect wether we've exit the routine with RET or JP

anyway, your idea to write clean and intelligible programs is good and I congratulate you for wanting to be as clean, but I think resetting the SP register is a good thing all the same Wink
and my program works with it, of course ! Razz
Back to top
CoBB


Active Member


Joined: 30 Jun 2003
Posts: 720

Posted: 30 Jun 2003 03:19:25 pm    Post subject:

That's just theory. In fact, I love writing obfuscated code, so I also use the saved SP to exit. Very Happy
Back to top
Tyraniek


Member


Joined: 07 Jun 2003
Posts: 133

Posted: 30 Jun 2003 04:18:50 pm    Post subject:

LOL, OK ! Very Happy
Back to top
NETWizz
Byte by bit


Bandwidth Hog


Joined: 20 May 2003
Posts: 2369

Posted: 30 Jun 2003 06:59:12 pm    Post subject:

Look, saving the stack pointer is perfectly okay. It is a plenty clean operation.

For example, let us say with have the following stack

>FFDE
ABC5

> points to the top which is FFFC

So, if we save the stackpointer

e.g.

ld (appbackupscreen),sp

now we push 1234

We have
>1234
FFDE
ABC5

If we then ld sp,(appbackupscreen)

we would have

1234
>FFDE
ABC5

The 1234 would not be relevant because if you pop you get FFDE and if you push, you overwrite 1234.

Moreover, the area of ram used for the stack is reserved meaning that if you reset the stack pointer, the ram that is trival junk will not be used for anything else; therefore, you have nothing to worry about.

There is room to put 200 entries on the stack. Smile
Back to top
Adm.Wiggin
aka Tianon


Know-It-All


Joined: 02 Jun 2003
Posts: 1874

Posted: 30 Jun 2003 09:24:33 pm    Post subject:

if u pop, does it reset the entry to zero, or is it preserved? i would guess it is preserved... until the next push that is Wink
Back to top
CoBB


Active Member


Joined: 30 Jun 2003
Posts: 720

Posted: 01 Jul 2003 01:20:13 am    Post subject:

Jbirk: it seems you misunderstood my point. I don't have anything against saving SP in particular. What I'm against is the reliance on this tool. It makes sense to write well organised code, doesn't it?

Adm: POP fills the register in its operand with the contents of the top of the stack and increases SP by two. The data in the memory is left intact. It would be illogical to clear it anyway: what if the programmer adjusts SP directly?
Back to top
Adm.Wiggin
aka Tianon


Know-It-All


Joined: 02 Jun 2003
Posts: 1874

Posted: 01 Jul 2003 12:46:40 pm    Post subject:

ya, that is what i thought... ok, i think i have push and pop worked out...
push rr =

Code:
     dec sp
     dec sp
     ld sp,rr

and pop rr =

Code:
     ld rr,sp
     inc sp
     inc sp
is that right? or do i have something wrong in there somewhere?

and i think writing organized code should be done whenever possible! to do this eaiser, i always decide
1. is the routine a routine, or a new peice of code? (call or jump?)
2. if it is a new peice of code, then i jump in, jump out
3. if it is a routine, then i call in, ret out!

get it?


Last edited by Guest on 01 Jul 2003 12:48:57 pm; edited 1 time in total
Back to top
CoBB


Active Member


Joined: 30 Jun 2003
Posts: 720

Posted: 01 Jul 2003 12:54:47 pm    Post subject:

That's almost right. To be more exact, the CPU does the following (illustration only):

PUSH xx:

Code:
dec sp
ld (sp),xxh
dec sp
ld (sp),xxl


POP xx:

Code:
ld xxl,(sp)
inc sp
ld xxh,(sp)
inc sp


E. g. if xx=BC then xxh is B and xxl is C. So PUSH is predecrementing and POP is postincrementing.
Back to top
Adm.Wiggin
aka Tianon


Know-It-All


Joined: 02 Jun 2003
Posts: 1874

Posted: 01 Jul 2003 01:21:54 pm    Post subject:

ahh, ok, i understand now! cool... now, Tyraniek, did u get it working? i think u said u did, but not sure...
Back to top
Tyraniek


Member


Joined: 07 Jun 2003
Posts: 133

Posted: 01 Jul 2003 02:23:50 pm    Post subject:

yes, I did, don't worry !
since the beggining I get it working ! Laughing
Back to top
Adm.Wiggin
aka Tianon


Know-It-All


Joined: 02 Jun 2003
Posts: 1874

Posted: 01 Jul 2003 02:25:59 pm    Post subject:

what kind of prgm is it? what does it do?
Back to top
Tyraniek


Member


Joined: 07 Jun 2003
Posts: 133

Posted: 01 Jul 2003 05:05:08 pm    Post subject:

héhé...
it was juste my snake !
the most recent version is here : www.tyraniek.fr.st/assembleur.php
the ticalc version has many bugs...
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
    » Goto page 1, 2  Next
» View previous topic :: View next topic  
Page 1 of 2 » All times are UTC - 5 Hours

 

Advertisement