prt0.as 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. movl -196(%eax),%eax /* FindTask(nil) */
  40. call *%eax
  41. addl $8,%esp
  42. movl 64(%eax),%ecx /* SPUpper */
  43. subl 60(%eax),%ecx /* SPLower */
  44. /* Uncomment the symbol line below to force system stack use,
  45. and do not attempt to reallocate stack if the system-provided
  46. stack is smaller than the user specified */
  47. # FORCE_USE_SYSTEM_STACK:
  48. .ifndef FORCE_USE_SYSTEM_STACK
  49. /* Check if we need a new stack
  50. Only allocate a new stack if the system-provided
  51. stack is smaller than the one set compile time */
  52. cmpl __stklen,%ecx
  53. jl _allocStack
  54. .endif
  55. movl %ecx,__stklen /* Store the new stack size */
  56. xorl %eax,%eax
  57. movl %eax,StackAreaPtr /* Clear the stackAreaPtr for exit test */
  58. jmp _noAllocStack
  59. _allocStack:
  60. /* Allocating new stack */
  61. movl _ExecBase,%eax
  62. pushl %eax
  63. pushl $0 /* MEMF_ANY */
  64. pushl __stklen
  65. movl -456(%eax),%eax /* AllocVec() */
  66. call *%eax
  67. addl $12,%esp
  68. testl %eax,%eax
  69. je __exit
  70. movl %eax,StackAreaPtr
  71. /* Setting up StackSwap structure, and do the StackSwap */
  72. lea StackSwapStruct,%ecx
  73. movl %eax,(%ecx) /* Bottom of the stack */
  74. addl __stklen,%eax
  75. movl %eax,4(%ecx) /* Top of the stack */
  76. movl %eax,8(%ecx) /* Initial stackpointer */
  77. movl _ExecBase,%eax
  78. pushl %eax
  79. lea StackSwapArgs,%ebx
  80. pushl %ebx
  81. lea _initProc,%ebx
  82. pushl %ebx
  83. pushl %ecx
  84. movl -536(%eax),%eax /* NewStackSwap() */
  85. call *%eax
  86. addl $16,%esp
  87. jmp _afterMain
  88. _noAllocStack:
  89. call _initProc
  90. _afterMain:
  91. /* check if we have a StackArea to free */
  92. movl StackAreaPtr,%eax
  93. testl %eax,%eax
  94. je __exit
  95. _freeStack:
  96. /* Freeing up stack area */
  97. movl _ExecBase,%eax
  98. pushl %eax
  99. pushl StackAreaPtr
  100. movl -460(%eax),%eax /* FreeVec() */
  101. call *%eax
  102. addl $8,%esp
  103. __exit:
  104. /* get back all registers */
  105. popal
  106. /* get returncode */
  107. movl operatingsystem_result,%eax
  108. /* bye bye */
  109. ret
  110. /* This function is getting called from NewStackSwap() or
  111. as standalone if we don't do stackswap */
  112. _initProc:
  113. pushal
  114. /* Save stack pointer */
  115. movl %esp,STKPTR
  116. /* call the main function */
  117. call PASCALMAIN
  118. /* entry to stop the program */
  119. _haltproc:
  120. haltproc:
  121. /* restore the old stackPtr and return */
  122. movl STKPTR,%esp
  123. popal
  124. ret
  125. /*----------------------------------------------------*/
  126. .bss
  127. .global CommandLineLen # byte length of command line
  128. .global CommandLine # comandline as PChar
  129. .global STKPTR # Used to terminate the program, initial SP
  130. .global _ExecBase # exec library base
  131. .align 4
  132. CommandLine: .skip 4
  133. CommandLineLen: .skip 4
  134. STKPTR: .skip 4
  135. _ExecBase: .skip 4
  136. StackAreaPtr: .skip 4
  137. StackSwapStruct: .skip 12
  138. StackSwapArgs: .skip 32