prt0.as 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #
  2. # This file is part of the Free Pascal run time library.
  3. # Copyright (c) 2011 by Marcus Sackrow
  4. #
  5. # Startup code for AROS/i386 RTL
  6. #
  7. # See the file COPYING.FPC, included in this distribution,
  8. # for details about the copyright.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY;without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. #
  14. # AROS Startup Code
  15. .text
  16. .align 4
  17. .section .aros.startup, "ax"
  18. .globl _start
  19. .globl start
  20. .globl _haltproc
  21. .globl haltproc
  22. _start:
  23. start:
  24. /* Save the exec library base */
  25. movl 12(%esp),%eax
  26. movl %eax,_ExecBase
  27. /* Save the command line pointer length to CommandLineLen */
  28. movl 8(%esp),%eax
  29. movl %eax,CommandLineLen
  30. /* Save the command line pointer to CommandLine */
  31. movl 4(%esp),%eax
  32. movl %eax,CommandLine
  33. /* save all registers */
  34. pushal
  35. /* get the pointer to current stack */
  36. movl _ExecBase,%eax
  37. pushl %eax
  38. pushl $0
  39. call *-196(%eax) /* FindTask(nil) */
  40. addl $8,%esp
  41. movl 64(%eax),%ecx /* SPUpper */
  42. subl 60(%eax),%ecx /* SPLower */
  43. /* Uncomment the symbol line below to force system stack use,
  44. and do not attempt to reallocate stack if the system-provided
  45. stack is smaller than the user specified */
  46. # FORCE_USE_SYSTEM_STACK:
  47. .ifndef FORCE_USE_SYSTEM_STACK
  48. /* Check if we need a new stack
  49. Only allocate a new stack if the system-provided
  50. stack is smaller than the one set compile time */
  51. cmpl __stklen,%ecx
  52. jl _allocStack
  53. .endif
  54. movl %ecx,__stklen /* Store the new stack size */
  55. xorl %eax,%eax
  56. movl %eax,StackAreaPtr /* Clear the stackAreaPtr for exit test */
  57. jmp _noAllocStack
  58. _allocStack:
  59. /* Allocating new stack */
  60. movl _ExecBase,%eax
  61. pushl %eax
  62. pushl $0 /* MEMF_ANY */
  63. pushl __stklen
  64. call *-456(%eax) /* AllocVec() */
  65. addl $12,%esp
  66. testl %eax,%eax
  67. je __exit
  68. movl %eax,StackAreaPtr
  69. /* Setting up StackSwap structure, and do the StackSwap */
  70. lea StackSwapStruct,%ecx
  71. movl %eax,(%ecx) /* Bottom of the stack */
  72. addl __stklen,%eax
  73. movl %eax,4(%ecx) /* Top of the stack */
  74. movl %eax,8(%ecx) /* Initial stackpointer */
  75. movl _ExecBase,%eax
  76. pushl %eax
  77. lea StackSwapArgs,%ebx
  78. pushl %ebx
  79. lea _initProc,%ebx
  80. pushl %ebx
  81. pushl %ecx
  82. call *-536(%eax) /* NewStackSwap() */
  83. addl $16,%esp
  84. jmp _afterMain
  85. _noAllocStack:
  86. call _initProc
  87. _afterMain:
  88. /* check if we have a StackArea to free */
  89. movl StackAreaPtr,%eax
  90. testl %eax,%eax
  91. je __exit
  92. _freeStack:
  93. /* Freeing up stack area */
  94. movl _ExecBase,%eax
  95. pushl %eax
  96. pushl StackAreaPtr
  97. call *-460(%eax) /* FreeVec() */
  98. addl $8,%esp
  99. __exit:
  100. /* get back all registers */
  101. popal
  102. /* get returncode */
  103. movl operatingsystem_result,%eax
  104. /* bye bye */
  105. ret
  106. /* This function is getting called from NewStackSwap() or
  107. as standalone if we don't do stackswap */
  108. _initProc:
  109. pushal
  110. /* Save stack pointer */
  111. movl %esp,STKPTR
  112. /* call the main function */
  113. call PASCALMAIN
  114. /* entry to stop the program */
  115. _haltproc:
  116. haltproc:
  117. /* restore the old stackPtr and return */
  118. movl STKPTR,%esp
  119. popal
  120. ret
  121. /*----------------------------------------------------*/
  122. .bss
  123. .global CommandLineLen # byte length of command line
  124. .global CommandLine # comandline as PChar
  125. .global STKPTR # Used to terminate the program, initial SP
  126. .global _ExecBase # exec library base
  127. .align 4
  128. CommandLine: .skip 4
  129. CommandLineLen: .skip 4
  130. STKPTR: .skip 4
  131. _ExecBase: .skip 4
  132. StackAreaPtr: .skip 4
  133. StackSwapStruct: .skip 12
  134. StackSwapArgs: .skip 32