Guest User

Untitled

a guest
Sep 29th, 2019
280
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # define a set of numbers in the data section of the exe.
  2. # 4 byte unsigned integer array.
  3. static a_list_of_numbers: ui32[] = {
  4. 0x4a, 0x23, 0x0f, 0x41, 0x3e,
  5. 0x28, 0x1a, 0x61, 0x16, 0x3c,
  6. 0x0d, 0x1e, 0x37, 0x5d, 0x10,
  7. 0x54, 0x17, 0x27, 0x10, 0x3e,
  8. 0x19, 0x3b, 0x0c, 0x45, 0x30,
  9. 0x19, 0x61, 0x49, 0x3b, 0x0a,
  10. 0x06, 0x54, 0x32, 0x02, 0x4a,
  11. 0x0e, 0x61, 0x35, 0x0c, 0x3b,
  12. 0x26, 0x0e, 0x2a, 0x1b, 0x5e,
  13. 0x37, 0x40, 0x14, 0x29, 0x0f,
  14. 0x56, 0x47, 0x23, 0x3a, 0x27,
  15. 0x15, 0x53, 0x21, 0x4a, 0x07,
  16. 0x4e, 0x5f, 0x20, 0x13, 0x07,
  17. 0x40, 0x4d, 0x24, 0x14, 0x51,
  18. 0x28, 0x39, 0x27, 0x56, 0x34,
  19. 0x10, 0x0a, 0x03, 0x15, 0x1b,
  20. 0x52, 0x2b, 0x62, 0x2a, 0x2f,
  21. 0x0b, 0x37, 0x27, 0x14, 0x58,
  22. 0x2b, 0x0a, 0x58, 0x01, 0x10,
  23. 0x35, 0x1e, 0x2e, 0x61, 0x12,
  24. }
  25.  
  26. # this is the entry point. things that are defined can be addressed
  27. # via a pathing scheme similar to file systems. './' starts at the
  28. # local group, '~/' starts at the root. main here is the interface
  29. # definition (standard C main interface in this case). 'x86-64' here
  30. # specifies which instruction set and required extensions are being used.
  31. procedure start: ./libc/main x86-64 {
  32. # this block of register definitions is bound to the scope it
  33. # is declared in (this case it is the implicit procedure root).
  34. # these registers are 'mapped' and 'unmapped' at the start and
  35. # end of the scope (can also be done manually).
  36. registers {
  37. accumulator: ui32
  38. t0: to ui32
  39. }
  40.  
  41. # here we compute the address to pass to sum_ui32. the 'at' syntax
  42. # can be seen as a dereferencing operator, or the '[reg]' syntax in
  43. # traditional assemblers. An 'at' chases a pointer type, which is
  44. # specified using a matching 'to' in the format declaration. A 'to'
  45. # is implicit when accessing statics.
  46. lea t0, at ./a_list_of_numbers
  47.  
  48. # this call expands to a bit more than just the x86 call instruction.
  49. # using the interface specified, the stack and register swapping is
  50. # done automatically.
  51. invoke ./my_cool_math_lib/sum_ui32 with
  52. accumulator = accumulator,
  53. nums = t0,
  54. num_count = ./a_list_of_numbers.%element_count
  55.  
  56. # set the return parameter specified by the 'main' interface. the 'as'
  57. # syntax here is similar to a cast but does not result in any actual
  58. # conversion. this is purely to satisfy the format matcher.
  59. mov return, accumulator as si32
  60. }
  61.  
  62. # simple namespacing for top level definitions.
  63. push_group my_cool_math_lib
  64.  
  65. # the 'auto' here specifies no explicit interface. the interface is instead
  66. # defined automatically by the parameters specified in the body.
  67. procedure sum_ui32: auto x86-64 {
  68. parameters {
  69. accumulator: ui32
  70. nums: to ui32
  71. nums_count: ui32
  72. }
  73. registers {
  74. nums_cur: to ui32
  75. nums_end: to ui32
  76. }
  77.  
  78. # the second operand here is an expression which evals to a 0
  79. # immediate in the instruction.
  80. mov accumulator, ui32(0)
  81.  
  82. # get some bounds for the iteration. the correct instruction variant
  83. # is chosen based on the operand formats.
  84. mov nums_cur, nums
  85. mov nums_end, nums_count
  86. mul nums_end, ui32(8)
  87. add nums_end, nums_cur
  88.  
  89. # this opens a new scope where registers and a new stack frame can be
  90. # defined (don't do either here since we only use this to jump).
  91. # %start and %end are defined to be jump targets for branching.
  92. scope {
  93. add accumulator, at nums_cur
  94. add nums_cur, ui32(8)
  95. cmp nums_cur, nums_end
  96. jmp<ne> %start
  97. }
  98.  
  99. # note there is no return here. the accumulator parameter is 'persistent'
  100. # in the default calling convention. in and out parameters are also possible
  101. # to construct a more c-like calling convention.
  102. }
RAW Paste Data