如何使用汇编语言绘制用户给出的尺寸的正方形?

时间:2015-05-31 17:09:16

标签: loops assembly draw

到目前为止,我有这个,但我认为代码中的问题在于屏幕如何清除?另外,如果我使用" mov ah,7& int 21h"作为getch(),如果我将它们与之比较的变量是字节大小的,那么它是否会起作用,而" mov ah,0& int 16h"适用于变量是字长的吗?

.model tiny
.code
org 100h    

project:
call ClearScreen
mov ah, 0
int 16h
mov xlimit, ax  ; number of "rows"
mov ah, 0
int 16h
mov ylimit, ax  ; number of "columns"
call border
int 20h

clearScreen:
mov ah, 7
mov al, 25
mov ch, 0
mov cl, 0
mov dh, 24
mov dl, 79
int 10h
mov ah, 2
mov bh, 0
mov dh, 0
mov dl, 0
int 10h
ret
; ----------------------------------------------------
border:
    mov dh, 0
    mov dl, 0
    mov ah, 2
    int 10h
    mov dl, 95
    int 21h
    mov cx, xlimit 

bucle:
    mov dl, cl
    mov dh,0
    int 10h
    mov dl, 95
    int 21h
    mov dx, ylimit
    mov dh, dl
    mov dl, cl
    int 10h
    mov dl, 95
    int 21h
loop bucle
    mov cx, ylimit
    xor dh, dh

bucle2:
    mov dl, 0
    mov dh, cl
    int 10h
    mov dl, 95
    int 21h
    mov dx, xlimit
    mov dh, cl
    int 10h
    mov dl, 95
    int 21h
loop bucle2
ret

; ---------------------------------------------------
; global variables

ylimit dw ?
xlimit dw ?
end project

2 个答案:

答案 0 :(得分:1)

mov ah, 0
int 16h
mov xlimit, ax  ; number of "rows"

此代码只等到按下一个键,然后在AL中给出ASCII,在AH中给出扫描码。要获得有意义的数字,您必须将其转换为数字 如果p.e.你输入一个字符来自" 1"至" 9"你通过从AL减去48并将其存储在 xlimit 中来转换它。在存储到 xlimit 之前,继续将限制用作单词清除AH。

mov ah, 0
int 16h
sub al, 48
cbw
mov xlimit, ax  ; number of "rows"

此评论实际上应该是列数

奖励:您的 clearScreen 例程在调用BIOS之前需要一个额外的参数。

clearScreen:
mov ah, 7
mov al, 25
mov bh, 07h  ;Attribute for the blanked area, WhiteOnBlack
mov ch, 0
mov cl, 0
mov dh, 24
mov dl, 79
int 10h
mov ah, 2
mov bh, 0
mov dh, 0
mov dl, 0
int 10h
ret

答案 1 :(得分:-2)

.model small
.stack 100h
.data
   menu db 'MENU:',13,10,'[A] Horizontal',13,10,'[B] Vertical',13,10,'[C] Diagonal',13,10,'Choice: $'
   msg db 13,10,'Enter number of display: $'
   height db 6
   length db 0
   hash db '#$'
   msgagain db 13,10,'Do you want to try again? $'
   msgtry db 13,10,'Invalid input! Press any key to reset the program..$'
   msgend db 13,10,'Program terminated! Press any key to exit...$'
.code
   mov ax, @data
   mov ds, ax
RESET:   
   mov ax, 03
   int 10h

   mov height, 6

   mov ah, 09
   lea dx, menu
   int 21h

   mov ah, 01
   int 21h

   cmp al, 'A'
   je HOR
   cmp al, 'a'
   je HOR
   cmp al, 'B'
   je VER
   cmp al, 'b'
   je VER
   cmp al, 'C'
   je DIAG
   cmp al, 'c'
   je DIAG
   jmp reset

HOR:       
   mov ah, 09
   lea dx, msg
   int 21h

   mov ah, 01
   int 21h

   cmp al, '1'
   jl ERR
   cmp al, '9'
   jg ERR

   sub al, 30h
   mov bl, 1
   mul bl

   mov cx, ax
   hashhor:
      mov ah, 09
      lea dx, hash
      int 21h
   loop hashhor
   jmp EXIT

ERR:
   mov ah, 09
   lea dx, msgtry
   int 21h

   mov ah, 01
   int 21h  

   jmp RESET

VER:

   mov ah, 09
   lea dx, msg
   int 21h

   mov ah, 01
   int 21h

   cmp al, '1'
   jl ERR
   cmp al, '9'
   jg ERR

   sub al, 30h
   mov bl, 1
   mul bl

   mov cx, ax
   hashver:
      mov ah, 02
      mov bh, 00
      mov dl, 0
      mov dh, height
      int 10h

      mov ah, 09
      lea dx, hash
      int 21h 

      inc height
   loop hashver
   jmp EXIT


DIAG:
   mov ah, 09
   lea dx, msg
   int 21h

   mov ah, 01
   int 21h

   cmp al, '1'
   jl ERR
   cmp al, '9'
   jg ERR

   sub al, 30h
   mov bl, 1
   mul bl

   mov cx, ax
   hashdiag:
      mov ah, 02
      mov bh, 00
      mov dl, length
      mov dh, height
      int 10h

      mov ah, 09
      lea dx, hash
      int 21h 

      inc length
      inc height
   loop hashdiag  

EXIT:
   mov ah, 09
   lea dx, msgagain
   int 21h

   mov ah, 01
   int 21h

   cmp al, 'n'
   je FIN
   cmp al, 'N'
   je FIN  
   jmp RESET

FIN:
   mov ah, 09
   lea dx, msgend
   int 21h

   mov ah, 01
   int 21h

   mov ah, 4ch
   int 21h

END HORIZONAL,DIAGONAL,VERTICAL LOOP