Joined: 08 Jun 2020 Posts: 122 Location: Migrating
Posted: Wed Sep 15, 2021 2:38 am Post subject: ASM compare against multiple values
Just something I've been wondering about recently, is there a simpler way to check multiple specific values in ASM than actually checking individually?
ex:
Code:
cmp eax,1
je @f
cmp eax,2
je @f
cmp eax,3
je @f
cmp eax,4
jne code
@@:
//something
jmp return
code:
...
or loop (not sure if this is formatted correctly, just typing from phone)
If you know any system programming language (C, C++, Rust...), throw code into a compiler and see what comes out.
e.g. this C++ code:
Code:
void foo();
void bar();
void injection(int a) {
if (a == 1 || a == 2 || a == 3 || a == 4)
foo();
else
bar();
}
Using x86-64 gcc 11.2, unoptimized (-O0), it produces more or less the same code you wrote. Optimized (-O2), it generates this:
Code:
injection(int):
sub edi, 1
cmp edi, 3
ja .L2
jmp foo()
.L2:
jmp bar()
This sub/cmp/jcc pattern is generally what you'll see for comparing whether or not an integer is in a certain range- in this case, 1 to 4 inclusive. _________________
I don't know where I'm going, but I'll figure it out when I get there.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum