user979388
user979388

Reputation: 53

8086 assembly: MOV part of a string into a variable

Assuming I have a string of ascii characters such as "652+346*779=", and I want to move some characters FROM this variable TO another variable...

Buffer is the string (in this case "652+346*779=") lengthofnum is the length of the number in question (in this case 346 has length 3) A_ascii is the variable to which I'm trying to transport the string "346".

I have a loop that doesn't work at all, and I can't figure out what addressing mode I'm supposed to use. emu8086 hates everything I've tried so far, and gives me errors regarding my syntax with the MOV instruction

mov cx,lengthofnum
dumploop1:
    mov bx, offset buffer
    ;dump the number from buffer into A_ascii
    mov A_ascii[cx],[bx]+cx
loop dumploop1:

I get the following error codes:

(672) wrong parameters: MOV  A_ascii[cx],[bx]+cx

(672) probably it's an undefined var: A_ascii[cx] 

Upvotes: 1

Views: 9107

Answers (2)

Polynomial
Polynomial

Reputation: 28316

You can't directly move between two pointers. You need to move it into a register for temporary storage:

mov dx, [bx+cx]
mov [A_ascii+cx], dx

If you've got two blocks of memory that you want to move, the usual method is something like this:

  xor cx, cx                ; set counter = 0
  mov ax, addressOfSource   ; load base addresses
  mov bx, addressOfDest
move_loop:
  mov dx, [ax+cx]           ; load 2 bytes of data from source
  mov [bx+cx], dx           ; move data into dest
  add cx, 2                 ; increment counter
  cmp cx, dataLength        ; loop while counter < length
  jl move_loop

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490098

Contrary to (apparently) popular belief, you can do a direct mem->mem move on an x86, without (explicitly) moving to/from a register. Since you already have the length in CX, you're already started in the right direction:

mov si, offset A_ascii
mov di, offset B_ascii
rep movsb    ; automatically uses length from CX

Upvotes: 6

Related Questions