I had tried to make a compare strings function in real mode assembly, but it didn't work, so I tried it another way and it did. It seems to me that both functions I wrote do the same thing yet one doesn't work. Can someone tell me why, the two functions are below.

This one works:

CmpString:
	mov bh, [di]
	cmp bh, [si]
	jne StringRet
	
	cmp bh, 0
	je StringEnd
	add di, 1
	add si, 1
	jmp CmpString
	
	StringEnd:
	stc
	ret
	
	StringRet:
	clc
	ret

And this one did not:

CmpString:
	mov bh, [di]
	cmp bh, [si]
	jne StringRet
	
	cmp bh, 0
	add di, 1
	add si, 1
	jne CmpString
	
	stc
	ret
	
	StringRet:
	clc
	ret

Hi,
The problem is that 'add si, 1' modifies the flags. You can test if bh is equal to zero after adding 1 to si and your code will work right.

;cmp bh, 0 - Remove this line
add di, 1 ; Flags are affected.
add si, 1 ; Flags are affected.
cmp bh, 0 ; - Add this line
jne CmpString

I hope that this can be useful.

Hi,
The problem is that 'add si, 1' modifies the flags. You can test if bh is equal to zero after adding 1 to si and your code will work right.

;cmp bh, 0 - Remove this line
add di, 1 ; Flags are affected.
add si, 1 ; Flags are affected.
cmp bh, 0 ; - Add this line
jne CmpString

I hope that this can be useful.

O thanks, I didn't realize that the add instruction modified the flags thanks.

This question has already been answered. Start a new discussion instead.