align.inc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. ; vim:ft=fasm:
  2. ; taken from: https://github.com/tgrysztar/fasmg
  3. ; Universal implementation of ALIGN.
  4. ; Basic use:
  5. ; align 4
  6. ; Ensure specific offset relative to an aligned address:
  7. ; align 4 | 1
  8. ; Provide information for the alignment of relocatable symbols:
  9. ; align.assume rbp, 16
  10. ; element RELOCATABLE_BASE
  11. ; org RELOCATABLE_BASE
  12. ; align.assume RELOCATABLE_BASE, 10000h
  13. ; Pad with value:
  14. ; align 16, 90h
  15. ; Use a custom command to pad:
  16. ; align 16, db # dup ($ and 0FFh)
  17. define align? align
  18. align?.count = 0
  19. calminstruction align?.assume? address*, alignment*
  20. local index, symbol
  21. compute address, address
  22. compute alignment, alignment
  23. check address relativeto 0
  24. jyes absolute
  25. compute index, 0
  26. find:
  27. check index = align.count
  28. jyes update
  29. arrange symbol, align.=address#index
  30. check address relativeto symbol
  31. jyes update
  32. compute index, index + 1
  33. jump find
  34. update:
  35. arrange symbol, align.=address#index
  36. publish symbol, address
  37. arrange symbol, align.=alignment#index
  38. publish symbol, alignment
  39. compute align.count, align.count + 1
  40. exit
  41. absolute:
  42. check address mod alignment = 0
  43. jyes ok
  44. err 'false assumption about absolute address'
  45. ok:
  46. end calminstruction
  47. calminstruction align? alignment*, filler:rb #
  48. local index, address, offset
  49. compute offset, 0
  50. match alignment | offset, alignment
  51. compute alignment, alignment
  52. check alignment >= 0
  53. jyes alignment_ok
  54. err 'invalid alignment value'
  55. exit
  56. alignment_ok:
  57. compute address, $
  58. check address relativeto 0
  59. jyes align
  60. compute index, 0
  61. find:
  62. check index = align.count
  63. jyes fail
  64. arrange symbol, align.=address#index
  65. check address relativeto symbol
  66. jyes found
  67. compute index, index + 1
  68. jump find
  69. found:
  70. compute address, address - symbol
  71. arrange symbol, align.=alignment#index
  72. check symbol > 0 & symbol mod alignment = 0
  73. jyes align
  74. fail:
  75. err 'variable portion of the address is not aligned enough'
  76. exit
  77. align:
  78. compute alignment, (alignment-1) - (address-offset+alignment-1) mod alignment
  79. local prefix, suffix
  80. match prefix? # suffix?, filler
  81. jyes custom
  82. arrange alignment, =db alignment =dup (filler)
  83. assemble alignment
  84. exit
  85. custom:
  86. arrange alignment, prefix alignment suffix
  87. assemble alignment
  88. end calminstruction