2011-11-27 131 views
2

我試圖複製使用16位程序集的字符串。如何在x86程序集中將字符從一個內存位置複製到另一個內存位置?

我有(其中包括).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]產生無效的有效地址錯誤。

我在做什麼錯,我該如何解決?

回答

3

好吧,我想通了。看來對於這樣的操作,我需要使用si和di寄存器來代替ax和bx。它們被合適地命名爲源索引和目標索引寄存器。

+0

如果您查找系統ABI的暫存寄存器,它可能會有所幫助,因爲這些寄存器在通話期間(通常是EAX,ECX和EDX)不會被保留。 – Necrolis

+0

@Necrolis在我的特殊情況下,他們是(好的,我正在使用的)。只是要清楚:如果他們不是,我會得到運行時錯誤,對吧?我在裝配過程中發生錯誤。 – AndrejaKo

+0

啊,是啊,沒關係,在這裏晚了一點,我不是直讀:S,但是,仍然看看你的系統的ABI,它可能會幫助你從長遠來看:) – Necrolis

2

DX是一個2字節的寄存器。如果你只想訪問一個字節,你應該使用DL寄存器:

mov dl, [ax] 
mov [bx], dl 
+0

是的,沒錯,但這個答案對我沒有幫助。即使我這樣做,我也會得到同樣的錯誤。儘管如此,你還是得到了努力。 – AndrejaKo

+0

_print_string是否有可能改變dx的值?這似乎是斧頭可能成爲無效地址的唯一原因。 – extesy

+0

不,_print_string在開始時執行'pusha',在末尾執行'popa',它不使用dx。 – AndrejaKo

相關問題