使用间接寻址在x86程序集中复制字符串

时间:2016-11-23 07:18:19

标签: assembly x86 masm irvine32

我仍然很擅长集会,今晚才开始了解我想做的大部分事情,所以我可能有很多错误,但我很难说出由于缺乏经验。

我正在尝试使用x86程序集中的间接寻址将字符串从源复制到目标。我试图运用我对此解释的理解,但我正在努力理解如何将数据@esi复制到edi中。

.data
val1 BYTE 10h,20h,30h
.code
mov esi,OFFSET val1
mov al,[esi]    ; dereference ESI (AL = 10h)

inc esi
mov al,[esi]    ; AL = 20h

inc esi
mov al,[esi]    ; AL = 30h

这是我到目前为止所做的,我实际上已经让它运行(!!!)但不幸的是它根本没有复制。我怀疑我的循环是坏的(我不确定如何正确地退出)并且我的复制有些疯狂,但我不知道我哪里出错:

INCLUDE Irvine32.inc
.data
   source  BYTE  "Source string I want to copy",0
   target  BYTE  SIZEOF source DUP(0)

.code
main PROC
    mov  esi,OFFSET source              
    mov  edi,OFFSET target             
L1:
    mov  al,[esi]       ; get char from source
    mov  [edi],al       ; store it in target
    inc  esi            ;move to next char in source
    inc  edi            ;move to next position in target
    cmp al, 28          ;compare al to 28 (the number of chars in source string)
    JNZ L1              ;repeat the loop if al != 28 (length of source string)

    mov edx, OFFSET source 
    call WriteString ;Prints out source string (working fine)
    mov edx, OFFSET target
    call WriteString ;Prints out dest string (working fine)

写入字符串在底部正常工作,因为我能够在使用索引编址运行时将其输出“源字符串我想要复制源字符串我要复制”到控制台中。

1 个答案:

答案 0 :(得分:1)

你的字符串是NUL终止的(字符串末尾的,0)。因此,如果修改字符串,请忘记与某些硬编码长度进行比较;只需检查您读取的字节是否为零:

L1:
    mov  al,[esi]       
    mov  [edi],al       
    inc  esi            
    inc  edi            
    test al,al          ; you could also use  cmp al,0  if you prefer that
    JNZ L1              ; repeat the loop if al != 0