loop instruction zeros cx for some reason
Posted: Wed Jun 03, 2009 10:05 pm
I have a doubly nested loop that I use to search the FAT root directory for a filename in a bootloader. The code follows:
For some reason, the second loop instruction zeros cx, and hence does not loop, and falls through to the not_found code. Bochs register dump immediately before the instruction:
Immediately after:
The instruction also messes with the values of bx and di, which are the variables that are changed within the outer loop. Does anyone know why this is happening?
Edit:
Note that it also clears the parity flag; is that relevant?
Code: Select all
.ff_search_loop:
mov si, dx ;; beginning of string
mov di, bx ;; entry to search
push cx ;; save loop counter
mov cx, 11 ;; length of filename
.ff_strcmp_loop:
cmpsb ;; compare bytes
jne .ff_next ;; if unequal, try next entry
loop .ff_strcmp_loop
jmp ff_search_done ;; if loop finishes, then it matches
.ff_next:
add bx, 32 ;; try next entry (32-byte entries)
pop cx ;; restore original loop counter
loop .ff_search_loop ;; if loop finishes, then not found -- this is the problem
ff_not_found:
mov word [ebp - 4], 0xffff ;; return val: not found
jmp ff_done
ff_search_done:
mov word [ebp - 4], bx ;; save entry offset
ff_done:
Code: Select all
eax: 0x00000280 640
ecx: 0x00000200 512
edx: 0x00008f1b 36635
ebx: 0x00009ea0 40608
esp: 0x00009be5 39909
ebp: 0x00009bf9 39929
esi: 0x00008f1c 36636
edi: 0x00009e81 40577
eip: 0x00008d98
eflags 0x00000286
id vip vif ac vm rf nt IOPL=0 of df IF tf SF zf af PF cf
Code: Select all
eax: 0x00000280 640
ecx: 0x00000000 0
edx: 0x00008f1b 36635
ebx: 0x0000de80 56960
esp: 0x00009be5 39909
ebp: 0x00009bf9 39929
esi: 0x00008f1c 36636
edi: 0x0000de61 56929
eip: 0x00008d9a
eflags 0x00000282
id vip vif ac vm rf nt IOPL=0 of df IF tf SF zf af pf cf
Edit:
Note that it also clears the parity flag; is that relevant?