简体   繁体   English

汇编器 printf function 奇怪的行为

[英]Assembler printf function strange behavior

I've opened a simple Hello World program in IDA (Pro 7.5) like that:我在IDA (Pro 7.5) 中打开了一个简单的 Hello World 程序,如下所示:

    global  _main
    extern  _printf

    section .text
_main:
    push    message
    call    _printf
    add     esp, 4
    push    message2
    call    _printf
    add     esp, 4
    ret
message:
    db  'Hello, World 1', 0xD, 0xA, 0 
message2:
    db  'Hello, World 2', 0xD, 0xA, 0

Now i've made a simple detour - moved the code and data definition to the unused end of the code segment like this:现在我做了一个简单的detour - 将codedata定义移动到code段的未使用端,如下所示:

str1  db 'Hello, World1',0Dh,0Ah,0
                                         ; DATA XREF: .text:detour↓o
str2  db 'Hello, World2',0Dh,0Ah,0
                                         ; DATA XREF: .text:00404057↓o
str3  db 'Hello, World3',0Dh,0Ah,0
                                         ; DATA XREF: .text:00404064↓o↓o
      db    0
      db    0
 ; ---------------------------------------------------------------------------

 detour:                                 ; CODE XREF: .text:_main↑j
   push    offset str1     ; "Hello, World 1\r\n"
   call    _printf
   add     esp, 4
   push    offset str2     ; "Hello, World 2\r\n"
   call    _printf
   add     esp, 4
   push    offset str3     ; "Hello, World 3\r\n"
   call    _printf
   add     esp, 4
   jmp     go_back

Leaving in the main function only the jmp code like that:main function 中只留下这样的jmp代码:

_main:                                 
  jmp detour
go_back:                              
  retn

And this yeiled me a problem.这给我带来了一个问题。 The output of the patched program is:修补程序的output为:

Hello, World1

Hello, World2

Hello, World3

ABh@@Hello, World2

Hello, World3

ABh@@Hello, World3

ABh@@

Instead just:而只是:

Hello, World 1
Hello, World 2
Hello, World 3

Where do that extra 3 lines an empty rows额外的 3 行在哪里空行

ABh@@Hello, World2

Hello, World3

ABh@@Hello, World3

ABh@@

come from?来自?

Printf prints strings until it finds a zero symbol. Printf 打印字符串,直到找到零符号。

Your original strings are zero terminated (0 at the end):您的原始字符串以零结尾(末尾为 0):

db  'Hello, World 1', 0xD, 0xA, 0 

The new one, except str3, are not:新的,除了 str3,不是:

str1  db 'Hello, World 1',0Dh,0Ah
str2  db 'Hello, World 2',0Dh,0Ah
str3  db 'Hello, World 3',0Dh,0Ah
      db    0

Thus the first printf call prints all 3 strings, the second str2 and str3, and the third str3.因此,第一个 printf 调用打印所有 3 个字符串,第二个 str2 和 str3,以及第三个 str3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM