Assembly 8086 - 将一个缓冲区复制到另一个缓冲区

时间:2016-03-21 22:33:46

标签: assembly x86-16

我正在处理一个程序集程序,该程序将整个文本文件读入缓冲区,然后在控制台中显示它。它一次显示24行(每行的最大长度为80,因为我使用8​​0宽* 25高度的幻灯箱)然后等待用户输入,以便用户可以滚动文本。

我想在每行的开头添加行数,因此我想可以制作第二个缓冲区并从第一个复制字符1by1,当我找到换行符时,我会调用一个可以添加该行的过程编号到缓冲区,然后继续,直到我继续通过整个缓冲区。但是我从一个缓冲区复制到另一个缓冲区的方式很糟糕。

所以我想要复制 BUFFA到BUFFB:

mov di,OFFSET BUFFB ;so i set di to the beggining of bufferB




mov si,Pos         ;Pos is my position in the first buffer
lea bx,BUFFA[si]   ;move the content of buffA to bx , i think the problem is here
mov [di],bx        ;move to the addres of di the content of bx
inc di
inc Pos

问题是当我打印出第二个缓冲区的内容时,我发现我将si(与Pos相同)的值复制到我的缓冲区而不是缓冲区A [si]的内容。我该如何修复此代码?

编辑1:

所以解决方案是使用mov和al register:

mov si,Pos
mov al,[BUFF + si]
mov [di],al
inc di

1 个答案:

答案 0 :(得分:7)

你可以使用

lodsb

而不是

mov al,[si]
inc si

stosb

而不是

mov [di],al
inc di

在最好的情况下,您可以将两者结合起来

movsb    ; move byte at [si] to [di], and increase both indices

如果您知道要复制多少字节,您甚至可以使用" rep"移动内存块,然后在CX时间后重复该指令:

cld                    ; make sure that movsb copies forward
mov si, source_buffer
mov di, dest_buffer
mov cx, #amount of bytes to copy
rep movsb

或填充内存块

cld                  ; make sure that stosb moves forward
mov si, buffer       ; start here
mov al, 0xFF         ; fill with 0xFF
mov cx, #20          ; 20 times
rep stosb

如果您使用单词而不是字节,请使用lodsw,stosw和movsw

当方向标志被清除(通过CLD)或向后 - 向后(DEC si / di)时,所有这些指令可以转到 - 向前(INC si / di)。设置方向标志时(通过STD)

相关问题