i86.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>
  2. * This file is part of the Linux-8086 C library and is distributed
  3. * under the GNU Library General Public License.
  4. */
  5. /*
  6. * These functions will not normally be useful for Linux-8086. But they
  7. * can be used and may be useful in the kernel.
  8. */
  9. #ifdef __AS386_16__
  10. #ifdef L___seg_regs
  11. unsigned int
  12. __get_cs()
  13. {
  14. #asm
  15. mov ax,cs
  16. #endasm
  17. }
  18. unsigned int
  19. __get_ds()
  20. {
  21. #asm
  22. mov ax,ds
  23. #endasm
  24. }
  25. unsigned int
  26. __get_es()
  27. {
  28. #asm
  29. mov ax,es
  30. #endasm
  31. }
  32. void
  33. __set_es(seg)
  34. {
  35. #asm
  36. #if __FIRST_ARG_IN_AX__
  37. mov es,ax
  38. #else
  39. mov bx,sp
  40. mov es,[bx+2]
  41. #endif
  42. #endasm
  43. }
  44. #endif
  45. #ifdef L___peek_es
  46. int
  47. __peek_es(off)
  48. unsigned int off;
  49. {
  50. #asm
  51. #if __FIRST_ARG_IN_AX__
  52. mov bx,ax
  53. #else
  54. mov bx,sp
  55. mov bx,[bx+2]
  56. #endif
  57. seg es
  58. mov al,[bx]
  59. xor ah,ah
  60. #endasm
  61. }
  62. #endif
  63. #ifdef L___poke_es
  64. int
  65. __poke_es(off, value)
  66. unsigned int off;
  67. int value;
  68. {
  69. #asm
  70. #if __FIRST_ARG_IN_AX__
  71. mov bx,sp
  72. mov bx,[bx+2]
  73. xchg ax,bx
  74. #else
  75. mov bx,sp
  76. mov ax,[bx+4]
  77. mov bx,[bx+2]
  78. #endif
  79. seg es
  80. mov [bx],al
  81. xor ah,ah
  82. #endasm
  83. }
  84. #endif
  85. #ifdef L___deek_es
  86. int
  87. __deek_es(off)
  88. unsigned int off;
  89. {
  90. #asm
  91. #if __FIRST_ARG_IN_AX__
  92. mov bx,ax
  93. #else
  94. mov bx,sp
  95. mov bx,[bx+2]
  96. #endif
  97. seg es
  98. mov ax,[bx]
  99. #endasm
  100. }
  101. #endif
  102. #ifdef L___doke_es
  103. int
  104. __doke_es(off, value)
  105. unsigned int off;
  106. int value;
  107. {
  108. #asm
  109. #if __FIRST_ARG_IN_AX__
  110. mov bx,sp
  111. mov bx,[bx+2]
  112. xchg ax,bx
  113. #else
  114. mov bx,sp
  115. mov ax,[bx+4]
  116. mov bx,[bx+2]
  117. #endif
  118. seg es
  119. mov [bx],ax
  120. #endasm
  121. }
  122. #endif
  123. #ifdef L___strchr_es
  124. char *
  125. __strchr_es(s, c)
  126. char * s;
  127. int c;
  128. {
  129. #asm
  130. mov bx,sp
  131. push si
  132. #if __FIRST_ARG_IN_AX__
  133. mov bx,[bx+2]
  134. mov si,ax
  135. #else
  136. mov si,[bx+2]
  137. mov bx,[bx+4]
  138. #endif
  139. xor ax,ax
  140. #ifdef PARANOID
  141. cld
  142. #endif
  143. push ds
  144. push es
  145. pop ds
  146. in_loop:
  147. lodsb
  148. cmp al,bl
  149. jz got_it
  150. or al,al
  151. jnz in_loop
  152. pop ds
  153. pop si
  154. ret
  155. got_it:
  156. lea ax,[si-1]
  157. pop ds
  158. pop si
  159. #endasm
  160. }
  161. #endif
  162. #ifdef L___strnget_es
  163. char *
  164. __strnget_es(d, s, c)
  165. char *d;
  166. char *s;
  167. register int c;
  168. {
  169. register int i = __strlen_es(s);
  170. if(i < c) c = i+1;
  171. /* else s[--c] = 0; ?? */
  172. /* else return -E2BIG; ?? */
  173. __movedata(__get_es(), s, __get_ds(), d, c);
  174. }
  175. #endif
  176. #ifdef L___strlen_es
  177. int __strlen_es(str)
  178. char * str;
  179. {
  180. #asm
  181. #if !__FIRST_ARG_IN_AX__
  182. mov bx,sp
  183. #endif
  184. push di
  185. cld
  186. #if __FIRST_ARG_IN_AX__
  187. mov di,ax
  188. #else
  189. mov di,[bx+2]
  190. #endif
  191. mov cx,#-1
  192. xor ax,ax
  193. repne
  194. scasb ! Scans [ES:DI]
  195. not cx
  196. dec cx
  197. mov ax,cx
  198. pop di
  199. #endasm
  200. }
  201. #endif
  202. #ifdef L_int86
  203. int
  204. int86(intr, in_regs, out_regs)
  205. int intr;
  206. union REGS* in_regs;
  207. union REGS* out_regs;
  208. {
  209. #asm
  210. push bp
  211. mov bp,sp
  212. push ds ! save ds
  213. ! es too ?
  214. push bp ! same for new bp
  215. pushf ! iret flags
  216. mov ax,[bp-6] ! flags for simulated int
  217. push cs ! iret address segment
  218. mov bx,#ret_addr ! iret address offset
  219. push bx
  220. and ah,#$0C ! simulate interrupt flags
  221. push ax ! flags are pushed first
  222. xor bx,bx
  223. mov es,bx ! interrupt vectors in seg 0
  224. mov bl,[bp+4]
  225. shl bx,#1
  226. shl bx,#1 ! intr*4 => interrupt vector address
  227. seg es
  228. push [bx+2] ! fetch interrupt segment
  229. seg es
  230. push [bx] ! fetch interrupt offset
  231. mov bx,[bp+6] ! input union REGS*
  232. mov ax,[bx]
  233. mov cx,[bx+4]
  234. mov dx,[bx+6]
  235. mov si,[bx+8]
  236. mov di,[bx+10]
  237. mov bx,[bx+2]
  238. ! Ignore cflag/flags ?
  239. iret ! simulate interrupt.
  240. ! But won't be nice for protected mode ...
  241. ret_addr:
  242. ! Int $25/6 would need resetting sp:ss too ... should I ?
  243. pop bp ! unzapped versions
  244. pop ds ! paranoia
  245. pushf ! save interrupt flags
  246. push bx ! save pointer register
  247. mov bx,[bp+8] ! output union REGS*
  248. mov [bx],ax
  249. pop [bx+2]
  250. mov [bx+4],cx
  251. mov [bx+6],dx
  252. mov [bx+8],si
  253. mov [bx+10],di
  254. mov word [bx+12],#0 ! cflag
  255. jnc no_carry
  256. mov byte [bx+12],#1
  257. no_carry:
  258. pop [bx+14] ! flags
  259. pop bp
  260. #endasm
  261. }
  262. #endif
  263. #ifdef L_int86x
  264. int
  265. int86x(intr, in_regs, out_regs, segr)
  266. int intr;
  267. union REGS* in_regs;
  268. union REGS* out_regs;
  269. struct SREGS * segr;
  270. {
  271. #asm
  272. push bp
  273. mov bp,sp
  274. push ds ! save ds
  275. ! es too ?
  276. push bp ! same for new bp
  277. pushf ! iret flags
  278. mov ax,[bp-6] ! flags for simulated int
  279. push cs ! iret address segment
  280. mov bx,#ret_addr ! iret address offset
  281. push bx
  282. and ah,#$0C ! simulate interrupt flags
  283. push ax ! flags are pushed first
  284. xor bx,bx
  285. mov es,bx ! interrupt vectors in seg 0
  286. mov bl,[bp+4]
  287. shl bx,1
  288. shl bx,1 ! intr*4 => interrupt vector address
  289. seg es
  290. push word [bx+2] ! fetch interrupt segment
  291. seg es
  292. push word [bx] ! fetch interrupt offset
  293. mov bx,[bp+10] ! struct SREGS*
  294. mov es,[bx]
  295. push [bx+6] ! ds
  296. mov bx,[bp+6] ! input union REGS*
  297. mov ax,[bx]
  298. mov cx,[bx+4]
  299. mov dx,[bx+6]
  300. mov si,[bx+8]
  301. mov di,[bx+10]
  302. mov bx,[bx+2]
  303. ! Ignore cflag/flags ?
  304. pop ds
  305. iret ! simulate interrupt
  306. ! But won't be nice for protected mode ...
  307. ret_addr:
  308. ! Int $25/6 would need resetting sp:ss too ... should I ?
  309. pop bp ! in case it was zapped
  310. pushf ! save interrupt flags
  311. push cx ! save work register
  312. mov cx,ds
  313. push bx ! save pointer register
  314. mov ds,word [bp-2] ! restore original ds
  315. mov bx,[bp+10] ! struct SREGS*
  316. mov [bx],es
  317. mov [bx+6],cx
  318. mov bx,[bp+8] ! output union REGS*
  319. mov [bx],ax
  320. pop [bx+2] ! bx
  321. pop [bx+4] ! cx
  322. mov [bx+6],dx
  323. mov [bx+8],si
  324. mov [bx+10],di
  325. mov word [bx+12],#0 ! cflag
  326. jnc no_carry
  327. mov byte [bx+12],#1
  328. no_carry:
  329. pop [bx+14] ! flags
  330. pop ds
  331. pop bp
  332. #endasm
  333. }
  334. #endif
  335. #ifdef L_segread
  336. segread(segp)
  337. struct SREGS * segp;
  338. {
  339. #asm
  340. #if __FIRST_ARG_IN_AX__
  341. mov bx,ax
  342. #else
  343. mov bx,sp
  344. mov bx,[bx+2]
  345. #endif
  346. mov [bx],es
  347. mov [bx+2],cs
  348. mov [bx+4],ss
  349. mov [bx+6],ds
  350. #endasm
  351. }
  352. #endif
  353. #endif /* __AS386_16__ */