如何在x86程序集中将字符从一个内存位置复制到另一个内存位置?

时间:2011-11-27 17:57:46

标签: assembly x86 nasm

我正在尝试使用16位程序集复制字符串。

我在.dirBafer中有16个11个字符的字符串,我希望将每个字符串复制到.ime_dat中,以便我可以打印它并稍后处理它(处理代码尚未写入) 。每个字符串的第一个字符由32个字节的数据分隔。基本上.dirbafer是FAT12目录的转储,我正在尝试打印文件名。

我有以下代码:

mov dx, .dirBafer ;loads address of .dirBafer in dx
mov cx, 16 ;16 entries in a dir
.load_dir:

            push cx
            mov ax, dx ;loads address of .dirBafer from dx into ax
            mov bx, .ime_dat ;buffer for storing file names
            mov cx, 11 ;each file name is 11 characters long
.ime_dat_str:
            push dx ; push dx, since it's being used as a temporary register
            mov dx, [ax] ;this is supposed to load first char from location pointed byax to dx
            mov [bx], dx ;this is supposed to load the first char from  location pointed by dx to bx

            add ax, 1 ; moving on to the next character
            add bx, 1 ;moving on to the next character

            pop dx ; we pop the dx so that the original value returns
            loop .ime_dat_str ;this should loop for every character in the file name


            mov si, bx ;preparing to print the file name
            call _print_string ; printing the name
            add dx, 32 ; we move to the next dir entry
            pop cx ; popping cx so that the outer look counter can be updated
            loop .load_dir

.dirBafer   times 512 db 0
.ime_dat    times 12 db 0

我的问题是该行:

mov dx, [ax]生成无效的有效地址错误。

我做错了什么,如何解决?

2 个答案:

答案 0 :(得分:3)

好的,我明白了。似乎对于这样的操作,我需要使用si和di寄存器而不是ax和bx。它们被恰当地命名为源索引和目标索引寄存器。

答案 1 :(得分:2)

DX是一个2字节的寄存器。如果您只想访问一个字节,则应使用DL寄存器:

mov dl, [ax]
mov [bx], dl