6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Thu May 09, 2024 7:17 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Programming help needed
PostPosted: Sun Aug 31, 2003 2:32 am 
Offline

Joined: Sun Aug 31, 2003 2:14 am
Posts: 1
Please somebody help me with this problem. I am finding it hard because I am totally new to this language, otherwise it should be pretty straight forward!

I need a 6502 program that asks from user to enter a number between 0 and 4000, and converts it to the corresponding roman numeral (eg 7 ->VII , 789 ->DCCLXXXIX, etc) as screen output.

Any help would be greatly appreciated

PLDC


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Aug 31, 2003 7:49 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8432
Location: Southern California
We don't normally like to just people's homework for them, but this should give some insight into the job. I'll assume that if it is an assignment for a class, you've been introduced to the instruction set, at least of the NMOS 6502 if not the CMOS 65c02 (which has more instructions and addressing modes).

I think the most efficient way to form the output string would probably be to have a string look-up table for each digit. For example, if the ones' digit is a 1, the string is "I"; if it's a 4, it's "IV", etc.. The tens' place will have another look-up table, so if you have a 1 in the tens' place, the record in the table will a one-byte-long string that just says "X". Indexing into these tables will be easiest if each string building block like this is given the same number of bytes, which will probably be best made three bytes for characters and one for length. (I don't think any need four characters, do they?) Obviously some bytes will go unused since not all the roman-numberal building blocks are 3 characters long. If 3+1 is adequate, you take your digit, shift left twice (using ASL) to multiply by four, put that index number in X (using TAX), and do LDA digit_string_table,X. As you read each byte, you increment X (with INX) to get the next byte. A zero in any place gives you a zero-length string to add to the output string. Since you'll be adding these fragments to the final result string you're constructing, you'll need an index register for that one too. Instead of storing X to memory and then modifying, restoring, etc., use Y for the other one. For the simple indexing, X and Y are interchangeable to some extent. For example, there is both an LDA abs,X and an LDA abs,Y, and both an STA abs,X and an STA abs,Y

Now as for getting the digit from the number-- If you're working in decimal mode (which is much more practical on the 6502 than on many other processors), you can get the digit pretty easily since each four bits contains a decimal digit and you can isolate it with AND#$0F, or AND#$F0 LSR LSR LSR LSR. What may make it more difficult for the beginner however is that computers usually keep and work the numbers in hex, and only convert between hex and decimal when it comes time for human-readable I/O. If you have to do it in hex, you can get each digit, starting with the ones' place, by dividing the whole number by ten and looking at the remainder. You repeat this process until there's nothing left of the number (ie, it is 0). In fact, that's the way Forth easily converts to any base. It can just as easily convert to base 7 or base 13 if you had some odd reason to do so. Division routines are not trivial though, so I'll direct you to my division entry in the 6502.org source code repository, at http://www.6502.org/source/integers/umm ... modfix.htm . The comments are very complete.

I haven't had the misfortune of having to think about roman numerals in a long time, so hopefully I'm not forgetting something important.

Garth


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Aug 31, 2003 4:53 pm 
Offline

Joined: Fri Aug 30, 2002 2:05 pm
Posts: 347
Location: UK
GARTHWILSON wrote:
Indexing into these tables will be easiest if each string building block like this is given the same number of bytes, which will probably be best made three bytes for characters and one for length.

Don't bother with the length byte, just fill the unused characters with $00 and then exit the string output routine after 4 characters or on the first null byte.

GARTHWILSON wrote:
(I don't think any need four characters, do they?)

8 = VIII
80 = LXXX
800 = DCCC

I did this many years ago to print out BIG roman numbers on a teletype, each character being 69 characters high and made of @'s. They read sideways if you see what I mean. I could also print them to punch tape with each character being a 5x7 matrix.

Lee.


Top
 Profile  
Reply with quote  
 Post subject: Hehe
PostPosted: Thu Sep 04, 2003 7:00 am 
Offline

Joined: Thu Sep 04, 2003 6:55 am
Posts: 1
As this guy's lecturer (and the one who set the roman numerals assignment)... I find this all a trifle amusing - wanna breach their privacy and send me their email address? :D or just yank their chain and tell them that you did...

They didn't tell you that they are supposed to read the number in as a string, and so all of the decimal mode, and everything related discussion is unnecessary... hopefully they convert a string to a big number and then convery it back - 'cos then they'll be easy to spot :wink:

Anyway, thanks for not posting the solution :)


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Sep 04, 2003 7:25 am 
Offline

Joined: Thu Sep 04, 2003 7:20 am
Posts: 1
Hi I am trying to cheat on this assignment. Is there any way that this solution could be changed so that it does not convert a string to a big number and then convert it back.

Thanks :evil:


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Sep 04, 2003 11:10 am 
Offline

Joined: Fri Aug 30, 2002 2:05 pm
Posts: 347
Location: UK
Yes, as you read in each character of the input number you use that as the index to the numeral table, after suitable checking and conversion. There is no need then to convert the whole string to a binary number.

Don't forget that you'll have to count the total number of characters in the input string to work out the power of 10 that each one represents.

Lee.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 14 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: