brainfuck.S 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
  2. │vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│
  3. ╞══════════════════════════════════════════════════════════════════════════════╡
  4. │ Copyright 2021 Justine Alexandra Roberts Tunney │
  5. │ Some size optimisations by Peter Ferrie │
  6. │ │
  7. │ Permission to use, copy, modify, and/or distribute this software for │
  8. │ any purpose with or without fee is hereby granted, provided that the │
  9. │ above copyright notice and this permission notice appear in all copies. │
  10. │ │
  11. │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
  12. │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
  13. │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
  14. │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
  15. │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
  16. │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
  17. │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
  18. │ PERFORMANCE OF THIS SOFTWARE. │
  19. ╚─────────────────────────────────────────────────────────────────────────────*/
  20. // compliant brainf*#k in 99 bytes
  21. // ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
  22. // ..+++.>>.<-.<.+++.------.--------.>>+.>++.[]$
  23. .code16
  24. .globl _start
  25. _start: mov $0x7e00,%di
  26. push %di
  27. Load: xor %ax,%ax
  28. int $0x16
  29. stosb
  30. cmp $'[',%al
  31. je Lsb
  32. cmp $']',%al
  33. je Rsb
  34. cmp $'$',%al
  35. jne Load
  36. pop %si
  37. Brain: lodsb
  38. cbw
  39. mov $0x0e,%bh
  40. mov (%di),%bl
  41. sub $'+',%al
  42. jz Inc # + 43
  43. dec %ax
  44. jz Get # , 44
  45. dec %ax
  46. jz Dec # - 45
  47. dec %ax
  48. jz Put # . 46
  49. cmp $'>'-'.',%al
  50. je Right # > 60 14
  51. cmp $'<'-'.',%al
  52. je Left # < 62 16
  53. cmp $']'-'.',%al
  54. je Loop # ] 91 45
  55. cmp $'['-'.',%al
  56. jne Brain # [ 93 47
  57. Do: cmp $1,%bl
  58. sbb %bl,%bl
  59. Loop: test %bl,%bl
  60. lodsw
  61. jz Brain
  62. xchg %ax,%si
  63. inc %si
  64. inc %si
  65. .byte 0x80
  66. Inc: incb (%di)
  67. jmp Brain
  68. Get: int $0x16
  69. stosb
  70. Left: dec %di
  71. .byte 0x80
  72. Dec: decb (%di)
  73. .byte 0x3C
  74. Right: inc %di
  75. .byte 0x84
  76. Put: xchg %bx,%ax
  77. int $0x10
  78. jmp Brain
  79. Rsb: pop %si
  80. mov %di,(%si)
  81. xchg %si,%ax
  82. .byte 0x3c
  83. Lsb: push %di
  84. stosw
  85. jmp Load
  86. Sig: .fill 510 - (. - _start), 1, 0xce
  87. .word 0xAA55
  88. .type Sig,@object