Thursday, August 1, 2013

The Secrets Of cmp

Sign flag and zero flag
efl is flags register

how cmp changes the flags

how the jumps analyze the state of the flag bits


what inc does to the flags, and why we can't inc after we cmp


example programs with issues:

mov eax,1
mov ebx,2
again:
inc ebx   ; inc changes the state of flags due to the value of the last result
mul ebx
cmp ebx,100
jl again


; stops at 0
doit proc
mov eax, -5 ; what will be the sign and carry as we inc eax?
again:
cmp eax, 10
inc eax
jl again
ret


On this blog I'm going to talk about the sign flag and the zero flag. The sign flag and the zero flag are both in the same register and they determine whether or not you pass on when you get to the jl or the jle. Now the secret to what happens in the sign flag and the zero flag lies in cmp. Now if you don't know what cmp means you should read the post called loop de loop 1st.

All that a cmp does is a subtraction. The sign flag shows whether or not the number is negative when you subtract the number that your'e counting to from it. If it is negative the sign flag will be a 1,if it isn't the sign flag will be a 0.  The zero flag shows whether or not the answer is 0. If the answer is 0 the zero flag will be a 1. If the answer isn't 0 the zero flag will be 0.

Here are some examples:

cmp 5, 6

The results would be:      sign flag    1         zero flag    0

cmp 5,5

The results would be:                     0                           1

cmp 6,5                

The results would be:                    0                            0


Those are the 3 possible combinations you can have with this. Now I bet your'e thinking that can't be right if there are 2 bits there should be 4 possible combinations well the one that both of them would be a 1 wouldn't be possible because the answer can't be 0 and negative at the same time.

No comments:

Post a Comment