What loop should I write If I want to skip all spaces at the beginning of the string and start to do something with the string when my code reaches the first symbol of the string. If I have a string like
a=' s o m e word'
the code should start when 's' is reached. It should be some kind of loop but I still don't know how to write it correctly.
My try:
mov si, offset buff+2 ;buffer
mov ah, [si]
loop_skip_space:
cmp ah,20h ;chech if ah is space
jnz increase ;if yes increase si
jmp done ;if no end of loop
increase:
inc si
loop loop_skip_space
done:
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.
mov ah, [si]
must be part of the loop. You need to verify different characters from the string, so loading following bytes is necessary.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?.
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:
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