gpt4 book ai didi

c - 汇编代码和 Switch 语句案例

转载 作者:太空宇宙 更新时间:2023-11-04 01:24:48 24 4
gpt4 key购买 nike

我无法理解如何通过汇编语言确定案例 50、52 等。

据我了解,跳转表对应于每种情况下要执行的操作,并且检查 edx > 5 意味着情况范围从 0 到 5?我相信 1 被遗漏了,因为它是默认情况,但为什么遗漏了 5?

我感觉应该有一个case 55: where result *= result, no?

如果有人能帮忙解释一下,那就太好了。谢谢!

int switch_prob(int x) 
{
int result = x;
switch (x)
{
case 50:
case 52:
result <<= 2;
break;
case 53:
result >>= 2;
break;
case 54:
result *= 3;
break;
default:
result += 10;
}
return result;
}

Figure 3.38 shows the disassembled object code for the procedure. We are only interested in the part of the code shown on lines 4 through 16. We can see on line 4 that parameter x (at offset 8 relative to %ebp) is loaded into register %eax, corresponding to program variable result. The “lea 0x0(%esi), %esi” instruction on line 11 is a nop instruction inserted to make the instruction on line 12 start on an address that is a multiple of 16

The jump table resides in a different area of memory. Using the debugger GDB, we can examine the six 4-byte words of memory starting at address 0x8048468 with the command x/6w 0x8048468. GDB prints the following:

(gdb) x/6w
0x8048468: 0x080483d5 0x080483eb 0x080483d5 0x0x80483e0
0x8048478: 0x080483e5 0x080483e8
(gdb)

Assembly Code:

1: 080483c0 <switch_prob>:
2: 80483c0: push %ebp
3: 80483c1: mov %esp,%ebp
4: 80483c3: mov 0x8(%ebp),%eax // X is copied into eax ; eax = x
5: 80483c6: lea 0xffffffce(%eax),%edx // placeholder
6: 80483c9: cmp $0x5, %edx // Compare edx (3rd argument) with 5; Edx - 5 // clearly, edx is x
7: 80483cc: ja 80483eb <switch_prob+0x2b> // if edx - 5 > 0, Jump into line 16 (default)
8: 80483ce: jmp *0x8048468(,%edx,4) // Go into the jump table
9: 80483d5: shl $0x2, %eax // eax << 2
10: 80483d8: jmp 80483ee <switch_prob+0x2e> // Jump to line 17
11: 80483da: lea 0x0(%esi),%esi // esi = esi NOP. Filling in N bytes
12: 80483e0: sar $0x2, %eax // eax >> 2
13: 80483e3: jmp 80483ee <switch_prob+0x2e> // Jump to line 17
14: 80483e5: lea (%eax, %eax, 2), %eax // eax = eax + 2(eax)
15: 80483e8: imul %eax, %eax // eax *= eax
16: 80483eb: add $0xa, %eax // eax += 10
17: 80483ee: mov %ebp, %esp // esp = ebp
18: 80483f0: pop %ebp
19: 80483f1: ret

最佳答案

程序集与源代码不匹配。它匹配更像这样的东西:

int switch_prob(int x) 
{
int result = x;
switch (x)
{
case 50:
case 52:
result <<= 2;
break;
case 53:
result >>= 2;
break;
case 54:
result *= 3;
// WARNING: Falls through
case 55:
result *= result;
// WARNING: Falls through
default:
result += 10;
break;
}
return result;
}

这很可能是人为错误造成的(例如,更新问题中的源代码,使其与去年学生得到的问题不相同,但忘记更新程序集以匹配)。

永远不要假设教师/教授不是人类......

关于c - 汇编代码和 Switch 语句案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33207339/

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