Stack algorithm
|
07-12-2016, 12:36 PM
Post: #1
|
|||
|
|||
Stack algorithm
Hello all!
To write a program that emulates a "real" HP-stack is more complicated than I thought - anyway I am convinced that the solution should be simple. I don't want to bother you with C-code so I try to formulate my algorithm in common: Code: loop It doesn't work like on my HP35s. When I have a value in X only and then press ie "2+" it overwrites the value in X. Where is my mind mistake and how did it HP on most calculators? Any improvement of my algorithm or a link to a simple program (I want to do it with an input-string) are highly welcome. Regards deetee |
|||
07-12-2016, 04:32 PM
Post: #2
|
|||
|
|||
RE: Stack algorithm
I was in Vienna a few days ago. Missed meeting you! We could have talked about emulators. :-(
Namir |
|||
07-12-2016, 09:55 PM
Post: #3
|
|||
|
|||
RE: Stack algorithm
You need to implement a stack lift flag. Sometimes when you enter a new number you need to raise the stack before putting the value in X, other times you don't. You need a flag to distinguish the two cases. Enter disables stack lift on entry, + enables it.
Of course it is more complex than this. This is a reside of a decision made in the early 1970's if not before because the calculators then didn't have enough RAM to store the number being entered separately -- instead they used the x register for this purpose. - Pauli |
|||
07-13-2016, 02:28 AM
Post: #4
|
|||
|
|||
RE: Stack algorithm
Here are a few stack functions I wrote as part of a larger C++ program. The function names and comments are fairly self explanatory. My approach was fairly straightforward. I discovered there are many special cases that have to be accounted for when emulating RPN stack dynamics.
PHP Code: void enter_pressed(bool *enter_was_pressed_) PHP Code: void add_pressed(bool *a_function_was_executed_, bool *decimal_fraction_) PHP Code: void space_pressed (bool *decimal_fraction_, bool *a_function_was_executed_, bool *enter_was_pressed_, bool *decimal_point_just_pressed_) PHP Code: void tab_pressed() //tab is the CHS key PHP Code: void roll_up_pressed(bool *a_function_was_executed_) |
|||
07-13-2016, 09:54 AM
(This post was last modified: 07-13-2016 09:55 AM by Dieter.)
Post: #5
|
|||
|
|||
RE: Stack algorithm
(07-13-2016 02:28 AM)Dwight Sturrock Wrote: Here are a few stack functions I wrote as part of a larger C++ program. About 20 years ago I did something similar with an RPN calculator written in Pascal. (07-13-2016 02:28 AM)Dwight Sturrock Wrote: The function names and comments are fairly self explanatory. My approach was fairly straightforward. I discovered there are many special cases that have to be accounted for when emulating RPN stack dynamics. It's not that complicated. Essentially it all boils down to the question whether stacklift is enabled or not. So a simple boolean variable "stacklift" will do. It is true after an operation or function call (except Σ+/Σ–) and false after ENTER and CLX. If a digit key (including dot/comma, CHS and EEX) is pressed, start a new number entry (resp. change sign) if stacklift is true resp. append to the current entry if not. Essentially, that's all you have to handle. Dieter |
|||
07-13-2016, 02:13 PM
(This post was last modified: 07-13-2016 02:28 PM by deetee.)
Post: #6
|
|||
|
|||
RE: Stack algorithm
Thank you very much for this expert help and many hints (and even thanks for inviting to meet in Vienna to exchange ideas).
I really was desperated and felt close to the solution but couldn't reach. And to handle a stack the right way is probably tho most fundamental HP topic. It makes much sense (or at least made it in the 70's) to save RAM and use the X-register to enable inputs - and use a one bit flag in addition (unfortunately my compiler uses one byte for a boolean variable). Here is my C-code (on which I am going to build up) - note that SPACE (' ') emulates ENTER: Code: void loop() Regards deetee |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)