prt0.asm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ; nasm -f obj -o prt0.o prt0.asm
  2. cpu 8086
  3. segment text use16
  4. extern PASCALMAIN
  5. extern dos_psp
  6. extern dos_version
  7. extern _edata ; defined by WLINK, indicates start of BSS
  8. extern _end ; defined by WLINK, indicates end of BSS
  9. extern __stklen
  10. extern __stkbottom
  11. extern __nearheap_start
  12. extern __nearheap_end
  13. ..start:
  14. ; init the stack
  15. mov bx, dgroup
  16. mov ss, bx
  17. mov sp, stacktop
  18. ; zero fill the BSS section
  19. mov es, bx
  20. mov di, _edata wrt dgroup
  21. mov cx, _end wrt dgroup
  22. sub cx, di
  23. jz no_bss
  24. xor al, al
  25. rep stosb
  26. no_bss:
  27. ; save the Program Segment Prefix
  28. push ds
  29. ; init DS
  30. mov ds, bx
  31. ; pop the PSP from stack and store it in the pascal variable dos_psp
  32. pop ax
  33. mov word [dos_psp], ax
  34. ; get DOS version and save it in the pascal variable dos_version
  35. mov ax, 3000h
  36. int 21h
  37. xchg al, ah
  38. mov word [dos_version], ax
  39. ; allocate max heap
  40. ; TODO: also support user specified heap size
  41. ; try to resize our main DOS memory block until the end of the data segment
  42. mov bx, word [dos_psp]
  43. mov es, bx
  44. sub bx, dgroup
  45. neg bx ; bx = (ds - psp) in paragraphs
  46. add bx, 1000h ; 64kb in paragraphs
  47. mov ah, 4Ah
  48. int 21h
  49. jc mem_realloc_err
  50. ; bx = the new size in paragraphs
  51. add bx, word [dos_psp]
  52. sub bx, dgroup
  53. mov cl, 4
  54. shl bx, cl
  55. sub bx, 2
  56. mov sp, bx
  57. add bx, 2
  58. sub bx, word [__stklen]
  59. and bl, 0FEh
  60. mov word [__stkbottom], bx
  61. cmp bx, _end wrt dgroup
  62. jb not_enough_mem
  63. ; heap is between [ds:_end wrt dgroup] and [ds:__stkbottom - 1]
  64. mov word [__nearheap_start], _end wrt dgroup
  65. mov bx, word [__stkbottom]
  66. dec bx
  67. mov word [__nearheap_end], bx
  68. jmp PASCALMAIN
  69. not_enough_mem:
  70. mov dx, not_enough_mem_msg
  71. jmp error_msg
  72. mem_realloc_err:
  73. mov dx, mem_realloc_err_msg
  74. error_msg:
  75. mov ah, 9
  76. int 21h
  77. mov ax, 4CFFh
  78. int 21h
  79. global FPC_MSDOS_CARRY
  80. FPC_MSDOS_CARRY:
  81. stc
  82. global FPC_MSDOS
  83. FPC_MSDOS:
  84. mov al, 21h ; not ax, because only the low byte is used
  85. pop dx
  86. pop cx
  87. push ax
  88. push cx
  89. push dx
  90. global FPC_INTR
  91. FPC_INTR:
  92. push bp
  93. mov bp, sp
  94. mov al, byte [ss:bp + 6]
  95. mov byte [cs:int_number], al
  96. mov si, [ss:bp + 4]
  97. push ds
  98. mov ax, word [si + 16]
  99. mov es, ax
  100. mov ax, word [si + 14] ; ds
  101. push ax
  102. mov ax, word [si]
  103. mov bx, word [si + 2]
  104. mov cx, word [si + 4]
  105. mov dx, word [si + 6]
  106. mov bp, word [si + 8]
  107. mov di, word [si + 12]
  108. mov si, word [si + 10]
  109. pop ds
  110. db 0CDh ; opcode of INT xx
  111. int_number:
  112. db 255
  113. pushf
  114. push ds
  115. push si
  116. push bp
  117. mov bp, sp
  118. mov si, word [ss:bp + 8]
  119. mov ds, si
  120. mov si, word [ss:bp + 14]
  121. mov word [si], ax
  122. mov word [si + 2], bx
  123. mov word [si + 4], cx
  124. mov word [si + 6], dx
  125. mov word [si + 12], di
  126. mov ax, es
  127. mov word [si + 16], ax
  128. pop ax
  129. mov word [si + 8], ax
  130. pop ax
  131. mov word [si + 10], ax
  132. pop ax
  133. mov word [si + 14], ax
  134. pop ax
  135. mov word [si + 18], ax
  136. pop ds
  137. pop bp
  138. ret 4
  139. segment data
  140. mem_realloc_err_msg:
  141. db 'Memory allocation error', 13, 10, '$'
  142. not_enough_mem_msg:
  143. db 'Not enough memory', 13, 10, '$'
  144. segment bss class=bss
  145. segment stack stack class=stack
  146. resb 256
  147. stacktop:
  148. group dgroup data bss stack