Really Really Basic Question about Subroutines

Programs for HP-41

Really Really Basic Question about Subroutines

Postby Weegie » Mon Jun 28, 2021 3:10 pm

I'm just learning to program once again with the HP41.

At the moment I'm constructing some very simple programs that use common inputs.

I made 2 separate programs for different calculations, these use the same inputs, so I made a third program for the inputs, the idea being I could call the input program as a subroutine from the other 2.

From reading the manual that would appear to be Ok, but obviously I'm missing something.

For the sake of this thread I'll call the calculation program "Calculation" and the program that requests the inputs "Input". Both have one global label at the top and an "END" at the bottom.

When I use the XEQ "Input" in the "Calculation" program, the program jumps to the "Input" program and runs, but doesn't jump back to the original program "Calculation", it just stops at the input END command.

What am I missing? Apologies for such a dumbassed basic question but from reading the manual I just can't see what I'm doing wrong.
Weegie
..
..
 
Posts: 6
Joined: Mon Jun 28, 2021 1:59 pm

Re: Really Really Basic Question about Subroutines

Postby Weegie » Mon Jun 28, 2021 3:44 pm

I tried another example which does the same thing in both my DM41X and the HP Emulator on my computer

So the TEST program calls SUB

Within SUB I get the option of pressing A or B on the keyboard from the prompt in User mode.

If I press A "AAAAAAAAAA" shows briefly on the display and then the option of pressing A or B once again.

If I press B I get "BBBBBBBBBB" shows briefly on the display the User mode is switched off and then the program stops.

What I was expecting is that the END in SUB would hand back execution to TEST and I'd then see "TTTTTTTTTTTT" but the program stops at the END of SUB

Here are the 2 listings

01 LBL "TEST"
02 XEQ "SUB"
03 "TTTTTTTTTTT"
04 AVIEW
05 PSE
06 END

01 LBL "SUB"
02 SF 27
03 LBL 09
04 "A B"
05 PROMPT
06 LBL A
07 "AAAAAAAAAAA"
08 AVIEW
09 PSE
10 GTO 09
11 LBL B
12 "BBBBBBBBBBBB"
13 AVIEW
14 PSE
15 CF 27
16 END
Weegie
..
..
 
Posts: 6
Joined: Mon Jun 28, 2021 1:59 pm

Re: Really Really Basic Question about Subroutines

Postby Weegie » Mon Jun 28, 2021 5:43 pm

Ok got my answer

It turns out if there is any interruption in the program, I take this to mean any stopping, then return to the original program isn't possible.

So subroutines can only be used when the program being called does not require inputs, as this would halt program execution.

When I deleted the inputs in SUB and just ran it so the program ran continuously, it showed AAAAAAA followed by BBBBBBBBBBB, then SUB returned to TEST to display TTTTTTTTTTT.
Weegie
..
..
 
Posts: 6
Joined: Mon Jun 28, 2021 1:59 pm

Re: Really Really Basic Question about Subroutines

Postby Garth » Mon Jun 28, 2021 6:15 pm

Looking in the manual, I'm surprised to find that END can act as a RTN and not just a STOP. But since it's in another program altogether, see what happens if you put the RTN in there, before the END. I'll have to review the rules for stopping and what it does to the subroutine return stack.
Garth
Moderator
Moderator
 
Posts: 267
Joined: Tue Jan 20, 2009 1:31 am

Re: Really Really Basic Question about Subroutines

Postby Weegie » Tue Jun 29, 2021 8:47 am

Yeah thanks Garth I did try the RTN and it behaved the same way as the END, there doesn't seem to be a great deal of difference in how the RTN and END influence the behaviour of the subroutine jump, although I'm sure with some more digging I'll find a reason for the 2 commands. Should I find anything further I'll post on here.

In the manual Vol 2 page 301 the bottom paragraph was where I found the statement on how program interruption will loose the pointer to enable the jump return. In the example below that, although a bit academic, you can also see an example where they use and END to jump back to the next instruction after a subroutine then use a RTN to halt program execution.

I'm now going to use flags and labels on both the "Input" and "Calcualtion" to redirect the pointer to the correct position and continue execution

Oh and apologies due to being new and not familiar with the forum I probably posted this in the wrong place and it should have been under Programming, please relocate it if the thread is in the wrong place
Weegie
..
..
 
Posts: 6
Joined: Mon Jun 28, 2021 1:59 pm

Re: Really Really Basic Question about Subroutines

Postby Garth » Tue Jun 29, 2021 9:30 am

You could use synthetic programming to save the return stack, and then restore it after you do your manual inputs in another program and come back. Registers a and b hold the returns. The book "Extend Your HP-41" by W.A.C. Mier-Jedrzejowicz, Ph.D., pages 473-479, tells how. The ZENROM and a few other modules that do synthetic programming will make it easier than using the byte grabber. Angel's WarpCore module also has PUSHRTN and POPRTN (push return stack to buffer and pop return stack from buffer) functions. The module has a ton of functions that improve the speed and code density of programs in any discipline.
Garth
Moderator
Moderator
 
Posts: 267
Joined: Tue Jan 20, 2009 1:31 am

Re: Really Really Basic Question about Subroutines

Postby Weegie » Tue Jun 29, 2021 11:44 am

Yeah Angel Martin is quite the "God" when it comes to programming with these devices, his work is nothing short of amazing.

Then there all the additional modules he's made too

At the moment I'm not at the level where I'm able to understand a lot of the programming modules and tweaks that Angel has developed.

Reckon that the more code I (badly) write then the more understanding I'll get.

Got it passing back and forward just using a single flag, at the moment just the in built commands are daunting enough

Had a quick look for the book but nowhere seems to have it available

Thanks for the help Garth, always a bit of unknown when you join a forum, not knowing what sort of response you'll get to, what are, beginners question, nice to recieve a warm welcome...........appreciated

John
Weegie
..
..
 
Posts: 6
Joined: Mon Jun 28, 2021 1:59 pm

Re: Really Really Basic Question about Subroutines

Postby mike-stgt » Tue Jun 29, 2021 12:39 pm

Weegie wrote:... the "God" ...
Very fundamental remark to your Really Really Basic Question: Ni Dieu, ni maitre !
mike-stgt
.........
.........
 
Posts: 132
Joined: Tue Dec 24, 2019 12:12 pm

Re: Really Really Basic Question about Subroutines

Postby pcscote » Thu Oct 21, 2021 11:48 pm

Hello Garth,

Garth wrote:Looking in the manual, I'm surprised to find that END can act as a RTN and not just a STOP. But since it's in another program altogether, see what happens if you put the RTN in there, before the END. I'll have to review the rules for stopping and what it does to the subroutine return stack.


On the HP-41, both the 1 byte RTN and 3 bytes END/.END. will return from a call if there are addresses on the return stack and will stop a running program if the return stack is empty.
The 3 bytes END function also contains other informations like: previous global label offset, local/global end info and packed program flag.
Finally, the END also isolate program from each other, so that local labels (00..99, A..J, a..e) are only reachable within a program space.

Sylvain
User avatar
pcscote
......
......
 
Posts: 22
Joined: Sat Oct 25, 2014 1:24 pm
Location: Quebec/Canada


Return to HP-41 Software

Who is online

Users browsing this forum: No registered users and 1 guest

cron