无论我使用的是printf还是puts,为什么在拆卸时都显示puts? [英] Why it shows puts when I disassemble no matter whether I'm using printf or puts?

查看:64
本文介绍了无论我使用的是printf还是puts,为什么在拆卸时都显示puts?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程还很陌生,想问为什么我用不同的代码会得到相同的结果.我实际上是在读一本书,而本书中的示例是使用printf的(同样在Assembler中).在这种情况下,它说< printf @ plt> .本书中的汇编代码与我的不同,但C代码相同.我的处理器只是在计算不同吗?

I'm pretty new to programming and wanted to ask why I get the same result with different code. I'm actually reading a book and the example in the book is with printf (also in Assembler). In this case it says <printf@plt>. The assembler code in the book differs from mine but C Code is the same. Is my processor just computing different?

(问题在通话中< + 34> < puts @ plt> )

代码1:

#include <stdio.h>

int main()
{
    int i;
    for(i=0; i<10; i++)
    {
        printf("Hello, world!\n");
    }
    return 0;
}

代码2:

#include <stdio.h>

int main()
{
    int i;
    for(i=0; i<10; i++)
    {
        puts("Hello, world!\n");
    }
    return 0;
}

反汇编的代码1:

Dump of assembler code for function main:
   0x080483eb <+0>: lea    ecx,[esp+0x4]
   0x080483ef <+4>: and    esp,0xfffffff0
   0x080483f2 <+7>: push   DWORD PTR [ecx-0x4]
   0x080483f5 <+10>:    push   ebp
   0x080483f6 <+11>:    mov    ebp,esp
   0x080483f8 <+13>:    push   ecx
=> 0x080483f9 <+14>:    sub    esp,0x14
   0x080483fc <+17>:    mov    DWORD PTR [ebp-0xc],0x0
   0x08048403 <+24>:    jmp    0x8048419 <main+46>
   0x08048405 <+26>:    sub    esp,0xc
   0x08048408 <+29>:    push   0x80484b0
   0x0804840d <+34>:    call   0x80482c0 <puts@plt>
   0x08048412 <+39>:    add    esp,0x10
   0x08048415 <+42>:    add    DWORD PTR [ebp-0xc],0x1
   0x08048419 <+46>:    cmp    DWORD PTR [ebp-0xc],0x9
   0x0804841d <+50>:    jle    0x8048405 <main+26>
   0x0804841f <+52>:    mov    eax,0x0
   0x08048424 <+57>:    mov    ecx,DWORD PTR [ebp-0x4]
   0x08048427 <+60>:    leave  
   0x08048428 <+61>:    lea    esp,[ecx-0x4]
   0x0804842b <+64>:    ret    
End of assembler dump.

反汇编代码2:

Dump of assembler code for function main:
   0x080483eb <+0>: lea    ecx,[esp+0x4]
   0x080483ef <+4>: and    esp,0xfffffff0
   0x080483f2 <+7>: push   DWORD PTR [ecx-0x4]
   0x080483f5 <+10>:    push   ebp
   0x080483f6 <+11>:    mov    ebp,esp
   0x080483f8 <+13>:    push   ecx
   0x080483f9 <+14>:    sub    esp,0x14
   0x080483fc <+17>:    mov    DWORD PTR [ebp-0xc],0x0
   0x08048403 <+24>:    jmp    0x8048419 <main+46>
=> 0x08048405 <+26>:    sub    esp,0xc
   0x08048408 <+29>:    push   0x80484b0
   0x0804840d <+34>:    call   0x80482c0 <puts@plt>
   0x08048412 <+39>:    add    esp,0x10
   0x08048415 <+42>:    add    DWORD PTR [ebp-0xc],0x1
   0x08048419 <+46>:    cmp    DWORD PTR [ebp-0xc],0x9
   0x0804841d <+50>:    jle    0x8048405 <main+26>
   0x0804841f <+52>:    mov    eax,0x0
   0x08048424 <+57>:    mov    ecx,DWORD PTR [ebp-0x4]
   0x08048427 <+60>:    leave  
   0x08048428 <+61>:    lea    esp,[ecx-0x4]
   0x0804842b <+64>:    ret    
End of assembler dump.

推荐答案

首选 puts 函数,因为它在功能(无格式字符串解码)和参数传递方面都比较简单.

The puts function is preferred because it is simpler to in both functionality (no format string decoding) and argument passing.

例如,System V ABI x86调用约定要求在 RAX 中设置XMM(YMM)参数的数量( printf 是可变的). puts 更容易,因为只有单个参数与 RDI 一起传递.

For instance, System V ABI x86 calling conventions require to set number of XMM (YMM) arguments (printf is variadic) in RAX. puts is easier, as there is only single argument passed with RDI.

这篇关于无论我使用的是printf还是puts,为什么在拆卸时都显示puts?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆