Get value of a compare instruction

Viewed 1677

As I understand it, a cmp instruction will set some of the bits in your flags register. You can then use instructions like jle, jnp, etc. to branch based on those.

What I am wondering is how you can recover an integral value from a comparison.

Example: The following is valid c syntax

y = x[a >= 13];

So a is compared with 13 to get a true or false which is interpreted as 1 or 0 respectively. However, that 1 or 0 must be fed into the array access as an integer. What will the compiler do?

Some things that I can think of are:

Do the comparison and then branch to either x[0] or x[1]

Do the comparison and then branch to do either tmp = 0 or tmp = 1 then do x[tmp]

Maybe do some fancy logic on the flags (not sure if there are instructions to access the flags directly)

I've tried to look at what gcc spits out for this code example but it's impossible to pick out the logic from within all of the extra junk it throws in.

I am working on a compiler so any suggestions would be appreciated.

1 Answers
Related