Post Reply 
Two's complement on the 50g - a misadventure and a challenge
09-28-2024, 11:18 PM
Post: #1
Two's complement on the 50g - a misadventure and a challenge
I've probably done this before, 15 years ago, but today I did it again: I searched for how to interpret a binary number as a two's complement signed integer with my 50g and I didn't find it in there. So I decided to code it.

Now, my RPL coding skills never did get much development, as pretty much all I ever need is either a built-in function or a simple chain of instructions. Branching? I needed to look that up today. My overall coding skills must be pretty rusty, or maybe it is my understanding of two's complement which I never really had a need for until today - you see, a good solution to this problem really shouldn't need a branch. What I wrote today is not the good solution. More on that later.

This is what I ended up with:

Debug:
Code:
<< 41.01 MENU 'B->2s' DBUG >>

Max2s:
Code:
<< 2 RCWS 1 - ^ 1 - >>

Now you know what 95% of all RPL code I've ever written looks like. Forum usage sidenote: is there a better way to put the name of each function? Maybe as the label of the code block?

Now for today's foray into more advanced programming.

B->2s:
Code:
<< B->R DUP Max2s
   IF >
   THEN 2 RCWS ^ -
   END
>>

Comments on that? I do find this readable, but I think RPL needs to be kept in similarly short descriptively named pieces in order to stay readable. I wouldn't want multiple control flow constructs in the same piece of code. I want to add a description for each function about what it expects on the stack and what it leaves on the stack, not sure how to do that. What it expects can to some extent be shown by putting it into local variables with reasonable names, but it feels rather silly to do that just to immediately put the same things on the stack again in the same order and never actually make use of those local variables.

It works now, but there were several drafts that did not. As you may have realized by the fact that I created that debug launching macro.

For a small challenge if you know two's complement and a larger one if you don't know and are working only from the code above: find the solution that doesn't need branching. I haven't tried it myself yet, but I think I know how to do it. I'm pretty sure it can be done without RCWS as well as with, I'm not sure which is best, for any definition of best. I'll provide my solutions in three weeks, 19th or 20th.
Find all posts by this user
Quote this message in a reply
09-29-2024, 04:54 AM (This post was last modified: 09-29-2024 06:46 AM by Thomas Klemm.)
Post: #2
RE: Two's complement on the 50g - a misadventure and a challenge
(09-28-2024 11:18 PM)LinusSch Wrote:  Forum usage sidenote: is there a better way to put the name of each function?
(…)
I want to add a description for each function about what it expects on the stack and what it leaves on the stack, not sure how to do that.

You could list the directory and use stack diagrams in comments:
Code:
DIR
  B\->2s @ (B -- 2s)
    \<< B\->R DUP Max2s
      IF >
      THEN 2 RCWS ^ -
      END
    \>>
  Max2s @ ( -- Max2s)
    \<< 2 RCWS 1 - ^ 1 -
    \>>
END
However, the stack diagrams seem superfluous to me in this case.
By using trigraphs I can copy and paste the code into an emulator.

It may only be me, but examples are often helpful.

Examples

16 STWS

Max2s

32767

# 7d
+/-

# 65529d

B→2s

-7
Find all posts by this user
Quote this message in a reply
09-29-2024, 07:14 AM
Post: #3
RE: Two's complement on the 50g - a misadventure and a challenge
(09-28-2024 11:18 PM)LinusSch Wrote:  For a small challenge if you know two's complement and a larger one if you don't know and are working only from the code above: find the solution that doesn't need branching.

Code:
\<< # 1d RR OVER AND B\->R -2 * SWAP B\->R +
\>>
Find all posts by this user
Quote this message in a reply
10-31-2024, 06:36 PM
Post: #4
RE: Two's complement on the 50g - a misadventure and a challenge
(09-28-2024 11:18 PM)LinusSch Wrote:  I'll provide my solutions in three weeks, 19th or 20th.

What are your solutions?
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)