On Fri, May 9, 2008 at 10:22 PM, Glen F. Pankow <Glen.F.Pankow / noaa.gov> wrote:
> Chris Shea wrote:
>> ... I hope others share some.
>
> Below is a (very ugly) machine to convert binary to octal,
> since it seems binary is the popular test representation.
>
That's an interesting solution. When I wrote my binary to hex
converter I took a fairly different approach.  I think binary is
popular because it limits the number of state transitions required.
Compare my seekLSB state to the exitHex state - both do the same
thing, but the one that supports hex is 6 times bigger.
-Adam

--------------
## bin2hex.tm
## converts binary to hex
## by creating an accumulator (the store)
## to the left of the input, and moving one
## bit at a time from the input to the store

#first find the MSB of the input
seekMSB 1 seekMSB 1 L
seekMSB 0 seekMSB 0 L
seekMSB _ makeStore _  L

#creates the storage for the hex value:  tape:=  0_<input>
makeStore _ moveRight 0 R

#goes to lsb of input
moveRight _ seekLSB _ R
seekLSB 1 seekLSB 1 R
seekLSB 0 seekLSB 0 R
seekLSB _ dec _ L

#decrement binary, starting from LSB
#when we get to a 1 we are done
#if we get to a _ we must have started with all 0s .
dec 0 dec 1 L
dec 1 findStore 0 L
dec _ clearRight _ R     #so just cleanup the original input

#seeks back to the store
findStore 0 findStore 0 L
findStore 1 findStore 1 L
findStore _ addToStore _ L

#adds 1 to store
addToStore _ exitHex 1 R
addToStore 0 exitHex 1 R
addToStore 1 exitHex 2 R
addToStore 2 exitHex 3 R
addToStore 3 exitHex 4 R
addToStore 4 exitHex 5 R
addToStore 5 exitHex 6 R
addToStore 6 exitHex 7 R
addToStore 7 exitHex 8 R
addToStore 8 exitHex 9 R
addToStore 9 exitHex A R
addToStore A exitHex B R
addToStore B exitHex C R
addToStore C exitHex D R
addToStore D exitHex E R
addToStore E exitHex F R
addToStore F addToStore  0 L #carry

#move head back into the input
exitHex  0 exitHex 0 R
exitHex 1 exitHex 1 R
exitHex 2 exitHex 2 R
exitHex 3 exitHex 3 R
exitHex 4 exitHex 4 R
exitHex 5 exitHex 5 R
exitHex 6 exitHex 6 R
exitHex 7 exitHex 7 R
exitHex 8 exitHex 8 R
exitHex 9 exitHex 9 R
exitHex A exitHex A R
exitHex B exitHex B R
exitHex C exitHex C R
exitHex D exitHex D R
exitHex E exitHex E R
exitHex F exitHex  F R
exitHex  _ seekLSB _ R

#erase a binary string to the right
clearRight 0 clearRight _ R
clearRight 1 clearRight _ R
clearRight _ done _ L  #done cleanup, ready to print