prt0.as 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. pushl $0
  80. lea _initProc,%ebx
  81. pushl %ebx
  82. pushl %ecx
  83. movl -536(%eax),%eax /* NewStackSwap() */
  84. call *%eax
  85. addl $16,%esp
  86. jmp _afterMain
  87. _noAllocStack:
  88. call _initProc
  89. _afterMain:
  90. /* check if we have a StackArea to free */
  91. movl StackAreaPtr,%eax
  92. testl %eax,%eax
  93. je __exit
  94. _freeStack:
  95. /* Freeing up stack area */
  96. movl _ExecBase,%eax
  97. pushl %eax
  98. pushl StackAreaPtr
  99. movl -460(%eax),%eax /* FreeVec() */
  100. call *%eax
  101. addl $8,%esp
  102. __exit:
  103. /* get back all registers */
  104. popal
  105. /* get returncode */
  106. movl operatingsystem_result,%eax
  107. /* bye bye */
  108. ret
  109. /* This function is getting called from NewStackSwap() or
  110. as standalone if we don't do stackswap */
  111. _initProc:
  112. pushal
  113. /* Save stack pointer */
  114. movl %esp,STKPTR
  115. /* call the main function */
  116. call PASCALMAIN
  117. /* entry to stop the program */
  118. _haltproc:
  119. haltproc:
  120. /* restore the old stackPtr and return */
  121. movl STKPTR,%esp
  122. popal
  123. ret
  124. /*----------------------------------------------------*/
  125. .bss
  126. .global CommandLineLen # byte length of command line
  127. .global CommandLine # comandline as PChar
  128. .global STKPTR # Used to terminate the program, initial SP
  129. .global _ExecBase # exec library base
  130. .align 4
  131. CommandLine: .skip 4
  132. CommandLineLen: .skip 4
  133. STKPTR: .skip 4
  134. _ExecBase: .skip 4
  135. StackAreaPtr: .skip 4
  136. StackSwapStruct: .skip 12