Assembly 如何用汇编语言绘制正方形?
我想用汇编语言画一个正方形。我骑过一些关于Assembly 如何用汇编语言绘制正方形?,assembly,Assembly,我想用汇编语言画一个正方形。我骑过一些关于int10h的车。有人知道怎么做吗?请在这里放置一些代码,告诉我可以使用什么来绘制正方形,或者一些高质量的教程。谢谢。正如弗雷德里克·哈米迪所说,“int 0x10h”是一个过时的BIOS接口。如果您对汇编感兴趣,我强烈建议您避免使用16位DOS/Masm教程,并在更现代的操作系统(如Linux)上学习 我强烈推荐Jonathan Bartlett的“从头开始编程”: 然而,如果您真的想在16位实模式(即DOS)下绘制直线(或正方形),下面是一个示例:
int10h
的车。有人知道怎么做吗?请在这里放置一些代码,告诉我可以使用什么来绘制正方形,或者一些高质量的教程。谢谢。正如弗雷德里克·哈米迪所说,“int 0x10h”是一个过时的BIOS接口。如果您对汇编感兴趣,我强烈建议您避免使用16位DOS/Masm教程,并在更现代的操作系统(如Linux)上学习
我强烈推荐Jonathan Bartlett的“从头开始编程”:
然而,如果您真的想在16位实模式(即DOS)下绘制直线(或正方形),下面是一个示例:
我能想到的做这样的事情的唯一可能的原因是在原始形状光栅化的方式上获得经验。正如评论者所指出的,你真的不能再在你的CPU的汇编语言中做这种事情了,如果有的话。现代计算机有显卡。也许你想学习如何与那些设备驱动程序对话?或者你也可以使用像WindowsGDI这样的半可移植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中的名称相同
真的,我不想麻烦你。它唯一的价值是演示汇编语言调用,实际上。
int10h
是一种比dirt更早的视频BIOS中断,在当前操作系统中无法从用户模式访问。您正在运行MS-DOS吗?您是指从DOS(模拟)运行吗?如果是,您使用的是什么视频模式?否则,您希望以什么图形API为目标?非常感谢!我想问,如何在下面画更多的线?正如您所看到的,上面的代码将电脑置于320x200x8模式(int 0x0x10,mode 0x13),然后将20个黄色像素(0x44)绘制到视频缓冲区(地址40960==0xa000)。视频缓冲区实际上是一个数组。第一行中的第一个像素是0xa000+0,第一行中的第二个像素是0xa000+1,等等。第二行中的第一个像素是0xa000+200,第二个是0xa000+201,等等。如果要绘制任何行中的任何像素,只需计算偏移量。要画一条水平线,只需加上“1”。要画一条垂直线,只需加上“200”。有道理吗?如果我错了,请纠正我。所以我必须填第一行才能填第二行?但非常感谢你,我想我需要我需要的。这不是一个好例子。您正在执行2字节存储(AX,而不是AL),但只将指针增加1。因此,最后一次迭代将0
字节存储到[20]
,即第21个字节。
; ----------------------------------------------------------------------------
; 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