In this 16-bit code that fetches its string offset at buff+2
, I believe it's a safe bet to consider this string to belong to the input gotten from executing the DOS.BufferedInput function 0Ah.
The code snippets below are based on this assumption. Next observations about the OP's code remain valid anyway.
- The
mov ah, [si]
must be part of the loop. You need to verify different characters from the string, so loading following bytes is necessary.
- Your code should exit the loop upon finding a non-space character. Currently you exit upon the first space.
- The
loop
instruction requires setting up the CX
register with the length of the string. You omitted that.
mov si, offset buff+2
mov cl, [si-1]
mov ch, 0
jcxz done ; String could start off empty
loop_skip_space:
cmp byte ptr [si], ' '
jne done
inc si ; Skip the current space
loop loop_skip_space
done:
Here SI
points at the first non-space character in the string with CX
characters remaining. CX
could be zero!
You can write better code if you stop using the loop
instruction, because it's an instruction that is said to be slow. See Why is the loop instruction slow? Couldn't Intel have implemented it efficiently?.
- Avoiding
loop
and no longer requiring the use of CX
mov si, offset buff+2
mov cl, [si-1]
test cl, cl
jz done ; String could start off empty
loop_skip_space:
cmp byte ptr [si], ' '
jne done
inc si ; Skip the current space
dec cl
jnz loop_skip_space
done:
- Avoiding
loop
and using the delimiter 13 (carriage return)
mov si, offset buff+1
loop_skip_space:
inc si
cmp byte ptr [si], ' '
je loop_skip_space