gpt4 book ai didi

c - 此汇编 x86 代码的反编译 (C) 代码构造是什么?

转载 作者:行者123 更新时间:2023-12-01 23:12:07 27 4
gpt4 key购买 nike

此代码将字符串(位于 ebp+arg_0)的每个字符与不同的常量(ASCII 字符)(如“I”、“o”和“S”)进行比较。我猜,根据其他代码部分,这段代码最初是用 C 编写的。

assembly

这个比较代码部分看起来非常低效。 我的问题,您认为这段代码在 C 中看起来如何?最初使用什么代码结构?到目前为止我的想法

  • 这不是for 循环。因为我没有看到任何向上跳跃和停止条件。

  • 不是while/case/switch代码构造

  • 我最好的猜测是这是很多连续的 if/else 语句。你能帮忙吗?

是的,这是挑战的一部分,我已经有了标志/解决方案,不用担心。只是想更好地理解代码。

最佳答案

It's not a for loop. Because i don't see any upward jump and stop-condition.

正确。

It's not a while/case/switch code constuct

不可能,它比较的是数组的不同指标。

My best guess is that this are a lot of consecutive if/elses. Can you help?

看起来可能是这段代码:

void f(const char* arg_0) {
if(arg_0[4] == 'I' && arg_0[5] == 'o' && arg_0[6] == 'S') {
printf("Gratz man :)");
exit(0); //noreturn, hence your control flow ends here in the assembly
}
puts("Wrong password"); // Or `printf("Wrong password\n");` which gets optimized to `puts`
// leave, retn
}

This is how gcc compiles it without optimizations :

.LC0:
.string "Gratz man :)"
.LC1:
.string "Wrong password"
f(char const*):
push ebp
mov ebp, esp
sub esp, 8
mov eax, DWORD PTR [ebp+8]
add eax, 4
movzx eax, BYTE PTR [eax]
cmp al, 73
jne .L2
mov eax, DWORD PTR [ebp+8]
add eax, 5
movzx eax, BYTE PTR [eax]
cmp al, 111
jne .L2
mov eax, DWORD PTR [ebp+8]
add eax, 6
movzx eax, BYTE PTR [eax]
cmp al, 83
jne .L2
sub esp, 12
push OFFSET FLAT:.LC0
call printf
add esp, 16
sub esp, 12
push 0
call exit
.L2:
sub esp, 12
push OFFSET FLAT:.LC1
call puts
add esp, 16
nop
leave
ret

看起来与您的反汇编代码非常相似。

This compare-code-part looks very inefficient

看起来它是在没有优化的情况下编译的。启用优化后,gcc compiled the code to :

.LC0:
.string "Gratz man :)"
.LC1:
.string "Wrong password"
f(char const*):
sub esp, 12
mov eax, DWORD PTR [esp+16]
cmp BYTE PTR [eax+4], 73
jne .L2
cmp BYTE PTR [eax+5], 111
je .L5
.L2:
mov DWORD PTR [esp+16], OFFSET FLAT:.LC1
add esp, 12
jmp puts
.L5:
cmp BYTE PTR [eax+6], 83
jne .L2
sub esp, 12
push OFFSET FLAT:.LC0
call printf
mov DWORD PTR [esp], 0
call exit

不知道为什么 gcc 决定跳下来再跳起来而不是直线 jnes。此外,ret 消失了,您的 printf 得到了尾部调用优化,即 jmp printf 而不是 call printf 后跟 ret

关于c - 此汇编 x86 代码的反编译 (C) 代码构造是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55759599/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com