将字符串元素移动到汇编中的另一个字符串
Moving string elements into another string in assembly
我正在尝试通过汇编语言将一个字符串中的元素移动到另一个字符串中。问题是我在组装时不断收到错误,例如:"Can't add relative types"。为什么会发生这种情况?我该如何解决才能将所有元素从 STRING 复制到 STRING2?
DATA SEGMENT PARA PUBLIC 'DATA'
STRING DB 1, 2, 3, 4 ; first string
LENGTH DB $-STRING ; length of the first string
ITERATOR DB 0
DATA ENDS
DATA SEGMENT PARA PUBLIC 'DATA'
STRING2 DB 100 DUP (?) ; second string
DATA ENDS
CODE SEGMENT PARA PUBLIC 'CODE'
START PROC FAR
ASSUME CS:CODE, DS:DATA
PUSH DS
XOR AX, AX
PUSH AX
MOV AX,DATA
MOV DS,AX
MOV CL, LENGTH ;
THELOOP:
MOV ITERATOR, CL
SUB ITERATOR, 1
MOV STRING2[ITERATOR], STRING[ITERATOR] ; this is where the error appears
LOOP THELOOP
RET
START ENDP
CODE ENDS
END START
这个表达式,STRING[ITERATOR]
无法在 Intel 汇编中表达。
您只能在一条指令中使用一个内存引用;其他任何东西都必须是一个寄存器。
此外,您不能直接将内存移动到内存,除非使用 movs
:
push ds
push es
mov ax, DATA
mov ds, ax
mov es, ax
mov si, offset STRING
mov di, offset STRING2
xor ch, ch
mov cl, [LENGTH]
rep movsb ; movsb: move a byte from ds:si to es:di, and increment si and di
pop es
pop ds
或者,更接近您的解决方案,您必须将内存值加载到寄存器中,然后存储它:
xor bx, bx
xor ch, ch # the loop instruction uses all of cx!
THELOOP:
mov al, STRING[bx]
mov STRING2[bx], al
inc bx
loop THELOOP
我正在尝试通过汇编语言将一个字符串中的元素移动到另一个字符串中。问题是我在组装时不断收到错误,例如:"Can't add relative types"。为什么会发生这种情况?我该如何解决才能将所有元素从 STRING 复制到 STRING2?
DATA SEGMENT PARA PUBLIC 'DATA'
STRING DB 1, 2, 3, 4 ; first string
LENGTH DB $-STRING ; length of the first string
ITERATOR DB 0
DATA ENDS
DATA SEGMENT PARA PUBLIC 'DATA'
STRING2 DB 100 DUP (?) ; second string
DATA ENDS
CODE SEGMENT PARA PUBLIC 'CODE'
START PROC FAR
ASSUME CS:CODE, DS:DATA
PUSH DS
XOR AX, AX
PUSH AX
MOV AX,DATA
MOV DS,AX
MOV CL, LENGTH ;
THELOOP:
MOV ITERATOR, CL
SUB ITERATOR, 1
MOV STRING2[ITERATOR], STRING[ITERATOR] ; this is where the error appears
LOOP THELOOP
RET
START ENDP
CODE ENDS
END START
这个表达式,STRING[ITERATOR]
无法在 Intel 汇编中表达。
您只能在一条指令中使用一个内存引用;其他任何东西都必须是一个寄存器。
此外,您不能直接将内存移动到内存,除非使用 movs
:
push ds
push es
mov ax, DATA
mov ds, ax
mov es, ax
mov si, offset STRING
mov di, offset STRING2
xor ch, ch
mov cl, [LENGTH]
rep movsb ; movsb: move a byte from ds:si to es:di, and increment si and di
pop es
pop ds
或者,更接近您的解决方案,您必须将内存值加载到寄存器中,然后存储它:
xor bx, bx
xor ch, ch # the loop instruction uses all of cx!
THELOOP:
mov al, STRING[bx]
mov STRING2[bx], al
inc bx
loop THELOOP