Search code examples
assemblytasm

Can a pointer register be used twice?


Hi everyone I'm new to assembly and I am having a hard time to understand why the code below works. What I learnt is si holds a address of the data segment and because a and b sit in different places in the data segment it can't be used for both of them and I would have to use di for b. But for some reason the code below (which copies the first char of a to the first char of b) works fine

.model  small
.stack  100H
.data
  a  db   '     This      is     a          test     $'
  b   db   100 dup('$')
.code
  mov si,0
  mov al,a[si]
  mov b[si],al
.exit
end

My main misunderstanding is what is the difference between a[si] to b[si] and to [si] any explanation would be highly appreciated.


Solution

  • What I learnt is si holds a address of the data segment

    Traditionally. SI means "source index." However, ever since Protected Mode, the addressing modes have been loosened up a lot, so si can be used for a lot more. It's a general-purpose register, after all.

    My main misunderstanding is what is the difference between a[si] to b[si] and to [si] [...]

    • [si] takes the address in si and references the data at that address.
    • a[si] takes the address in si plus the address constant a to reference data.
    • b[si] takes the address in si plus the address constant b to reference data.

    The line

    mov al, a[si]
    

    moves the byte at a + si into al.

    mov b[si], al
    

    moves the byte in al into the byte at b + si. That's a transitive chain of operations and so a[si] = b[si] after this.