assembly x86(nasm)画三角形等图形的实现

参考了一位大佬的博客

https://blog.csdn.net/qq_40298054/article/details/84496944传送门

https://blog.csdn.net/qq_40818798/article/details/83758281

事情是这样的,原本愉快的一天因为一道题目而被毁掉

原题:

assembly x86(nasm)画三角形等图形的实现

就很头疼(于是改成了输入三个坐标)

assembly x86(nasm)画三角形等图形的实现

 

看一下成果:

assembly x86(nasm)画三角形等图形的实现

 

 

代码如下:

data    segment
message        db    'This is a sample program of keyboard and display'
            db    0dh,0ah,'Please strike the key!',0dh,0ah,'$'
buf1        db  5
            db  ?
            db  5 dup('$') 
x1            dw    ?
y1            dw    ?
x2            dw    ?
y2            dw    ?
a1          dw  3 dup(?)
b1          dw  3 dup(?)
s2            dw    ?
s1            dw    ?
p            dw    ?
xd            dw    ?
yd            dw    ?
area        dw    ?
data    ends
code    segment
assume    cs:code,ds:data
;------------------------------------------------------------------------------------------------------------------------------------------
main proc
start:        
        mov    ax,data
        mov    ds,ax
        mov    dx,offset message            ;dxhang,cxshu
        mov    ah,9                    
        int    21h
        mov al,12h                      ;320*200 256色的图形模式:
           mov ah,0                        ;是用来设定显示模式的服务程序
           int 10h
        xor di,di
        mov cx,3        
loop0:    push cx
        call cin
        mov a1[di],ax
        call cin
        mov b1[di],ax
        add di,2
        pop cx
        loop loop0
        call area1
        xor di,di    
        xor si,si
        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
        add si,2
        call chose
        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
        add si,2
        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
        add si,2
        call chose
        xor si,si
;       mov x1,100                    ;(10,100)
;        mov y1,250
;       mov x2,10                    ;(100,250)
;        mov y2,100
        mov    ah,4ch                        
        int    21h
main endp
;------------------------------------------------------------------------------------------------------------------------------------------
row proc
        mov cx,x1
           mov bx,x2
        cmp cx,bx
        jb nextr
        xchg cx,bx
nextr:    mov dx,y1
;          mov cx,10           ;x坐标
;       mov bx,100          ;终止x坐标
;       mov dx,250          ;y坐标
           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
;       mov cx,10           ;x坐标
;        mov bx,250          ;终止y坐标
;        mov dx,100          ;y坐标
        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
;------------------------------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------------------------------
;compare0 proc            ;trangle
;    xor si,si
;    mov ax,a1[]
;    mov bx,a2
;    cmp ax,bx
;    je  test1
;    jmp test2
;test1:
;    mov ax,a1
;    mov bx,a3
;    cmp ax,bx
;    je  Utrangle
;test2:
;    mov ax,b1
;    mov bx,b2
;    cmp ax,bx
;    je  text3
;    jmp exit
;test3:
;    mov ax,b1
;    mov bx,b3
;    cmp ax,bx
;    je  text3
;    jmp exit
;Utrangle:
;    mov ah,4ch
;    int 21
;exit:
;    ret
;compare0 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            
;------------------------------------------------------------------------------------------------------------------------------------------
cin proc
        xor cx,cx
        xor si,si
again:  mov dx,offset buf1           ;dxhang,cxshu
        mov ah,0ah                  
        int 21h
        mov cl,buf1[si+1]
        push cx
change: 
        mov al,buf1[si+2]
        and al,00001111b            ;根据ascii,减去30h
        mov buf1[si+2],al
        inc si
        loop change
sum:
        xor ax,ax
        mov si,2
        pop cx
loopc:  mov bl,10
        mul bl
        add al,buf1[si]
        inc si
        loop loopc
        push ax
        mov ah,02h                    ;回车换行
        mov dl,0dh
        int 21h
        mov dl,0ah
        int 21h
        pop ax    
        ret
cin 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
;------------------------------------------------------------------------------------------------------------------------------------------
area1 proc                            ;2.S=1/2[(x1y2-x2y1)+(x2y3-x3y2)+(x3y1-x1y3)]65,536‬
    mov ax,a1[0];
    mov bx,b1[2];
    mul bx
    mov area,ax
    mov ax,a1[2];
    mov bx,b1[0];
    mul bx
    sub area,ax
    mov ax,a1[2];
    mov bx,b1[4];
    mul bx
    add area,ax
    mov ax,a1[4];
    mov bx,b1[2];
    mul bx
    sub area,ax
    mov ax,a1[4];
    mov bx,b1[0];
    mul bx
    add area,ax
    mov ax,a1[0];
    mov bx,b1[4];
    mul bx
    sub area,ax
    mov ax,area
    cmp ax,8000h                    ;与0比较
    jb  to
    neg ax
to:    mov bx,2
    div bx
    mov area,ax
loopb1:
    xor dx,dx
    inc cl
    mov bx,10
    div bx                            ;ax商,dx余
    push dx                            ;余数入栈
    cmp ax,0
    jne loopb1
loopc1: 
    pop dx
    or  dl,00110000b
    mov    ah,2                            
    int    21h
    loop loopc1
    ret
area1 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)]

还好,也就400来行。。。。

 

上一篇:linux – 如何通过ld将NASM程序链接到libc?


下一篇:linux – 如何在x86程序集中调用fgets?