Advertisement
milanmetal

[DTM] Increment bit-string / Overflow handled

Oct 30th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. // Input: a binary number n, for example 1101
  2. // Ouput: 01101
  3. //
  4. // Author: Milan Nedic
  5. //
  6.  
  7. name: Increment bit-string (handles overflow)
  8. init: to_the_right
  9. accept: qAccept
  10.  
  11. to_the_right,0
  12. to_the_right,0,>
  13.  
  14. to_the_right,1
  15. to_the_right,1,>
  16.  
  17. // to_the_right takes me to the first empty position on the right
  18.  
  19. // empty found? put the placeholder
  20. // and get back one place
  21. to_the_right, _
  22. to_the_left, x, <
  23.  
  24. // move back through all placeholders
  25. to_the_left, x
  26. to_the_left, x, <
  27.  
  28. // you saw 0 while moving back?
  29. // bring it forward one place
  30. to_the_left, 0
  31. bring_0, x, >
  32.  
  33. bring_0, 0
  34. bring_0, x, >
  35.  
  36. bring_0, x
  37. to_the_left, 0, <
  38.  
  39. // ---------------------
  40. // you saw 1 while moving back?
  41. // bring it forward one place
  42. to_the_left, 1
  43. bring_1, x, >
  44.  
  45. bring_1, 1
  46. bring_1, x, >
  47.  
  48. bring_1, x
  49. to_the_left, 1, <
  50.  
  51. to_the_left,_
  52. clear_placeholder,_, >
  53.  
  54. clear_placeholder, x
  55. init_incr, 0, >
  56.  
  57. // increment initialization, move
  58. // to the end again.
  59. init_incr, 0
  60. init_incr, 0, >
  61.  
  62. init_incr, 1
  63. init_incr, 1, >
  64.  
  65. init_incr, _
  66. increment_lsb, _, <
  67.  
  68. // if prior to incr you read 0 on LSB bit
  69. // you simply add 1 and the process is done.
  70. // go to the left.
  71. increment_lsb, 0
  72. incr_done, 1, <
  73.  
  74. incr_done, 0
  75. incr_done, 0, <
  76.  
  77. incr_done, 1
  78. incr_done, 1, <
  79.  
  80. incr_done, _
  81. dAccept, _,-
  82.  
  83.  
  84. // if you land on 1, then you increment it
  85. // and carry 1, so the zero is printed
  86. increment_lsb, 1
  87. carry_1, 0, <
  88.  
  89. carry_1, 0
  90. incr_done, 1, <
  91.  
  92. carry_1, 1
  93. carry_1, 0, <
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement