如何用汇编语言绘制方形

时间:2011-07-24 18:06:48

标签: assembly

T想用汇编语言绘制方形。我骑过int 10h的东西。有谁知道,该怎么办?请在这里给我一些代码,我可以用它来绘制正方形或一些高质量的教程。感谢。

2 个答案:

答案 0 :(得分:8)

像Frederic Hamidi所说,“int 0x10h”是一个过时的BIOS界面。如果您对汇编感兴趣,我强烈建议您避免使用16位DOS / Masm教程并学习更现代的操作系统(如Linux)。

我强烈推荐Jonathan Bartlett的“从头开始编程”:

http://savannah.nongnu.org/projects/pgubook/

然而,如果你真的想在16位实模式下(即在DOS中)画一条线(或方形),这是一个例子:

http://lateblt.tripod.com/asm.htm

; Draw a line in a graphics mode

;By extension, draws a yellow line in the upper-left.
;A good example of how to efficiently use INC, CMP,
;and a conditional jump for repetitive tasks.

mov ah,00h
mov al,13h
int 10h

;The above three lines just switch to 320x200 256-color VGA.

mov ds,40960
;a000h = 40960 decimal
mov ax, 44h
;44h is yellow! ;)
mov bx,0000
START:
mov [bx],ax
inc bx
cmp bx,20
JL START

;This waits until BX reaches 20, then exits!

mov ah,004C  ;terminate program
int 21h

答案 1 :(得分:3)

我能想到做这样的事情的唯一可能原因是获得原始形状被光栅化的方式的经验。正如评论者指出的那样,如果有的话,你真的不能用CPU的汇编语言做这种事情了。现代计算机有显卡。也许您想学习如何与这些设备驱动程序通信?或许你可以点击像Windows GDI这样的半便携式API。这个问题中存在太多变数。

只是为了好玩,这是一个轻率的答案。您可以从汇编语言程序访问OpenGL API。几年前(准确地说是2002年),我有一台Windows机器并编写了一个NASM程序来绘制着名红皮书开头的彩色三角形。您可能可以调整它以绘制正方形。这是:

; ----------------------------------------------------------------------------
; triangle.asm
;
; A very simple *Windows* OpenGL application using the GLUT library.  It
; draws a nicely colored triangle in a top-level application window.  One
; interesting thing is that the Windows GL and GLUT functions do NOT use the
; C calling convention; instead they use the "stdcall" convention which is
; like C except that the callee pops the parameters.
; ----------------------------------------------------------------------------

        global  _main
        extern  _glClear@4
        extern  _glBegin@4
        extern  _glEnd@0
        extern  _glColor3f@12
        extern  _glVertex3f@12
        extern  _glFlush@0
        extern  _glutInit@8
        extern  _glutInitDisplayMode@4
        extern  _glutInitWindowPosition@8
        extern  _glutInitWindowSize@8
        extern  _glutCreateWindow@4
        extern  _glutDisplayFunc@4
        extern  _glutMainLoop@0

        section .text
title:  db      'A Simple Triangle', 0
zero:   dd      0.0
one:    dd      1.0
half:   dd      0.5
neghalf:dd      -0.5

display:
        push    dword 16384
        call    _glClear@4              ; glClear(GL_COLOR_BUFFER_BIT)
        push    dword 9
        call    _glBegin@4              ; glBegin(GL_POLYGON)
        push    dword 0
        push    dword 0
        push    dword [one]
        call    _glColor3f@12           ; glColor3f(1, 0, 0)
        push    dword 0
        push    dword [neghalf]
        push    dword [neghalf]
        call    _glVertex3f@12          ; glVertex(-.5, -.5, 0)
        push    dword 0
        push    dword [one]
        push    dword 0
        call    _glColor3f@12           ; glColor3f(0, 1, 0)
        push    dword 0
        push    dword [neghalf]
        push    dword [half]
        call    _glVertex3f@12          ; glVertex(.5, -.5, 0)
        push    dword [one]
        push    dword 0
        push    dword 0
        call    _glColor3f@12           ; glColor3f(0, 0, 1)
        push    dword 0
        push    dword [half]
        push    dword 0
        call    _glVertex3f@12          ; glVertex(0, .5, 0)
        call    _glEnd@0                ; glEnd()
        call    _glFlush@0              ; glFlush()
        ret

_main:
        push    dword [esp+8]           ; push argv
        lea     eax, [esp+8]            ; get addr of argc (offset changed :-)
        push    eax
        call    _glutInit@8             ; glutInit(&argc, argv)
        push    dword 0
        call    _glutInitDisplayMode@4
        push    dword 80
        push    dword 80
        call    _glutInitWindowPosition@8
        push    dword 300
        push    dword 400
        call    _glutInitWindowSize@8
        push    title
        call    _glutCreateWindow@4
        push    display
        call    _glutDisplayFunc@4
        call    _glutMainLoop@0
        ret

现在这个程序只适用于Windows,你需要在你的机器上安装OpenGL和GLUT。

如果你需要为Linux(或Mac或Solaris或其他)调整它,你需要弄清楚所有的OpenGL调用是如何工作的;它们几乎肯定不会与我使用的Windows API中的名称相同。

我真的不打扰它。它唯一的价值就是说明汇编语言调用。