assembly 绘制Square的汇编代码[已关闭]

yfjy0ee7  于 2023-02-04  发布在  其他
关注(0)|答案(1)|浏览(112)

已关闭。此问题需要超过focused。当前不接受答案。
**想要改进此问题吗?**更新此问题,使其仅关注editing this post的一个问题。

六年前关闭了。
Improve this question
我需要一个代码汇编绘制一个正方形在屏幕中间。
使用BIOS视频服务时,屏幕的分辨率应为320x200。尺寸的长度应为20像素。正方形的颜色应为黑色背景的黄色,如下所示:

kmbjn2e3

kmbjn2e31#

BIOS服务具有极高的开销,不应用于绘制单个像素。
对于“模式0x 13”,您可以直接访问显示存储器-它是每像素1字节,从0xA 000:0x 0000开始,其中每一个水平行跟随前一个水平行(无间隙/填充)。例如:

mov ax,0xA000
    mov es,ax
    xor di,di           ;es:di = address of top left pixel

要填充屏幕的顶行,需要将320字节(像素)设置为零。

mov ax,0xA000
    mov es,ax
    xor di,di             ;es:di = address of top left pixel

    xor ax,ax             ;al = ah = black
    mov cx,320/2          ;cx = number of pairs of pixels to set
    cld                   ;Set direction to make sure
    rep stosw             ;Set entire line to black

现在,您需要将顶部的许多行设置为黑色,因此:

mov ax,0xA000
    mov es,ax
    xor di,di             ;es:di = address of top left pixel

;Do top lines

    mov bx,(200-BOX_Y)/2  ;bx = number of lines at top to make black
    xor ax,ax             ;al = ah = black
.topLoop:
    mov cx,320/2          ;cx = number of pairs of pixels to set
    cld                   ;Set direction to make sure
    rep stosw             ;Set entire line to black
    sub bx,1              ;bx = number of lines left to do
    jne .topLoop          ;Do next line if there are more lines to do

下一部分是方框本身,对于方框所在的每一行,你需要在左边添加一些黑色像素,在中间添加一些黄色像素,在右边添加更多的黑色像素:

;Do middle lines

    mov bx,BOX_Y          ;bx = number of lines in middle
.middleLoop:
    mov cx,(320-BOX_X)/2  ;cx = number of pixels to set
    rep stosb             ;Set left black
    mov cx,BOX_X
    mov al,BOX_COLOUR     ;al = box colour
    rep stosb             ;Set middle coloured part
    mov cx,320-(320-BOX_X)/2 - BOX_X
    xor al,al             ;al = black
    rep stosb             ;Set right black part
    sub bx,1              ;bx = number of lines left to do
    jne .middleLoop       ;Do next line if there are more lines to do

最后,你还想把最下面的几行也设置成黑色,这就像把最上面的几行也设置成黑色一样,整个代码的结尾是这样的:

mov ax,0xA000
    mov es,ax
    xor di,di             ;es:di = address of top left pixel

;Do top lines

    mov bx,(200-BOX_Y)/2  ;bx = number of lines at top to make black
    xor ax,ax             ;al = ah = black
.topLoop:
    mov cx,320/2          ;cx = number of pairs of pixels to set
    cld                   ;Set direction to make sure
    rep stosw             ;Set entire line to black
    sub bx,1              ;bx = number of lines left to do
    jne .topLoop          ;Do next line if there are more lines to do

;Do middle lines

    mov bx,BOX_Y          ;bx = number of lines in middle
.middleLoop:
    mov cx,(320-BOX_X)/2  ;cx = number of pixels to set
    rep stosb             ;Set left black
    mov cx,BOX_X
    mov al,BOX_COLOUR     ;al = box colour
    rep stosb             ;Set middle coloured part
    mov cx,320-(320-BOX_X)/2 - BOX_X
    xor al,al             ;al = black
    rep stosb             ;Set right black part
    sub bx,1              ;bx = number of lines left to do
    jne .middleLoop       ;Do next line if there are more lines to do

;Do bottom lines

    mov bx,200 - BOX_Y - (200-BOX_Y)/2
    xor ax,ax             ;al = ah = black
.bottomLoop:
    mov cx,320/2          ;cx = number of pairs of pixels to set
    rep stosw             ;Set entire line to black
    sub bx,1              ;bx = number of lines left to do
    jne .bottomLoop       ;Do next line if there are more lines to do
  • 注意:以上所有代码都是为NASM编写的(不同的汇编器可能不同);而且根本没有经过任何测试。我假设你是在真实的模式下。你需要定义一些常量来让它工作(BOX_XBOX_YBOX_COLOUR)。在各种情况下,它可以更有效地完成。我没有麻烦你使用什么调用约定,或者推送和弹出修改过的寄存器,或者做一个ret,我已经假设0x 00是黑色的(而且懒得去猜黄色的值是什么);这可能是错误的(如何将值Map到颜色取决于您如何设置调色板,因此任何值都可以是任何颜色,0x 00可能是亮粉色或其他颜色,而不是黑色)*

相关问题