Post Reply 
Converting from one base to another?
04-09-2016, 10:02 PM
Post: #1
Converting from one base to another?
Hello again all,

I have another newbie programming question.

As you may have seen, I developed an 8080 emulator that uses M1 as a single column array for the program memory. The 8080 instructions are stored as decimal numbers. I have been entering only very short test programs by hand to exercise my emulator so far.

I have the code from my system monitor in my vintage IMSAI system that allows a few activities, such as dump memory from one address to another, enter code byte by byte into memory, and goto a memory location and start executing.

I would like to enter this code into my M1 array for my emulator. I have the code as hexadecimal numbers. So, I tried to create a small program to allow me to enter hex bytes, and store them as decimal numbers. I figured I would be clever and simply change the Base to Hexadecimal for the hex entry, and then change the Base to Decimal to store the number into M1.

Here's the code:
Code:

// LoadCode
//
// Load 8080 code into M1
// Input the first location in decimal, then increment from there
// Input the 8080 code as hex bytes, convert to decimal and store
//
LOCAL loc,byte;
//
EXPORT LoadCode()
BEGIN
  byte:=0;
  loc:=0;
  INPUT(loc,"Starting Location","Start=","Enter the starting location in decimal",0);
  WHILE (byte>=0) DO
    byte:=M1(loc);
    3->Base;  // set base to hexadecimal
    INPUT(byte,"8080 Code","Byte=","Enter a byte as a hex number",0);
    2->Base;  // set base to decimal
    M1(loc):=byte;
    loc:=loc+1;
  END;
END;

Unfortunately for me, this is not working. The program as written works just fine if I enter normal decimal numbers. However, if I enter 0A, 0B, 0C... it stores away 0. Also, if I try A, B, C, D, E, or F, I get unusual numbers instead of the expected 10, 11, 12, 13, 14, or 15.

This method of changing the Base is not working for me. Am I on the right track, or will I have to enter two-character strings and do the whole conversion myself?

Thanks for listening. Your advice would be very much appreciated.

smp
Find all posts by this user
Quote this message in a reply
04-09-2016, 10:32 PM
Post: #2
RE: Converting from one base to another?
Would chapter 28 of the user guide, particularly the B→R(#integerm) feature be of use for you? Integers prefixed by # and suffixed by o, h, d (octal, hex, decimal) in particular might be just what you need.

-Dale-
Find all posts by this user
Quote this message in a reply
04-09-2016, 11:54 PM
Post: #3
RE: Converting from one base to another?
(04-09-2016 10:32 PM)DrD Wrote:  Would chapter 28 of the user guide, particularly the B→R(#integerm) feature be of use for you? Integers prefixed by # and suffixed by o, h, d (octal, hex, decimal) in particular might be just what you need.

-Dale-

Hi Dale,

Thanks very much for your reply.

Oh, no! I was hoping to be able to do the hex entry as C3 09 F3, rather than #C3h, #09h, #F3h. Well, too bad for me, I guess. Lots more typing than I hoped, unless I can figure out how to enter two character strings and do the conversion myself.

Thanks,
smp
Find all posts by this user
Quote this message in a reply
04-10-2016, 02:38 AM
Post: #4
RE: Converting from one base to another?
If 'byte' is a string, such as "C3", you could try something like: EXPR("#"+byte+"h")
Find all posts by this user
Quote this message in a reply
04-10-2016, 04:33 AM
Post: #5
RE: Converting from one base to another?
Hi!:

For convert, can use ... (see image).

Kind Regards.
informach.


Attached File(s) Thumbnail(s)
   
Find all posts by this user
Quote this message in a reply
04-10-2016, 01:17 PM
Post: #6
RE: Converting from one base to another?
(04-09-2016 11:54 PM)smp Wrote:  
(04-09-2016 10:32 PM)DrD Wrote:  Would chapter 28 of the user guide, particularly the B→R(#integerm) feature be of use for you? Integers prefixed by # and suffixed by o, h, d (octal, hex, decimal) in particular might be just what you need.

-Dale-

Hi Dale,

Thanks very much for your reply.

Oh, no! I was hoping to be able to do the hex entry as C3 09 F3, rather than #C3h, #09h, #F3h. Well, too bad for me, I guess. Lots more typing than I hoped, unless I can figure out how to enter two character strings and do the conversion myself.

Thanks,
smp

Why not a short program that puts the Prime in alpha mode then starts an input loop allowing you to enter the characters as you wish? The characters can then be converted to hex integers in the manner of Fortin's post above.

John
Find all posts by this user
Quote this message in a reply
04-10-2016, 01:43 PM
Post: #7
RE: Converting from one base to another?
(04-10-2016 01:17 PM)John Keith Wrote:  Why not a short program that puts the Prime in alpha mode then starts an input loop allowing you to enter the characters as you wish? The characters can then be converted to hex integers in the manner of Fortin's post above.

John

Hi John,

Yes that is just what I was thinking. Doing alpha input, then converting those into numbers to be saved in M1. I have to keep thinking of how to get the computer to do the work, not me!

smp
Find all posts by this user
Quote this message in a reply
04-10-2016, 02:08 PM
Post: #8
RE: Converting from one base to another?
For testing in the short term, an input box that only accepts strings may also help:

INPUT(
{{byte,[2]}},
"8080 Code",
{"Byte="},
{"Enter a byte as a hex number"},
{""},
{""}
);
Find all posts by this user
Quote this message in a reply
04-10-2016, 04:11 PM
Post: #9
RE: Converting from one base to another?
Hi!:

This is a youtube videos, in Spanish Language, of the use conversion, base (b, d, h, o) ... https://www.youtube.com/playlist?list=PL...Q_K6ZRiLgP

Best Regards.
informach
Find all posts by this user
Quote this message in a reply
04-10-2016, 06:24 PM
Post: #10
RE: Converting from one base to another?
Thanks very much to all who have offered their advice. I greatly appreciate your assistance.

Here is the code that I now have working:
Code:
// LoadCode
//
// Load 8080 code into M1
// Input the first location in decimal, then increment from there
// Input the 8080 code as hex bytes, convert to decimal and store
//
LOCAL loc,byte,temp;
//
EXPORT LoadCode()
BEGIN
  byte:="";
  loc:=0;
  temp:=0;
  INPUT(loc,"Starting Location","Start=","Enter the starting location in decimal",0);
  WHILE (1) DO
    INPUT(
      {{byte,[2]}},
      "8080 Code",
      {"Byte="},
      {"Enter a byte as a hex number"},
      {""},
      {""}
      );
    temp:=EXPR("#"+byte+"h");
    M1(loc):=SETBASE(temp,3);
    loc:=loc+1;
  END;
END;

Thanks again!

smp
Find all posts by this user
Quote this message in a reply
04-12-2016, 04:44 AM
Post: #11
RE: Converting from one base to another?
Hello,

The base editor is actually a quite OK convertor by itself...

in home, click on shift base twice and you enter the editor. Then you can click on edit to edit/enter the number that you want, in the base that you want, and see it in all the other bases...

you can perform shift operations directly there too (doing right and left swipes).

Cyrille

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
Find all posts by this user
Quote this message in a reply
04-12-2016, 06:50 AM (This post was last modified: 04-12-2016 06:52 AM by leprechaun.)
Post: #12
RE: Converting from one base to another?
Take a look at RealCalc for android. That is a calculator for efficient base conversion.
I do agree that the prime's base menu is alright. But if I do serious work where base conversion is needed I do not want dialoge based apps or unnecessary keystrokes. I want to get the result asap.
Dealing with integers on the prime (like 4x) needs additional characters like # so more keys are required. You can't change that easily.

I tried some keybound user programs on softmenus to make unit conversion more comfortable for me, but sooner or later the device did not behave as expected and finally I stopped my work. rpn input is best if you do things like 'one keystroke' conversions....in cas there is no bases at all and, to be honest, I still don't really like the underlying philosophy of the prime. (or I cannot get used to it 100%)

I just don't want to be upset about the prime any more... :-( Too many other problems to be solved.
Find all posts by this user
Quote this message in a reply
04-12-2016, 10:26 PM
Post: #13
RE: Converting from one base to another?
(04-12-2016 04:44 AM)cyrille de brébisson Wrote:  Hello,

The base editor is actually a quite OK convertor by itself...

Hi Cyrille,

Thanks very much for your attention and advice.

I have about 1000 8080 instructions in hexadecimal that I want to load into M1 for my 8080 emulator. That is why I was trying to create a program. I simply want to enter a starting address location for M1, and then enter hex byte after hex byte, with the program converting the hex bytes into decimal numbers and storing them sequentially into M1. I don't think the Prime capability for doing the base conversion would be good for this. If I'm missing something, please let me know.

Thanks,
smp
Find all posts by this user
Quote this message in a reply
04-12-2016, 11:07 PM
Post: #14
RE: Converting from one base to another?
Would a subroutine for your application be useful? Perhaps, something like these:

Code:

//  H2D(hex)
//
//  Hexadecimal to Decimal converter
//  Input:  Hexadecimal as quoted string ("09aF") case doesn't matter
//  Output: Decimal equivalent ==> (2479.00)

EXPORT H2D(hex)
BEGIN

  D:=0; N:=SIZE(hex);

  FOR I FROM 1 to N DO    
    D:=D+16^(N-I)*(INSTRING("0123456789ABCDEF", MID(UPPER(hex),I,1))-1);
  END;

  RETURN D;
  
END;

Code:

//  D2H(dec)
//
//  Decimal to Hexadecimal converter
//  INPUT:  Decimal value D2H(666)
//  OUTPUT: Hexadecimal value ==> "29A"

EXPORT D2H(dec)
BEGIN

  local digit,hex:="";

  WHILE 0 < dec DO
    digit:=dec MOD 16;
    dec:= FLOOR(dec/16);
    hex:= append(MID("0123456789ABCDEF",digit+1,1),hex);   
  END;

  RETURN (IFTE(hex=="","0",hex));

END;
Find all posts by this user
Quote this message in a reply
Post Reply 




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