Conditional jumps - comparing c code with assembly

I am trying to compare function c code with assembly equivalent and kind of confusion on conditional jumps

enter image description here

I was looking for jl instruction and it says jump if <but the answer was >= Can someone explain why this is?

+5
source share
3 answers

As I understand it, the condition is inverted, but the logic is the same; source C defines

if the condition is met, execute the next block

whereas the assembly source determines

if the condition is violated, skip the next block

which means that the execution thread will be the same in both implementations.

+4
source

In essence, what this assembly does fulfills your condition as it is installed, but uses negative logic.

Your condition says:

If a is less then b, return x. Otherwise, return y.

What the assembly code (simplified) says:

Move y to the buffer to return. Move b to another buffer. If a is greater than b, go to the return step. Then y is back. If a is not greater than b, continue in the program. The next step assigns x to the return buffer. The step after that returns as normal.

The result is the same, but the process is slightly different.

+2
source

assembly is performed line by line (code is not included because you placed it as an image):

 foo: return_value (eax) = y; // !!! temporary_edx = b; // x86 can't compare memory with memory, so "b" goes to register set_flags_by(ab); // cmp does subtraction and discards result, except flags "jump less to return" // so when a < b => return y (see first line) return_value (eax) = x; return 

to make C code the same you need:

 if (a >= b) { return x; } else { return y; } 

By the way, see how easy it is to flip:

 if (a < b) { return y; } else { return x; } 

Therefore, it makes no sense to translate jl to “less” in C, you must track each branch that really happens, and find for each calculation branch the correct calculation on the C side, and then “create” a condition in C to get the same calculation with both sides, so this task is not to “translate” the assembly, but to decipher the ASM + logic by rewriting it back to C. It looks like you completely missed the point and expected that you could get away with some simple translation of the “matching template”, while you have to fully process it.

+1
source

Source: https://habr.com/ru/post/1264816/


All Articles