I was working with the movsb and movsw instruction in assembly language using the flat assembler. Now, i noticed that when the movsb instruction is executed the SI and DI register is incremented by 1 while the movsw instruction increments the SI AND DI register by 2. I am a bit confused why? Can anyone explain me the reason. I am adding my codes below for both the instruction with comments. Please help me out. Thanks!
Using the movsb instruction(Flat assembler)
cld ;clear direction flag
lea si,[val] ;the memory address of source is loaded in the source register
lea di,[v];the memory address of the destination is loaded in the destination register
mov cx,5 ; the counter basically executes for the number of characters in the array
rep movsb ; repeats the instruction for 5 times
val:
db 'A'
db 'B'
db 'C'
db 'D'
db 'E'
v db 5 DUP(?)
Using the movsw instruction(Flat assembler)
cld ;clear the direction flag
lea si,[a];load the address of a in source
;register
lea di,[b] ;load the address of b in destination
;register
mov cx,3
rep movsw ;copy each word from source to
;destination and increment the source register
;by 2 and destination register by 2
a:
dw '1'
dw '2'
dw '3'
b dw 3 DUP(?)
movsw
, movsb
, movsd
, movsq
are instructions used to copy data from the source location DS:(ER)SI
to destination location ES:(ER)DI
.
They are useful because there is no mem-to-mem move instruction in IA32e.
They are meant to be used in cycles (for example by using the rep
prefix), so besides moving the data they also increments (if DF flag, Direction Flag, is 0) or decrements (if DF flag is 1) the pointers to the source and destination locations, i.e. the (ER)SI
and (ER)DI
registers.
From the assembly programmer perspective the memory is byte-addressable, it means that each address store one byte.
movsb
moves a byte, so the register are incremented/decremented by 1.movsw
moves a WORD (2 bytes), so the register are incremented/decremented by 2. movsd
moves a DWORD (Double Word, 2 Word, 4 bytes), so the register are incremented/decremented by 4.movsq
moves a QWORD (Quad Word, 4 words, 8 bytes), so the register are incremented/decremented by 8.