Date: 19970813 From: Calvin Krusen Organization: MEECO Inc. To: Motorola 68hc11 mailing list Subject: 2 byte word to ASCII string I need an assembly procedure to convert an unsigned integer(2 BYTES) to a 'C' style ASCII string. For example: value RMB 2 string RMB 5 ;5 digits no null necessary & OK if leading 0's ; set value = $8a4e = 35406 lda #8a sta value lda #4e sta value+1 jsr myst_sub ; now data starting at string = '3','5','4','0','6' I need an algoritm for myst_sub(abbreviated mystery subroutine). Now the killer. I am using the 68HC05 which does not have a DIV instruction. It does however have the full compliment of bit manipulations and a half carry bit in the CCR. I remember seeing an algorithm for 24/32 bit to BCD a while ago and can't seem to find it. Any help would be greatly appreciated. -- Calvin Krusen ================== Director of Engineering MEECO Inc. 250 Titus Ave Warrington, PA 18976 (USA) 215/343-6600 Date: 19970813 From: "Forrest J. Cavalier III" To: Motorola 68HC11 mailing list Subject: Re: 2 byte word to ASCII string > From: Calvin Krusen > Now the killer. I am using the 68HC05 which does not have a DIV > instruction. It does however have the full compliment of bit > manipulations and a half carry bit in the CCR. You are better off asking the question on the MC6805 list. There are lots of 16 bit instructions in the 11 which would just be "too tempting" to pass up when suggesting a solution. But basically you need to decide if you want "fast" or "compact." Then you create a table of values and do multiple 2 byte subtractions, (yes -- a pain on the HC05). The compact version has a table: FDB 10000 FDB 1000 FDB 100 FDB 10 (And you don't need an FDB 1) And you use this table to do multiple subtractions, which can take a long time if you have 59999 to convert. The "fast" version has a table with more entries, and you get one bit per subtraction. FDB 40000 FDB 20000 FDB 10000 FDB 8000 FDB 4000 FDB 2000 FDB 1000 FDB 800 FDB 400 FDB 200 FDB 100 FDB 80 FDB 40 FDB 20 FDB 10 There are some fancy ways to optimize even the "fast" version for speed, saving a 16 bit compare on each step, but you should be able to figure out the rest. And chances are your code already has bits and pieces of 16 bit math already which you can use. Just remember, all you ever need are two instructions: INC/and conditional branch -- everything is computable. Personally, I've even done log/exp conversions to/from BCD on the HC05. Forrest Cavalier, Mib Software http://www.mibsoftware.com Date: 19970814 From: Bill Keenan Organization: NMIT To: Motorola 68HC11 mailing list Subject: Re: 2 byte word to ASCII string Hi Kelvin, Here is a C routine incase it helps. Bill Keenan. Attachment Converted: "RECEIVED\Itoa.c" /* Integer to ASCII for signed decimal integers. */ static int next; static char qbuf[8]; char *itoa(n) int n; { register int r, k; int flag = 0; next = 0; if (n < 0) { qbuf[next++] = '-'; n = -n; } if (n == 0) { qbuf[next++] = '0'; } else { k = 10000; while (k > 0) { r = n/k; if (flag || r > 0) { qbuf[next++] = '0' + r; flag = 1; } n -= r * k; k = k/10; } } qbuf[next] = 0; return(qbuf); } [This may be another version than that Bill mailed, Jaap] Date: 19970813 From: Ryan Minnig To: Motorola 68HC11 mailing list Subject: Re: 2 byte word to ASCII string Calvin, Try the Mot FTP file server. I found routines similar to what you need, only these are for the 68hc11. I think they use some type of consecutive subtract 10 to find the ASCII chars. If you need the files I can email then direct to you or show you what I have been using in my 68hc11 source. Ryan Date: 19970814 From: Robert L. Smith To: Motorola 68HC11 mailing list Subject: Re: 2 byte word to ASCII string Calvin, Here is a routine I used several years ago for binary to BCD conversion. It was originally written for the MC6809 so should convert readily to the 'hc11. I forget where I originally found the "ADD THREE" algorithm, but I think it is widely documented in various micro-C books. I have converted it to several processors and into C with much success. ************************************************************************ *This routine converts an unsigned integer in X *to a four digit BCD result in D *The standard "Add 3" algorithm is used *define stack map msb equ 0 define work space layout lsb equ 1 hndthd equ 2 untten equ 3 binbcd: pshs x,d save x & make room for result leas -2,s make room for argument bucket stx msb,s store argument in work bucket clr untten,s clear result buckets clr hndthd,s ldx #16 set up loop count b2cdlp lda untten,s compare units position tfr a,b anda #$0f suba #$05 bmi b2cdat addb #$03 b2cdat tfr b,a compare tens anda #$f0 suba #$50 bmi b2cdbt addb #$30 b2cdbt stb untten,s lda hndthd,s compare hundreds tfr a,b anda #$0F suba #$05 bmi b2cdct addb #$03 b2cdct tfr b,a compare thousands anda #$F0 suba #$50 bmi b2cddt addb #$30 b2cddt stb hndthd,s asl lsb,s now shift next bit in rol msb,s rol untten,s into the result field rol hndthd,s leax -1,x decrement count and bne b2cdlp do em all leas 2,s restore stack pointer puls d,x,pc load result, restore x and return end ************************************************************************* Feel free to write if you have questions. Good Luck -- Bob Smith ---- Avoid computer viruses -- Practice safe hex -------------- * * Specializing in small, cost effective embedded control systems * * Robert L. (Bob) Smith Smith Machine Works, Inc. Date: 19970814 From: Shubel To: Motorola 68HC11 mailing list Subject: Re: 2 byte word to ASCII string >I need an assembly procedure to convert an unsigned integer(2 BYTES) to >a 'C' style ASCII string. Do you have an old (70's vintage) book on assembly language for any micro? For example, the Z80 was very popular then and there were many assembly- language-bibles written for it. I can't stress enough that you go out and buy one of these "outdated" books for 5-7 dollars. Most of these books cover the basics of assembly language programming and they can often be used as a source for instruction on assembly language programming techniques. Forget new x86 assembly language books. They won't go into subjects like: "how do you divide without a divide instruction". Cheers