Matching assembly comparisons

I am trying to understand the following code fragment in assembler:

if ( EAX >= 5 ) EBX = 1; else EBX = 2;" 

In assembler, this is written as (according to my book):

 1 cmp eax, 5 ;(assuming eax is signed) 2 js signon ;goto signon if SF = 1 3 jo elseblock ;goto elseblock if OF = 1 and SF = 0 4 jmp thenblock ;goto thenblock if SF = 0 and OF = 0 5 signon: 6 jo thenblock ;goto thenblock if SF = 1 and OF = 1 7 elseblock: 8 mov ebx, 2 9 jmp next 10 thenblock: 11 mov ebx, 1 12 next: 

I can understand that the resulting flags can be: (if (EAX> = 5)):

SF = 0 and OF = 0 But I can’t understand how the flags can be: SF = 1 and OF = 1? What calculation gives this?

To clarify what I mean:

If eax is in the lower negative border, it will potentially overflow with a positive subtraction of 5. If it were in the upper positive border, could it not overflow into a negative, subtracting 5?

Thanks!

+1
source share
2 answers

It is much easier to think about it in terms of 3-bit numbers, it all scales. Hmmm, if it is signed (you did not specify / set high-level in your code), then four bits are better because you used 5. Pass numbers close to 5 (this shows the output of alu)

 cmp reg,5 0111 - 0101 = 0111 + 1010 + 1 = 10010 0110 - 0101 = 0110 + 1010 + 1 = 10001 0101 - 0101 = 0101 + 1010 + 1 = 10000 0100 - 0101 = 0100 + 1010 + 1 = 01111 0011 - 0101 = 0011 + 1010 + 1 = 01110 

Now you need to understand how the equipment works. Some processor families, when you do the subtraction, invert the carry flag coming from alu, others do not. in any case, you can definitely see a change in state by 5-5 points. And you don’t need the carry flag at all, the code does not use it.

In case you are doing signed math, try some negative numbers as well.

 0000 - 0101 = 0000 + 1010 + 1 = 01011 1111 - 0101 = 1111 + 1010 + 1 = 11010 1110 = 0101 = 1110 + 1010 + 1 = 11001 

And this sheds light on the problem.

a signed overflow is defined as a transfer not equal to the execution on the msbit adder. This can become messy, so we just need to know where this border is.

 0111 - 0101 = 7 - 5 = 2 0110 - 0101 = 6 - 5 = 1 0101 - 0101 = 5 - 5 = 0 0100 - 0101 = 4 - 5 = -1 0011 - 0101 = 3 - 5 = -2 

etc. Using this 4-bit model, in the signed interpretation we are limited to +7 (0b0111) to -8 (0b1000). Thus, after -3-5 we will encounter a problem:

 1110 - 0101 = 1110 + 1010 + 1 = 11001 , -2 - 5 = -7 1101 - 0101 = 1101 + 1010 + 1 = 11000 , -3 - 5 = -8 1100 - 0101 = 1100 + 1010 + 1 = 10111 , -4 - 5 = 7 (-9 if we had more bits) 1011 - 0101 = 1011 + 1010 + 1 = 10110 , -5 - 5 = 6 (-10 if we had more bits) 1010 - 0101 = 1010 + 1010 + 1 = 10101 , -6 - 5 = 5 (-11 if we had more bits) 1001 - 0101 = 1001 + 1010 + 1 = 10100 , -7 - 5 = 4 (-12 if we had more bits) 1000 - 0101 = 1000 + 1010 + 1 = 10011 , -8 - 5 = 3 (-13 if we had more bits) 

The last five represent a signed overflow, a signed result cannot be represented in the number of bits available. (remember that we are playing with four bits now, this top bit is the carry bit, visually delete it when you look at the result).

A signed flag is simply an msbit of result, which also changes interesting boundaries. Cases in which the flag is set under the sign (msbit of result) are positive (eax) values ​​below 5 and negative numbers that do not lead to an overflow value (+4 to -3). All of them are in category <5, so they want to get result 2. The first test is designed for cases when a sign is installed, why is it trying to test a signed overflow? This does not make sense, we already know that all signed results are in category less than 5. an extra jump if the signed overflow does not hurt.

therefore, if you fail js signon, then the sign bit is off, which is equal to numbers greater than or equal to 5 (result 1 is required), or the results are negative enough to cause a signed overflow (result 2 is required). therefore jo elseblock sorts these two cases, choosing the result from two cases (signed overflow, very negative). and jmp thenblock takes positive numbers above 5.

It seems to me that you are doing signed math here (something is obvious due to using a signed overflow flag). Since you use 5 to compare with math and signature, you need 4 or more bits in your system to implement this code, so 8, 32, 64, 123456 bits does not matter, it all works the same as 4-bit system (for this comparison). It’s easier for me to minimize the number of bits for analysis. Hard-coded comparisons like this make it a lot easier, since the above manually calculate the results only higher, in and below. then go through all zeros (zero) to all (minus one) for the signed numbers and very negatively into the sign overflow range. for unsigned numbers, this is a little easier, but the same process.

+1
source

if (EAX> = 5) EBX = 1; another EBX = 2; "

 cmp eax,5 jae biggerthan mov ebx,2 jmp out .biggerthan mov ebx,1 .out 
0
source

All Articles