要求:从屏幕左上方开始逆时针用‘A’画一个矩形。
首先知道屏幕长80字符、宽25字符,用x,y记录当前位置,然后循环控制方向,到达边界转变方向即可。这里做了点扩展,由外向内填满整个屏幕。更多细节的完善和扩展请自行实现。
;des 表示方向 1下 2右 3上 四左 delay equ 500 org 100h start: mov ax,cs mov ds,ax mov ss,ax mov ax,0B800h mov es,ax mov byte[char],'A' loop: ;大循环,里面有四个小循环 mov ax,word[i] mov bx,13 sub ax,bx jz end mov ax,word[i] mov word[y],ax dec ax mov word[x],ax mov byte[des],1 inc word[i] down: inc word[x] mov bx,word[x] mov ax,26 sub ax,word[i] sub ax,bx jz d2r jmp show d2r: mov ax,25 sub ax,word[i] mov word[x],ax mov byte[des],2 right: inc word[y] mov bx,word[y] mov ax,81 sub ax,word[i] sub ax,bx jz r2u jmp show r2u: mov ax,80 sub ax,word[i] mov word[y],ax mov byte[des],3 up: dec word[x] mov bx,word[x] mov ax,-2 add ax,word[i] sub ax,bx jz u2l jmp show u2l: mov ax,-1 add ax,word[i] mov word[x],ax mov byte[des],4 left: dec word[y] mov bx,word[y] mov ax,-1 add ax,word[i] sub ax,bx jz loop jmp show end: jmp $ show: ;打印 xor ax,ax mov ax,word[x] mov bx,80 mul bx add ax,word[y] mov bx,2 mul bx mov bp,ax mov ah,0Fh mov al,byte[char] mov word[es:bp],ax jmp loop_wait loop_wait: ;延迟打印 dec word[count] jnz loop_wait mov word[count],delay dec word[dcount] jnz loop_wait mov word[count],delay mov word[dcount],delay mov al,1 cmp al,byte[des] jz down mov al,2 cmp al,byte[des] jz right mov al,3 cmp al,byte[des] jz up mov al,4 cmp al,byte[des] jz left datadef: count dw delay dcount dw delay i dw 0 ;当前层数 x dw 0 ;坐标 y dw 0 des db 1 ;方向初始化为向下 char db 'A'
效果图如下: