[DM15L M1B V28] Using R/S in a subroutine reached by line number wipes the call stack

If you think you've found a bug in one of these machines, post about it here indicating the steps you perform in order to reproduce it.

Please prefix your topic with the model number in question and firmware version this way:

[DM16 V22] CHS gives unexpected results
michaelzinn
Posts: 41
Joined: Tue Apr 10, 2018 11:34 pm

[DM15L M1B V28] Using R/S in a subroutine reached by line number wipes the call stack

Post by michaelzinn »

If you continue a subroutine that was stopped with R/S, the next RTN will only work if the subroutine was called by label (Either directly or by a positive number in the I register), not if it was called by line number (negative number in I).

To be precise:
- GTO works always
- GSB A works
- GSB A where the subroutine contains an R/S works
- GSB I where I=20 ("A") works
- GSB I where I=20 ("A") and the subroutine contains an R/S works
- GSB I where I=-1 (line of A) works
- GSB I where I=-1 (line of A) and the subroutine contains an R/S does not work, because the return stack seems to disappear, so the next RTN returns to 000 and stops

You can test this with these programs (Launch "B"):

Doesn't work when resuming the program with R/S:
LBL A
20
R/S
RTN
LBL B
1
CHS
STO I
GSB I
21 ; unreachable code
RTN

This works, the only difference is that it uses GSB A instead of GSB I
LBL A
20
R/S
RTN
LBL B
1
CHS
STO I
GSB A
21 ; fine, will show up
RTN

Also works, only difference is no R/S in the subroutine:
LBL A
20
RTN
LBL B
1
CHS
STO I
GSB I
21 ; fine
RTN

Also fine, I contains "A" as 20
LBL A
20
R/S
RTN
LBL B
20
STO I
GSB I
21 ; fine
RTN

The manual does not use GSB with line numbers, but I think that the correct behaviour would be to only wipe the subroutine call stack when the user interrupts the program control flow, i.e. by using the buttons A-E or GSB 0 etc.

I don't actually have an HP calculator, can anybody confirm this behaviour on a real HP15C?
dlachieze
Posts: 613
Joined: Thu May 04, 2017 12:20 pm
Location: France

Re: [DM15L M1B V28] Using R/S in a subroutine reached by line number wipes the call stack

Post by dlachieze »

This seems to be a bug of the original 15C firmware that was fixed by changing the manual to remove the reference to GSB I with I<0. See this discussion .
DM42: 00425 - DM41X: β00066 - WP43: 00042
rprosperi
Posts: 1703
Joined: Mon Apr 24, 2017 7:48 pm
Location: New York

Re: [DM15L M1B V28] Using R/S in a subroutine reached by line number wipes the call stack

Post by rprosperi »

dlachieze wrote:
Mon May 27, 2019 11:32 pm
This seems to be a bug of the original 15C firmware that was fixed by changing the manual to remove the reference to GSB I with I<0. See this discussion .
Nice find Didier.

I tend to agree with Message #11 Posted by Michael de Estrada in that thread. GSB is intended to go to a labeled routine that has a RTN; arbitrary jumps to line numbers is fine (it's a plain 'ol GOTO) but a Gosub to an arbitrary line number is poor programming so they probably prevented it. As to whether it was a bug in the 15C and then covered-up with a manual edit, or a design change after some early manuals were released, guess we'll never know. In any case though the DM15 should clearly behave the same as genuine HP-15Cs.
--bob p

DM42: β00071 & 00282, DM41X: β00071 & 00656, DM10L: 071/100
michaelzinn
Posts: 41
Joined: Tue Apr 10, 2018 11:34 pm

Re: [DM15L M1B V28] Using R/S in a subroutine reached by line number wipes the call stack

Post by michaelzinn »

rprosperi wrote:
Tue May 28, 2019 5:19 am
but a Gosub to an arbitrary line number is poor programming so they probably prevented it.
Not if you only use this to jump to specific line numbers because you ran out of labels.
rprosperi wrote:
Tue May 28, 2019 5:19 am
In any case though the DM15 should clearly behave the same as genuine HP-15Cs.
Personally, I think it should behave like a bug free HP-15C instead. But judging from the linked discussion I guess this would be very difficult to fix.

I guess I'll either have to do label budgeting or write my own gosub stack. :?
rprosperi
Posts: 1703
Joined: Mon Apr 24, 2017 7:48 pm
Location: New York

Re: [DM15L M1B V28] Using R/S in a subroutine reached by line number wipes the call stack

Post by rprosperi »

michaelzinn wrote:
Tue May 28, 2019 8:19 am
rprosperi wrote:
Tue May 28, 2019 5:19 am
but a Gosub to an arbitrary line number is poor programming so they probably prevented it.
Not if you only use this to jump to specific line numbers because you ran out of labels.
rprosperi wrote:
Tue May 28, 2019 5:19 am
In any case though the DM15 should clearly behave the same as genuine HP-15Cs.
Personally, I think it should behave like a bug free HP-15C instead. But judging from the linked discussion I guess this would be very difficult to fix.

I guess I'll either have to do label budgeting or write my own gosub stack. :?
Bug or not, this was not really needed in the limited RAM of the 15C, so even if it was a bug, it appears there was insufficient motivation to fix it.

How would you go about writing your own gosub stack? Do you mean use GTO I with flags and such to simulate a GSB/RTN ?
--bob p

DM42: β00071 & 00282, DM41X: β00071 & 00656, DM10L: 071/100
grsbanks
Posts: 1122
Joined: Tue Apr 25, 2017 11:23 am
Location: Preston, Lancs, UK
Contact:

Re: [DM15L M1B V28] Using R/S in a subroutine reached by line number wipes the call stack

Post by grsbanks »

michaelzinn wrote:
Tue May 28, 2019 8:19 am
Personally, I think it should behave like a bug free HP-15C instead.
Depending on what you class as a bug, both the HP-15C and the DM15 are bug-free...

Personally I don't consider this a bug. The original 15C had a maximum of 448 bytes (not steps, some instructions use 2 bytes) and it was not unreasonable to think that the 25 available labels should suffice. With the DM15_M80 and DM15_M1B you get 896 or 1603 bytes respectively. OK, you still have to spread the 25 available labels in that space but you're still getting more than you were with the original. Don't complain :)
There are only 10 kinds of people in the world: those who understand binary and those who do not.
michaelzinn
Posts: 41
Joined: Tue Apr 10, 2018 11:34 pm

Re: [DM15L M1B V28] Using R/S in a subroutine reached by line number wipes the call stack

Post by michaelzinn »

rprosperi wrote:
Tue May 28, 2019 3:07 pm
How would you go about writing your own gosub stack? Do you mean use GTO I with flags and such to simulate a GSB/RTN ?
Not sure, one way would be to put the stack pointer in register 0, having it contain the register number that contains the line number to jump to on return.

GOSUB could then look like this:

1. Put the target line number on the stack
2. Put the line number after the GTO on the stack
3. GTO 1

LBL 1
CHS
1
RCL+0
STO 0
STO I
CLx
+
STO (i)
x><y
STO I
GTO I

Returning would be simpler, just do GTO 2
LBL 2
RCL 0
STO I
1
-
STO 0
GTO (i)

This is not tested.

I'll have to ponder the problem for some time since there are several ways to approach this. For example, if I only need labels for GOSUB but not GOTO (always works) or FUNCTION calls (Can use GSB with a line number, since function never contain R/S) the 25 labels might be enough.
dlachieze
Posts: 613
Joined: Thu May 04, 2017 12:20 pm
Location: France

Re: [DM15L M1B V28] Using R/S in a subroutine reached by line number wipes the call stack

Post by dlachieze »

michaelzinn wrote:
Tue May 28, 2019 5:28 pm
rprosperi wrote:
Tue May 28, 2019 3:07 pm
How would you go about writing your own gosub stack? Do you mean use GTO I with flags and such to simulate a GSB/RTN ?
Not sure, one way would be to put the stack pointer in register 0, having it contain the register number that contains the line number to jump to on return.
There is an easier workaround as described in the post #46 of the discussion I linked above: just replace the GSB I by a GSB to a label followed by GTO I.
DM42: 00425 - DM41X: β00066 - WP43: 00042
michaelzinn
Posts: 41
Joined: Tue Apr 10, 2018 11:34 pm

Re: [DM15L M1B V28] Using R/S in a subroutine reached by line number wipes the call stack

Post by michaelzinn »

dlachieze wrote:
Tue May 28, 2019 5:37 pm
There is an easier workaround as described in the post #46 of the discussion I linked above: just replace the GSB I by a GSB to a label followed by GTO I.
That's brilliant! Thank you very much.
rprosperi
Posts: 1703
Joined: Mon Apr 24, 2017 7:48 pm
Location: New York

Re: [DM15L M1B V28] Using R/S in a subroutine reached by line number wipes the call stack

Post by rprosperi »

dlachieze wrote:
Tue May 28, 2019 5:37 pm
michaelzinn wrote:
Tue May 28, 2019 5:28 pm
rprosperi wrote:
Tue May 28, 2019 3:07 pm
How would you go about writing your own gosub stack? Do you mean use GTO I with flags and such to simulate a GSB/RTN ?
Not sure, one way would be to put the stack pointer in register 0, having it contain the register number that contains the line number to jump to on return.
There is an easier workaround as described in the post #46 of the discussion I linked above: just replace the GSB I by a GSB to a label followed by GTO I.
But how does that help if one has no available labels, which was the reason for switching from GSB Label to GSB I ?

Anyhow, I'd say you'd get better results reorganizing the program to somehow get it done with 25 labels and using flags or register contents as flags if more is absolutely needed.
--bob p

DM42: β00071 & 00282, DM41X: β00071 & 00656, DM10L: 071/100
Post Reply