UMBC CMSC 211 |
movsb/movsw | move string (memory to memory) |
lodsb/lodsw | load string (memory to register) |
stosb/stosw | store string (register to memory) |
cmpsb/cmpsw | compare string (memory to memory) |
scasb/scasw | compare string to byte (memory to register) |
cld | Clear D flag (set it to zero or auto-increment) |
std | Set D flag (set it to one or auto-decrement) |
movsb/w |
| ||||||||||||
lodsb/w |
| ||||||||||||
stosb/w |
| ||||||||||||
cmpsb/w |
| ||||||||||||
scasb/w |
|
CpyLoop: | |||||
mov | al, [si] | ||||
mov | es:[di], al | ||||
cmp | al, 0 | ; looking for the null terminator | |||
je | Done | je | Done | ||
inc | si | ||||
inc | di | ||||
jmp | CpyLoop | ||||
Done: |
cld | ; Don't assume the direction! | ||
CpyLoop: | |||
cmp | BYTE PTR [si], 0 | ; looking for the null terminator | |
movsb | ; mov's do not affect flags! | ||
jnz | CpyLoop |
Notice that the compare had to be done before the movsb which changes the si register. When we are done, we did not have to restore the D flag, according to the author. Personally, I think it could be wise to pushf/popf here. Also notice that this is assuming we are working with characters that are a byte in length. Please remember that not all characters are 8-bits long! If we were working with a 16-character set, then the cmp would need a WORD PTR, and the mov becomes movsw. This s movsw. This routine will not work with Unicode, which uses 8/16-bit characters.
for ( i = 0; i < 100; i++ ) { C[i] = A[i] + B[i]; }
mov | si, OFFSET B | ||
mov | bx, OFFSET C - 2 | ||
sub | bx, si | ||
mov | di, OFFSET A | ||
push | ds | ||
pop | es | ||
mov | cx, 100 | ||
cld | |||
AddLP: | |||
lodsw | |||
add | ax, [bx, si] | ||
stosw | |||
loop | AddLp |