开发者

Help understanding part of this generated assembly code

开发者 https://www.devze.com 2022-12-22 04:49 出处:网络
Can anyone explain to me the assembly generated by GCC of the following C++ codes? espeically, the meaning of setg and test in the codes. thx!

Can anyone explain to me the assembly generated by GCC of the following C++ codes? espeically, the meaning of setg and test in the codes. thx!

.cpp codes:

 1   /*for loop*/

 2   int main()

 3   { 
 4      int floop_id;

 5      for(floop_id=100;floop_id>=1;floop_id--)
 6      {}
 7      return 0;
 8   }

assembly codes:

 3      push %ebp               
 3      mov  %esp, %ebp         
 3      sub  $0x10,%esp         
 5      movl $0x64,-0x4(%ebp)   
 5      jmp  8048457<main+0x13> 
 5      subl $0x1,-0x4(%esp)    
 5   开发者_运维知识库   cmpl $0x0,-0x4(%esp)    
 5      setg %al                
 5      test %al, %al           
 7      mov  $0x0,%eax          
 8      leave                   
 8      ret 


cmpl $0x0,-0x4(%esp); setg %al means compare -0x4(%esp) (floop_id in your code) against 0, and set %al to 1 if it's greater, or 0 otherwise.

test %al, %al here isn't doing anything. I don't know why it's in the assembly. (Normally, testing a value with itself is used to get the signum of the value (i.e., zero, positive, or negative), but the result of this isn't being used here. Chances are, it was going to do a conditional branch (to implement the loop), but seeing as your loop is empty, it got removed.)


Your generated assembly code doesn't have the cycle in it (apparently the compiler decided that it is not needed), but it seems to contain some loose remains of that cycle. There are bits that load 100 into a variable, subtract 1 from it, compare it with 0. But there's no actual iteration in the code.

Trying to find any logic in this is a pointless exercise. The compiler, obviously, decided to remove the entire cycle. But why it left some "debris" behind is not clear. I'd say that what is left behind in the code is harmless, but at the same time has as much meaning as a value of an initialized variable.

BTW, where does the unconditional jmp lead to? It is not clear from your disassembly. Doesn't it just jump to 7 right away?

0

精彩评论

暂无评论...
验证码 换一张
取 消