954,479 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Trouble with compareing strings

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
ThatGuy2244
Light Poster
34 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

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.

untio
Light Poster
42 posts since Jan 2011
Reputation Points: 47
Solved Threads: 3
 

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.

ThatGuy2244
Light Poster
34 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: