Jump to content
IGNORED

Assembly - String to Dec?


Recommended Posts

I'm learning 6502 assembly and I've been struggling with how to do this and could use some help. I've read countless articles and searched and can't figure out how to do it.

 

I've taken input from keyboard and stored it in a memory location successfully. The memory space for the variable, after assembly, is at location $3797. I have a pointer to that location in $CC/$CD. The input holds a number input by the user, say 35 which takes two bytes. So $3797 ends up with "3", "5", and $0 as the third byte from my input routine.

 

Now I want to use the value of that input and perform operations like add/sub. The user could have entered 0 through 9 and just consume one byte, but its not in decimal form.

 

How do I convert that input from the 1,2,3 (etc) byte string into a number I can work with?

 

Then that brings to mind, how would I go the other direction? From decimal to printable string?

 

Maybe I've just not found the correct reference articles. Any help or pointing me in the right direction would be appreciated.

Edited by Ripdubski
Link to comment
Share on other sites

Each significant digit after the lowest has to be multiplied by 10.

 

So if you have 3, 5 decimal stored in those 2 locations you need to multiply the 3 by 10.

 

There's no multiply in 6502 so you can either do a loop and repetitively add, or in some cases there's easy shortcuts.

 

The shift instructions, e.g. ASL A - gives a multiply by 2 operation.

 

What you could do is multiply by 2, save that away, then multiply again by 2 twice over (giving * 8 )

 

Then just add the ( *8 )to the ( *2 ) result and you have the number multiplied by 10.

 

When using such tricks you just have to be sure there won't be overflow/loss of digits but in this case you're safe since you're only keeping values 0 thru 9 in a single byte.

 

Then of course, add the least significant digit to the result, ie add the 5 to the result which should give 35 in binary.

 

The OS Floating-Point has routines which you could investigate using - there's integer to BCD and back although you'd need to prepare the BCD number from what you have. But in such trivial cases, a small amount of coding can achieve what you need.

Edited by Rybags
Link to comment
Share on other sites

All depends on what you want to do with it, how big numbers you will be using etc. For this two digit example you can easily use look up table...

  clc
  ldx higher_digit
  lda multiply10_table,x
  adc lower_digit
  ...

multiply10_table .byte 0,10,20,30,40,50,60,70,80,90 
Edited by MaPa
Link to comment
Share on other sites

 

All depends on what you want to do with it, how big numbers you will be using etc. For this two digit example you can easily use look up table...

  clc
  ldx higher_digit
  lda multiply10_table,x
  adc lower_digit
  ...

multiply10_table .byte 0,10,20,30,40,50,60,70,80,90 

The numbers will be less than 255, or should be. I havent put any sanity checks against the input so a non digit could fubar it as well. But since this is a learning exercise and its only me using it, im not too concerned.

 

The ideas are steering me correctly i think. I was kind of hoping it would be a little easier. Thanx!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...