assembly x86(nasm)画三角形等图形的实现(升级版)
https://www.cnblogs.com/lanclot-/p/10962702.html接上一篇
本来就有放弃的想法,可是有不愿退而求次,
然后大神室友写了一个集海伦公式计算三角形面积,
三点坐标计算的程序,
data segment
message db 'Please input 3 numbers',0dh,0ah,'$'
message1 db 'These numbers can not make up a triangle','$'
num db ?,?,?
zuobiao dw ?,?,?,?,?,?
n db ?,?,?,?
m db 'area=',?,?,?,?,?
crlf db 0dh,0ah,'$'
d dw ?,?,?,?
a db ?,?,?
data ends
stack segment para stack'stack'
db 50 dup(?)
code segment
assume ds:data,ss:stack,cs:code
start:mov ax,data
mov ds,ax
mov dx,offset message
mov ah,9
int 21h
mov di,0
again:mov ah,1 ;输入3个数(ps:为了设计的方便,只能输入3个10以内的整数)
int 21h
and al,00001111b
mov num[di],al
mov dx,offset crlf
mov ah,9
int 21h
inc di
cmp di,3
jb again
mov al,num[1] ;将输入的数按从大到小排序
cmp al,num[2]
jb continue
mov bl,num[2]
mov num[1],bl
mov num[2],al
mov al,bl
continue:cmp al,num[0]
jnb inspect
mov bl,num[0]
mov num[0],al
mov num[1],bl
cmp bl,num[2]
jb inspect
mov al,num[2]
mov num[1],al
mov num[2],bl
inspect:mov al,num[0] ;检查是否满足形成三角形的条件
add al,num[1]
cmp al,num[2]
jna tip
mov al,num[2]
sub al,num[1]
cmp al,num[0]
jnb tip
jmp area
tip:mov dx,offset message1
mov ah,9
int 21h
jmp exit
;利用海伦公式求三角形面积
area:mov al,num[0] ;a+b+c=?
add al,num[1]
add al,num[2]
mov n[0],al
mov al,num[0] ;a+b-c=?
add al,num[1]
sub al,num[2]
mov n[1],al
mov al,num[0] ;a+c-b=?
add al,num[2]
sub al,num[1]
mov n[2],al
mov al,num[1] ;b+c-a=?
add al,num[2]
sub al,num[0]
mov n[3],al
mov al,n[0] ;(a+b+c)*(a+b-c)*(a+c-b)*(b+c-a)=?
mov bl,n[1]
mul bl
mov cx,ax
mov al,n[2]
mov bl,n[3]
mul bl
mul cx
output:mov bx,16 ;上述值除以16
div bx
mov bx,ax
mov ax,dx
mov cx,625
mul cx
mov d[0],ax
mov d[2],dx
mov ax,bx
mov bx,10000
mul bx
add d[0],ax
adc d[2],dx
mov bx,0
sqrt1: inc bx ;开根号
mov ax,bx
mul bx
cmp dx,d[2]
jna sqrt1
sqrt2:dec bx
mov ax,bx
mul bx
cmp ax,d[0]
ja sqrt2
mov ax,bx
mov d[0],bx
mov bl,100
div bl
mov cl,ah
cbw
mov bl,10
div bl
or al,00110000b
mov m[6],al
or ah,00110000b
mov m[7],ah
mov m[8],'.'
mov al,cl
cbw
mov bl,10
div bl
or al,00110000b
mov m[9],al
or ah,00110000b
mov m[10],ah
mov m[11],'$'
mov dx,offset m
mov ah,9
int 21h
graph:mov ax,d[0]
mov bx,2
mul bx
mov d[0],ax
mov d[2],dx
mov al,num[2]
cbw
mov n[0],al
mov n[1],ah
mov ax,d[0]
mov dx,d[2]
mov bl,n[0]
mov bh,n[1]
div bx
mov zuobiao[10],ax
mul ax
mov d[0],ax
mov d[2],dx
mov al,num[1]
mul al
mov bx,10000
mul bx
mov d[4],ax
mov d[6],dx
mov bx,0
heng1:inc bx
mov ax,bx
mul bx
add ax,d[0]
adc dx,d[2]
cmp dx,d[6]
jna heng1
heng2:dec bx
mov ax,bx
mul bx
add ax,d[0]
adc dx,d[2]
cmp ax,d[4]
ja heng2
mov zuobiao[8],bx ;bx的值为横坐标
mov zuobiao[0],0
mov zuobiao[2],0
mov al,num[2]
mov bl,100
mul bl
mov zuobiao[4],ax
mov zuobiao[6],0
exit:mov ah,4ch
int 21h
code ends
end start
于是,
把他封装成我的子程序fdw,
debug了许久
就成了!?!
先看结果:
完整版:
data segment message db 'Please input 3 numbers(0~9)',0dh,0ah,'$' message1 db 'These numbers can not make up a triangle','$' message2 db 0dh,0ah,'Please press any key to display graphics',0dh,0ah,'$' a1 dw 3 dup(?) b1 dw 3 dup(?) num db ?,?,? n db ?,?,?,? m db 'area=',?,?,?,?,?,?,? d dw ?,?,?,? crlf db 0dh,0ah,'$' buf1 db 5 db ? db 5 dup('$') x1 dw ? y1 dw ? x2 dw ? y2 dw ? s2 dw ? s1 dw ? p dw ? xd dw ? yd dw ? data ends code segment assume cs:code,ds:data ;------------------------------------------------------------------------------------------------------------------------------------------ main proc start: mov ax,data mov ds,ax call fdw mov al,12h ;320*200 256色的图形模式: mov ah,0 ;是用来设定显示模式的服务程序 int 10h mov ax,a1[0] mov bx,b1[0] mov x1,ax mov y1,bx mov ax,a1[2] mov bx,b1[2] mov x2,ax mov y2,bx call row mov ax,a1[0] mov bx,b1[0] mov x1,ax mov y1,bx mov ax,a1[4] mov bx,b1[4] mov x2,ax mov y2,bx call chose mov ax,a1[2] mov bx,b1[2] mov x1,ax mov y1,bx mov ax,a1[4] mov bx,b1[4] mov x2,ax mov y2,bx call chose xor si,si mov ah,4ch int 21h main endp ;------------------------------------------------------------------------------------------------------------------------------------------ ;------------------------------------------------------------------------------------------------------------------------------------------ fdw proc mov dx,offset message mov ah,9 int 21h mov di,0 again:mov ah,1 ;输入3个数(ps:为了设计的方便,只能输入3个10以内的整数) int 21h and al,00001111b mov num[di],al mov dx,offset crlf mov ah,9 int 21h inc di cmp di,3 jb again mov al,num[1] ;将输入的数按从大到小排序 cmp al,num[2] jb continue mov bl,num[2] mov num[1],bl mov num[2],al mov al,bl continue:cmp al,num[0] jnb inspect mov bl,num[0] mov num[0],al mov num[1],bl cmp bl,num[2] jb inspect mov al,num[2] mov num[1],al mov num[2],bl inspect:mov al,num[0] ;检查是否满足形成三角形的条件 add al,num[1] cmp al,num[2] jna tip mov al,num[2] sub al,num[1] cmp al,num[0] jnb tip jmp area tip:mov dx,offset message1 mov ah,9 int 21h jmp exitf ;利用海伦公式求三角形面积 area:mov al,num[0] ;a+b+c=? add al,num[1] add al,num[2] mov n[0],al mov al,num[0] ;a+b-c=? add al,num[1] sub al,num[2] mov n[1],al mov al,num[0] ;a+c-b=? add al,num[2] sub al,num[1] mov n[2],al mov al,num[1] ;b+c-a=? add al,num[2] sub al,num[0] mov n[3],al mov al,n[0] ;(a+b+c)*(a+b-c)*(a+c-b)*(b+c-a)=? mov bl,n[1] mul bl mov cx,ax mov al,n[2] mov bl,n[3] mul bl mul cx output:mov bx,16 ;上述值除以16 div bx mov bx,ax mov ax,dx mov cx,625 mul cx mov d[0],ax mov d[2],dx mov ax,bx mov bx,10000 mul bx add d[0],ax adc d[2],dx mov bx,0 sqrt1: inc bx ;开根号 mov ax,bx mul bx cmp dx,d[2] jna sqrt1 sqrt2:dec bx mov ax,bx mul bx cmp ax,d[0] ja sqrt2 mov ax,bx mov d[0],bx mov bl,100 div bl mov cl,ah cbw mov bl,10 div bl or al,00110000b mov m[6],al or ah,00110000b mov m[7],ah mov m[8],'.' mov al,cl cbw mov bl,10 div bl or al,00110000b mov m[9],al or ah,00110000b mov m[10],ah mov m[11],'$' mov dx,offset m mov ah,9 int 21h graph:mov ax,d[0] mov bx,2 mul bx mov d[0],ax mov d[2],dx mov al,num[2] cbw mov n[0],al mov n[1],ah mov ax,d[0] mov dx,d[2] mov bl,n[0] mov bh,n[1] div bx mov b1[4],ax mul ax mov d[0],ax mov d[2],dx mov al,num[1] mul al mov bx,10000 mul bx mov d[4],ax mov d[6],dx mov bx,0 heng1:inc bx mov ax,bx mul bx add ax,d[0] adc dx,d[2] cmp dx,d[6] jna heng1 heng2:dec bx mov ax,bx mul bx add ax,d[0] adc dx,d[2] cmp ax,d[4] ja heng2 mov a1[4],bx ;bx的值为横坐标 mov a1[0],0 mov b1[0],0 mov al,num[2] mov bl,100 mul bl mov a1[2],ax mov b1[2],0 mov ax,a1[0] mov cl,2 shr ax,cl add ax,20 mov a1[0],ax mov ax,b1[0] mov cl,2 shr ax,cl add ax,80 mov b1[0],ax mov ax,a1[2] mov cl,2 shr ax,cl add ax,20 mov a1[2],ax mov ax,b1[2] mov cl,2 shr ax,cl add ax,80 mov b1[2],ax mov ax,b1[4] mov cl,2 shr ax,cl add ax,80 mov b1[4],ax mov ax,a1[4] mov cl,2 shr ax,cl add ax,20 mov a1[4],ax lea dx,message2 mov ah,9 int 21h mov ah,1 int 21h xor al,al ret exitf: mov ah,4ch int 21h fdw endp ;------------------------------------------------------------------------------------------------------------------------------------------ ;------------------------------------------------------------------------------------------------------------------------------------------ row proc mov cx,20 mov bx,x2 mov dx,80 int 10h pheng: mov al,1100b ;淡红色 mov ah,0ch ;写入点像 inc cx ; cmp cx,bx int 10h jne pheng ret row endp ;------------------------------------------------------------------------------------------------------------------------------------------ line proc mov dx,y1 mov bx,y2 cmp dx,bx jb nextl xchg dx,bx nextl: mov cx,x1 int 10h pshu: mov al,1100b ;淡红色 mov ah,0ch ;写入点像 inc dx ; cmp dx,bx int 10h jne pshu ret line endp ;------------------------------------------------------------------------------------------------------------------------------------------ paint proc call compare1 mov ax,x2 mov bx,x1 cmp ax,bx jae dpos1 sub bx,ax mov s1,-1 mov xd,bx jmp d1 dpos1: sub ax,bx mov s1,1 mov xd,ax d1: mov ax,y2 mov bx,y1 cmp ax,bx jae dpos2 ;y2>=y1 sub bx,ax ;y2<y1 mov s2,-1 mov yd,bx jmp d2 dpos2: sub ax,bx mov s2,1 mov yd,ax d2: add ax,ax mov bx,xd sub ax,bx mov p,ax ;2dy-dx mov cx,x1 mov dx,y1 pxie: mov al,1100b ;淡红色 mov ah,0ch ;写入点像 int 10h mov ax,p mov bx,0 cmp ax,bx jge ppos ;p>=0 jl pneg ;p<0 ppos: mov ax,xd mov bx,yd cmp ax,bx ja ddpos1 ;xd>yd jbe ddneg1 ;xd<=yd ddpos1: ;0<k<1 或者 -1<k<0 同时 p>=0 mov ax,x1 ;x=x+1 mov bx,s1 add ax,s1 mov x1,ax mov ax,s2 ;y=y+1 mov bx,y1 add bx,ax mov y1,bx mov ax,p ;Pn+1=Pn+2(dy-dx) mov bx,xd mov cx,yd add bx,bx add cx,cx sub cx,bx add ax,cx mov p,ax jmp plot ddneg1: ;k>1 或者 k<-1 同时 p>=0 mov ax,y1 ;y=y+1 inc ax mov y1,ax mov ax,s1 ;x=x+1 或者 x=x-1 mov bx,x1 add bx,ax mov x1,bx mov ax,p ;Pn+1=Pn+2(dx-dy) mov bx,xd mov cx,yd add bx,bx add cx,cx sub bx,cx add ax,bx mov p,ax jmp plot pxie1: jmp pxie pneg: mov ax,xd mov bx,yd cmp ax,bx ja ddpos2 ;xd>yd jbe ddneg2 ;xd<=yd ddpos2: ;0<k<1 或者 -1<k<0 同时 p<0 mov ax,x1 ;x=x+1 mov bx,s1 add ax,s1 mov x1,ax mov ax,p ;Pn+1=Pn+2dy mov bx,yd add bx,bx add ax,bx mov p,ax jmp plot ddneg2: ;k>1 或者 k<-1 同时 p<0 mov ax,y1 ;y=y+1 inc ax mov y1,ax mov ax,p ;Pn+1=Pn+2dx mov bx,xd add bx,bx add ax,bx mov p,ax jmp plot plot: mov cx,x1 ;X坐标 mov dx,y1 ;Y坐标 cmp cx,x2 ;是否绘图完毕 jne pxie1 ret paint endp ;------------------------------------------------------------------------------------------------------------------------------------------ ;------------------------------------------------------------------------------------------------------------------------------------------ compare1 proc mov ax,y1 mov bx,y2 cmp ax,bx jna unch mov y1,bx mov y2,ax mov ax,x1 mov bx,x2 mov x1,bx mov x2,ax unch: ret compare1 endp ;------------------------------------------------------------------------------------------------------------------------------------------ moving proc mov ax,a1[si] mov bx,b1[si] mov x1,ax mov y1,bx mov ax,a1[si] mov bx,b1[si] mov x1,ax mov y1,bx add si,2 ret moving endp ;------------------------------------------------------------------------------------------------------------------------------------------ chose proc mov ax,x1 cmp ax,x2 je line1 mov ax,y1 cmp ax,y2 je row1 call paint jmp exitc row1: call row jmp exitc line1: call line jmp exitc exitc: ret chose endp ;------------------------------------------------------------------------------------------------------------------------------------------ code ends end start ;1.p=1/2(a+b+c) s=gp(p-a)(p-b)(p-c) ;2.S=1/2[(x1y2-x2y1)+(x2y3-x3y2)+(x3y1-x1y3)]
简直了