接上篇文章《Turbo C使用的汇编函数 -- VGA 12H模式图形函数(一)》,继续粘贴代码文件:
include graph.inc
DATASEG
bufseg dw
0bufoff dw 0
linebytes dw 0
tmpbuf db 80 dup(0)
CODESEG
PUBLIC _ImageSize
PUBLIC _GetImage
PUBLIC _PutImage
;
;
;
PROC _ImageSize
ARG x1 : word, y1 : word, x2 : word, y2 : word
USES si, di
mov ax, [x1]
mov bx, [y1]
mov si, [x2]
mov di, [y2]
mov cx, MAXX
mov dx, MAXY
call TestPoint
jnc @@
xor ax, ax
xor dx, dx
jmp short @@2
@@1:
xchg ax, si
sub ax, si
sub di, bx
inc di
and ax, 0fff8h
shr ax, 1
add ax, 4
mul di
add ax, 4
adc dx, 0
@@2:
ret
ENDP
;
PROC PackLine
USES bx, ds, es
push es
pop ds
mov es, [bufseg]
mov di, [bufoff]
mov si, offset tmpbuf
@@
xor bh, bh
@@2:
or bl, bl
jz @@3
lodsb
dec bl
inc bh
cmp al, [si]
je @@2
@@3:
cmp bh, 1
ja @@4
test al, 80h
jz @@5
@@4:
xchg al, bh
or al, 80h
stosb
mov al, bh
@@5:
stosb
or bl, bl
jnz @@1
mov [bufoff], di
ret
ENDP
;
PROC UnPackLine
USES bx, cx, di, es
push ds
pop es
mov di, offset tmpbuf
push di
push ds
mov si, [bufoff]
mov bx, [linebytes]
mov ax, [bufseg]
mov ds, ax
@@
lodsb
mov cx, 1
test al, 80h
jz @@2
and ax, 7fh
mov cx, ax
lodsb
@@2:
sub bx, cx
@@3:
stosb
loop @@3
cmp bx, 0
jne @@1
pop ds
mov [bufoff], si
pop si
ret
ENDP
;
; unsigned GetImage( int x1, int y1, int x2, int y2, void far *buf )
;
PROC _GetImage
ARG x1 : word, y1 : word, x2 : word, y2 : word, buf : far ptr
USES si, di
mov ax, [x1]
mov bx, [y1]
mov si, [x2]
mov di, [y2]
mov cx, MAXX
mov dx, MAXY
call TestPoint
jnc @@
xor ax, ax
jmp @@4
@@5:
push ds
mov cx, ax
and cl, 7
mov dl, cl
mov cx, si
not cl
add cl, dl
and cl, 7
mov dh, 0ffh
shl dh, cl
mov [x1], dx
sub si, ax
sub di, bx
mov cx, di
les di, [dword buf]
mov [word es:di], si
mov [word es:di+2], cx
push di
add di, 4
mov [bufseg], es
mov [bufoff], di
shr si, 3
call GetDspMemOff
xchg si, bx
inc bx
inc cx
push ds
pop es
mov di, offset tmpbuf
push DSPMEMSEG
pop ds
mov dx, 3ceh
cld
@@1:
push cx
push si
mov cx, [x1]
mov ax, 304h
@@2:
out dx, ax
push ax
push si
push di
push bx
lodsb
@@3:
mov ah, al
lodsb
push ax
shl ax, cl
mov al, ah
stosb
pop ax
dec bx
jnz @@3
and [byte es:di-1], ch
pop bx
call PackLine
pop di
pop si
pop ax
dec ah
jns @@2
pop si
pop cx
add si, DMWIDTH
loop @@1
pop di
pop ds
mov ax, [bufoff]
sub ax, di
@@4:
ret
ENDP
;
; void PutImage(int x, int y, void far *buf, int mod)
;
PROC _PutImage
ARG x : word, y : word, buf : far ptr, mod : word
USES si, di
les bx, [dword buf]
mov di, [word es:bx
mov si, [word es:bx]
mov [bufseg], es
add bx, 4
mov [bufoff], bx
mov ax, si
shr ax, 3
inc ax
mov [linebytes], ax
mov ax, [x]
mov bx, [y]
add si, ax
add di, bx
mov cx, MAXX
mov dx, MAXY
call TestPoint
jc @@7
push [word DrMode]
mov cx, si
not cl
and cl, 7
mov ch, 0ffh
shl ch, cl
mov cl, al
and cl, 7
mov [x], cx
push ax
shr si, 3
shr ax, 3
sub si, ax
push si
push [word mod]
call _SetDrMode
xor ah, ah
call SetWrMode
pop ax
cld
mov cx, di
sub cx, bx
inc cx
pop di
pop ax
call GetDspMem
@@2:
push bx
push cx
mov ax, 802h
mov cx, [x]
@@3:
mov dx, 3c4h
out dx, ax
mov dx, 3cfh
push ax
push bx
push di
call UnPackLine
mov ax, 0ffh
shr al, cl
dec di
js @@6
out dx, al
lodsb
ror ax, cl
xchg al, [byte es:bx]
inc bx
mov al, 0ffh
out dx, al
@@4:
dec di
js @@5
mov ch, ah
lodsb
xor ah, ah
ror ax, cl
or al, ch
xchg al, [byte es:bx]
inc bx
jmp short @@4
@@5:
mov al, 0ffh
@@6:
and al, [byte x+1]
out dx, al
lodsb
shr al, cl
or al, ah
xchg al, [byte es:bx]
pop di
pop bx
pop ax
shr ah, 1
jnz @@3
pop cx
pop bx
add bx, DMWIDTH
loop @@2
pop [word DrMode]
call RestReg
mov dx, 3c4h
mov ax, 0f02h
out dx, ax
@@7:
ret
ENDP
END
include graph.inc
BLOCKED equ
1UNBLOCKED equ 2
DATASEG
BorderValue dw
0FillColor dw 0
FillBufPtr dw 0
BufStartOfs dw 0
BufEndOfs dw 0
D dw 0
PrevD dw 0
PrevV dw 0
TmpX dw 0
TmpY dw 0
TmpFlag db 0
CODESEG
PUBLIC _Fill
;
; ax
;
PROC ScanRight
USES es, di, si, bx
mov si, ax
call GetDspMem
mov di, bx
mov cx, si
shr si,
and cl, 7
mov ch, 0ffh
shr ch, cl
mov dx, 3ceh
mov ah, [byte BorderValue]
mov al, 2
out dx, ax
mov ax, 805h
out dx, ax
mov ax, 0f07h
out dx, ax
mov al, [es:di]
inc di
not al
and al, ch
jnz @@1
mov cx, [MaxX]
shr cx, 3
sub cx, si
mov al, 0ffh
cld
repe scasb
mov al, [es:di-1]
not al
cmp cx, 0
jne @@1
mov cx, [MaxX]
and cl, 7
mov ch, 40h
shr ch, cl
or al, ch
@@1:
sub di, bx
add di, si
shl di, 3
mov cx, 8
@@2:
shl al, 1
jc @@3
loop @@2
@@3:
sub di, cx
mov ax, 2
out dx, ax
mov ax, 5
out dx, ax
mov ax, di
ret
ENDP
;
; return ax = x, bx = y
;
PROC ScanRegion
USES di
mov di, [BufStartOfs] ;i
@@1:
cmp di, si ;while(i < bufend)
jae @@5 ;{
cmp [byte es:di+4], BLOCKED ; if(buf[i].flag == BLOCKED
je @@2
mov bx, [es:di+2] ; || buf[i].y != buf[i+1].y)
cmp bx, [es:di+7]
je @@3
@@2:
add di, 5 ; i ++
jmp short @@1 ; }
@@3: ; else{
mov ax, [es:di] ; if(buf[i].x+1 < buf[i+1].x)
inc ax ; {
cmp ax, [es:di+5]
jge @@4
call ScanRight ; xr = ScanRight(buf[i].x+1,buf[i].y)
cmp ax, [es:di+5] ; if(xr < buf[i+1].x){
jl @@5 ; ax=xr; bx=buf[i].y
@@4: ; }
add di, 10 ; i += 2
jmp short @@1 ; }
@@5: ;}
mov [BufStartOfs], di ;bufstart = i
ret
ENDP
;
;
PROC FillRegion
USES di
push [word Color]
mov ax, [FillColor]
mov [Color], ax
mov di, [FillBufPtr]
call SetWrMode0
@@
cmp di, [BufEndOfs]
jae @@5
cmp [byte es:di+4], BLOCKED
je @@2
mov bx, [es:di+2]
cmp bx, [es:di+7]
je @@3
@@2:
add di, 5
jmp short @@1
@@3:
mov si, [es:di+5]
dec si
cmp si, [es:di]
jle @@4
push es
push di
mov ax, [es:di]
inc ax
mov di, bx
call Bar
pop di
pop es
@@4:
add di, 10
jmp short @@1
@@5:
call RestReg0
pop [word Color]
ret
ENDP
;
;
PROC AppendBPList
mov [
byte es:si+4], blmov ebx, [dword TmpX]
mov [dword es:si], ebx
add si, 5
ret
ENDP
;
;
PROC FindBP
USES bx
mov cx, [TmpX]
mov dx, [TmpY]
cmp ax,
jl @@5
cmp ax, 3
jg @@1
dec dx
jmp short @@2
@@1:
cmp ax, 5 ; 5, 6, 7, y ++
jl @@3
inc dx
@@2:
cmp ax, 3 ; 3, 4, 5, x --
jl @@4
cmp ax, 5
jg @@4
@@3:
dec cx
jmp short @@6
@@4:
cmp ax, 7 ; 0, 1, 7, x ++
je @@5
cmp ax, 1
jg @@6
@@5:
inc cx
@@6:
cmp cx, [MinX]
jl @@18
cmp cx, [MaxX]
jg @@18
cmp dx, [MinY]
jl @@18
cmp dx, [MaxY]
jg @@18
push es
push ax
push cx
push dx
mov ax, cx
mov bx, dx
call GetPixel
mov bx, ax
pop dx
pop cx
pop ax
pop es
cmp bx, [BorderValue]
je @@16
@@18:
cmp ax, [PrevD]
jne @@8
cmp [word PrevD], 4
je @@15
cmp [word PrevD], 0
jne @@7
mov [byte es:si-1], BLOCKED
jmp @@15
@@7:
mov bl, UNBLOCKED
call AppendBPList
jmp @@15
@@8:
mov bl, BLOCKED
cmp [word PrevD], 4
jne @@10
cmp [word PrevV], 5
jne @@13
mov [byte es:si-1], BLOCKED
jmp short @@13
@@10:
cmp [word PrevD], 0
jne @@11
mov [byte es:si-1], BLOCKED
cmp ax, 7
je @@13
mov bl, UNBLOCKED
jmp short @@13
@@11:
mov bl, UNBLOCKED
call AppendBPList
cmp ax, 1
jl @@14
mov bl, UNBLOCKED
cmp ax, 3
jg @@12
cmp [word PrevD], 5
jl @@14
cmp [word PrevD], 7
jg @@14
jmp short @@13
@@12:
cmp ax, 5
jl @@14
cmp [word PrevD], 1
jl @@14
cmp [word PrevD], 3
jg @@14
@@13:
call AppendBPList
@@14:
push [word PrevD]
pop [word PrevV]
@@15:
mov [PrevD], ax
mov [TmpX], cx
mov [TmpY], dx
mov ax, 1
jmp short @@17
@@16:
xor ax, ax
@@17:
ret
ENDP
;
;
PROC FindNextPixel
mov bx,
-2@@1:
inc bx
cmp bx, 5
jg @@2
mov ax, bx
add ax, [D]
and ax, 7
call FindBP
cmp ax, 0
je @@1
add [word D], bx
and [word D], 6
@@2:
ret
ENDP
;
; TraceBorder(int startx, int starty)
;
PROC TraceBorder
ARG startx : word, starty : word
mov [word D],
6mov [word PrevD], 8
mov [word PrevV], 2
push [word startx]
pop [word TmpX]
push [word starty]
pop [word TmpY]
@@1:
call FindNextPixel
cmp ax, 0
je @@2
mov bx, [startx]
cmp bx, [TmpX]
jne @@1
mov bx, [starty]
cmp bx, [TmpY]
jne @@1
@@2:
mov cx, [startx]
mov dx, [starty]
cmp ax, 0
jne @@3
mov [es:si], cx
mov [es:si+2], dx
mov [byte es:si+4], UNBLOCKED
add si, 5
jmp short @@4
@@3:
cmp [word PrevD], 3
jg @@5
cmp [word PrevD], 1
jl @@5
@@4:
mov [es:si], cx
mov [es:si+2], dx
mov [byte es:si+4], UNBLOCKED
add si, 5
@@5:
ret
ENDP
;
PROC _CompBP
mov ax, [es:di
+2]sub ax, [TmpY]
jne @@1
mov ax, [es:di]
sub ax, [TmpX]
jne @@1
mov al, [es:di+4]
sub al, [TmpFlag]
cbw
@@1:
ret
ENDP
;
PROC BorSort
mov ax, cx
sub ax, bx
cmp ax,
jl @@8
xor dx, dx
mov di, 5
div di
shr ax, 1
mul di
add ax, bx
mov di, ax
mov eax, [dword es:di]
mov [dword TmpX], eax
mov al, [es:di+4]
mov [TmpFlag], al
mov si, bx
mov dx, cx
sub si, 5
push di
@@0:
mov di, si
@@1:
add di, 5
call _CompBP
jl @@1
@@2:
mov si, di
mov di, dx
@@3:
sub di, 5
call _CompBP
jg @@3
@@4:
mov dx, di
cmp si, di
jae @@7
@@6:
mov eax, [dword es:si]
xchg eax, [dword es:di]
mov [dword es:si], eax
mov al, [es:si+4]
xchg al, [es:di+4]
mov [es:si+4], al
jmp short @@0
@@7:
pop di
cmp si, di
jae @@5
push cx
push si
mov cx, si
call BorSort
pop bx
pop cx
call BorSort
jmp short @@8
@@5:
push bx
push si
mov bx, si
call BorSort
pop cx
pop bx
call BorSort
@@8:
ret
ENDP
;
; void Fill(int x, int y, int fillcolor)
;
PROC _Fill
ARG x : word, y : word, fillcolor : word
USES si
mov ax, [x]
mov bx, [y]
cmp ax, [WidthX]
ja @@
cmp bx, [WidthY]
ja @@2
add ax, [MinX]
add bx, [MinY]
mov [x], ax
mov [y], bx
call GetPixel
mov [BorderValue], ax
mov ax, [fillcolor]
mov [FillColor], ax
push 0
push 15000
call _farmalloc
add sp, 4
cmp dx, 0
je @@2
mov es, dx
mov [FillBufPtr], ax
mov [BufStartOfs], ax
mov si, ax
mov ax, [x]
mov bx, [y]
call ScanRight
@@1:
push bx
push ax
call TraceBorder
add sp, 4
push si
mov cx, si
mov bx, [BufStartOfs]
call BorSort
pop si
call ScanRegion
cmp si, [BufStartOfs]
ja @@1
mov [BufEndOfs], si
call FillRegion
push es
push [word FillBufPtr]
call _farfree
add sp, 4
@@2:
ret
ENDP
;
END
include graph.inc
CODESEG
PUBLIC _Roll
;
;
;
PROC _Roll
ARG n : word, x1 : word, y1 : word, x2 : word, y2 : word
USES si, di, ds
cmp [word n],
0je @@4
mov ax, [x1]
mov bx, [y1]
mov si, [x2]
mov di, [y2]
mov cx, MAXX
mov dx, MAXY
call TestPoint
jc @@4
mov dx, 0ffffh
mov cx, ax
and cl, 7
shr dh, cl
mov cx, si
and cl, 7
not cl
shl dl, cl
push dx
push ax
shr ax, 3
shr si, 3
sub si, ax
mov [x2], si
mov cx, [n]
mov ax, DMWIDTH
mov [y2], ax
imul cx
mov si, ax
mov cx, di
sub cx, bx
inc cx
cmp si, 0
jg @@1
xchg bx, di
neg [word n]
neg [word y2]
@@1:
pop ax
call GetDspMem
mov di, bx
add si, di
pop bx
sub cx, [n]
jle @@4
mov ah, 1
call SetWrMode
push es
pop ds
cld
@@2:
push cx
push si
push di
mov cx, [x2]
mov al, bh
jcxz @@3
out dx, al
movsb
mov al, 0ffh
dec cx
jz @@3
out dx, ax
repz movsb
mov al, 0ffh
@@3:
and al, bl
out dx, ax
movsb
pop di
pop si
pop cx
add di, [y2]
add si, [y2]
loop @@2
call RestReg
@@4:
ret
ENDP
end
P386N
include graph.inc
DATASEG
Sin dw 0000h, 00afh, 015dh, 020bh, 02bah, 0368h, 0415h, 04c3h
dw 0570h, 061ch, 06c8h, 0774h, 081fh, 08cah, 0973h, 0a1ch
dw 0ac4h, 0b6ch, 0c12h, 0cb8h, 0d5ch, 0e00h, 0ea2h, 0f43h
dw 0fe3h, 1082h, 1120h, 11bch, 1257h, 12f0h, 1388h, 141eh
dw 14b3h, 1546h, 15d8h, 1668h, 16f6h, 1782h, 180dh, 1895h
dw 191ch, 19a1h, 1a23h, 1aa4h, 1b23h, 1b9fh, 1c19h, 1c92h
dw 1d07h, 1d7bh, 1dech, 1e5bh, 1ec8h, 1f32h, 1f9ah, 2000h
dw 2062h, 20c3h, 2120h, 217ch, 21d4h, 222ah, 227dh, 22ceh
dw 231ch, 2367h, 23afh, 23f5h, 2438h, 2478h, 24b5h, 24efh
dw 2527h, 255bh, 258dh, 25bbh, 25e7h, 2610h, 2635h, 2658h
dw 2678h, 2695h, 26afh, 26c5h, 26d9h, 26eah, 26f8h, 2702h
dw 270ah, 270eh, 2710h
StartQ dw
EndQ dw 0
StartX dw 0
StartY dw 0
EndX dw 0
EndY dw 0
;
EXTRN CirX0:word
EXTRN CirY0:word
;
CODESEG
PUBLIC _Arc
PUBLIC _Slice
PUBLIC _Pie
EXTRN Ellipse:proc
EXTRN PlotCirPix:proc
;
;
PROC SumArcXY
ARG x : word, y : word
mov ax, [CirX0]
mov bx, [CirY0]
and cx,
jnz @@1
add ax, [x]
sub bx, [y]
jmp short @@4
@@1:
cmp cx, 1
jne @@2
sub ax, [x]
sub bx, [y]
jmp short @@4
@@2:
add bx, [y]
cmp cx, 2
jne @@3
sub ax, [x]
jmp short @@4
@@3:
add ax, [x]
@@4:
ret
ENDP
;
;
PROC Arcjs
USES di
@@
1:cmp di, bp
je @@5
cmp ax, [es:di]
jne @@2
cmp bx, [es:di+2]
je @@5
cmp bx, [es:di+6]
je @@5
@@2:
cmp bx, [es:di+2]
jne @@3
cmp ax, [es:di+4]
je @@5
@@3:
add di, cx
jmp short @@1
@@5:
mov ax, di
ret
ENDP
;
; ax = jd, dx = r, return ax = sq, di =