[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: 36
Joined: Tue Apr 10, 2018 9:34 pm

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

Post by michaelzinn » Mon May 27, 2019 7:31 pm

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: 170
Joined: Thu May 04, 2017 10:20 am
Location: France

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

Post by dlachieze » Mon May 27, 2019 9: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 .
DM42 SN: 00425

rprosperi
Posts: 479
Joined: Mon Apr 24, 2017 5: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 » Tue May 28, 2019 3:19 am

dlachieze wrote:
Mon May 27, 2019 9: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

michaelzinn
Posts: 36
Joined: Tue Apr 10, 2018 9:34 pm

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

Post by michaelzinn » Tue May 28, 2019 6:19 am

rprosperi wrote:
Tue May 28, 2019 3: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 3: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: 479
Joined: Mon Apr 24, 2017 5: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 » Tue May 28, 2019 1:07 pm

michaelzinn wrote:
Tue May 28, 2019 6:19 am
rprosperi wrote:
Tue May 28, 2019 3: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 3: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

grsbanks
Posts: 851
Joined: Tue Apr 25, 2017 9: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 » Tue May 28, 2019 2:34 pm

michaelzinn wrote:
Tue May 28, 2019 6: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 :)
Not SwissMicros staff, just an enthusiast.

michaelzinn
Posts: 36
Joined: Tue Apr 10, 2018 9:34 pm

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

Post by michaelzinn » Tue May 28, 2019 3:28 pm

rprosperi wrote:
Tue May 28, 2019 1: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: 170
Joined: Thu May 04, 2017 10:20 am
Location: France

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

Post by dlachieze » Tue May 28, 2019 3:37 pm

michaelzinn wrote:
Tue May 28, 2019 3:28 pm
rprosperi wrote:
Tue May 28, 2019 1: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 SN: 00425

michaelzinn
Posts: 36
Joined: Tue Apr 10, 2018 9:34 pm

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

Post by michaelzinn » Tue May 28, 2019 4:56 pm

dlachieze wrote:
Tue May 28, 2019 3: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: 479
Joined: Mon Apr 24, 2017 5: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 » Tue May 28, 2019 9:01 pm

dlachieze wrote:
Tue May 28, 2019 3:37 pm
michaelzinn wrote:
Tue May 28, 2019 3:28 pm
rprosperi wrote:
Tue May 28, 2019 1: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

Post Reply