;	Static Name Aliases
;based on fractint and vgadoc. yet to be finished
;
;extern void _fastcall _near callafi(int, char _far *);
;extern struct ai_funcs _far * _near getafi(void);
;extern void _near myputs(char _far *);
;

	TITLE   util
	.286p
_DATA	SEGMENT  WORD PUBLIC 'DATA'
_DATA	ENDS
CONST	SEGMENT  WORD PUBLIC 'CONST'
CONST	ENDS
_BSS	SEGMENT  WORD PUBLIC 'BSS'
_BSS	ENDS
DGROUP	GROUP	CONST, _BSS, _DATA
	ASSUME DS: DGROUP, SS: DGROUP

_BSS	SEGMENT
afi_off	dw	?
afi_seg	dw	?
_BSS	ENDS

X	EQU	4		; defined for near procedures
;X	EQU	6		; defined for far procedures

IBM_TEXT	SEGMENT  WORD PUBLIC 'CODE'
	ASSUME	CS: IBM_TEXT
; get the jump table for the AI
	PUBLIC	_getafi
_getafi	PROC NEAR
	push	bp
	mov	bp, sp
	push	ax
	push	bx
	push	cx
	push	dx
	push	ds
	push	es
	assume	ds: _BSS
	mov	ax, SEG _BSS
	mov	ds, ax
	mov	afi_off, 0	; initialize afi pointer offset 
	mov	afi_seg, 0   	; initialize afi pointer segment
        mov     ax,357fh        ; read interrupt vector 7f
        int     21h
        mov     ax,es
        or      ax,bx           ; is 7f vector null
        stc
        jz      getafiret
	xor	cx, cx
	xor	dx, dx
        mov     ax,0105h        ; get Interface address
        int     7fh             ; by software interrupt 7f
        jc      getafiret       ; Interface not OK if carry set
	mov	afi_off, dx	; save afi pointer offset 
	mov	afi_seg, cx	; save afi pointer segment
        clc                     ; clear carry flag
getafiret:
	pop	es
	pop	ds
	pop	dx
	pop	cx
	pop	bx
	pop	ax
	pop	bp
        ret                     ; return to caller
_getafi ENDP

;void _fastcall _near callafi(int, char _far *);
@callafi PROC NEAR
	PUBLIC	@callafi
	push	bp
	mov	bp,sp
	push	si
	push	di
	les	bx, dword ptr afi_off
	shl	ax, 1
	shl	ax ,1
	add	bx, ax		;int * 4
	mov	ax,[bp+X]       ;char far *
	push	ax		;pass the parameter pointer
	call	DWORD PTR es:[bx]
	pop	di
	pop	si
	pop	bp
	ret	4
@callafi	ENDP

	public	_myputs
_myputs	PROC NEAR
	push	bp
	mov	bp,sp
	push	ds
	push	si

	lds	si,[bp+X]
	ASSUME	ds: nothing
	cld
$T0:
	lodsb
	or	al,al
	jz	$T1
	sub	bx,bx
	mov	ah,0eh
	int	10h
	jmp short $T0
$T1:
	pop	si
	pop	ds
	pop	bp
	ret
_myputs	ENDP

IBM_TEXT	ENDS
	END
