Where are the null-terminated strings when converting from C to assembly?

saga.x

I made two programs to output two strings, one in assembly and the other one in C. This is the program in assembly:

.section .data
string1:
.ascii "Hola\0"
string2:
.ascii "Adios\0"

.section .text
.globl _start
_start:

pushl $string1
call puts
addl $4, %esp

pushl $string2
call puts
addl $4, %esp

movl $1, %eax
movl $0, %ebx
int $0x80

I build the program with

as test.s -o test.o
ld -dynamic-linker /lib/ld-linux.so.2 -o test test.o -lc

And the output is as expected

Hola
Adios

This is the C program:

#include <stdio.h>
int main(void)
{
    puts("Hola");
    puts("Adios");
    return 0;
}

And I get the expected output, but when converting this C program to assembly with gcc -S (OS is Debian 32 bit) the output assembly source code does not include the null character in both strings, as you can see here:

    .file   "testc.c"
    .section    .rodata
.LC0:
    .string "Hola"
.LC1:
    .string "Adios"
    .text
    .globl  main
    .type   main, @function
main:
.LFB0:
    .cfi_startproc
    leal    4(%esp), %ecx
    .cfi_def_cfa 1, 0
    andl    $-16, %esp
    pushl   -4(%ecx)
    pushl   %ebp
    .cfi_escape 0x10,0x5,0x2,0x75,0
    movl    %esp, %ebp
    pushl   %ecx
    .cfi_escape 0xf,0x3,0x75,0x7c,0x6
    subl    $4, %esp
    subl    $12, %esp
    pushl   $.LC0
    call    puts
    addl    $16, %esp
    subl    $12, %esp
    pushl   $.LC1
    call    puts
    addl    $16, %esp
    movl    $0, %eax
    movl    -4(%ebp), %ecx
    .cfi_def_cfa 1, 0
    leave
    .cfi_restore 5
    leal    -4(%ecx), %esp
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
.LFE0:
    .size   main, .-main
    .ident  "GCC: (Debian 4.9.2-10) 4.9.2"
    .section    .note.GNU-stack,"",@progbits

My two questions are:

1) Why the gcc generated assembly code does not append the null character at the end of both strings? I thought that C did this automatically.

2) If I skip the null characters in my hand made assembly code i get this output:

HolaAdios
Adios

I understand why I get the "HolaAdios" part at the first line, but why does the program end successfully after the "Adios" part if it is not null-terminated?

Armitage.apk
  1. .string always appends a null terminator, as seen here.
  2. Well, you can check it yourself. puts just continues until it sees a null byte. \x00s are very common, there must be one nearby so it works (probably due to section alignment of .rodata).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How are null-terminated strings terminated in C++11?

From Dev

How are null-terminated strings terminated in C++11?

From Dev

Encoding and null terminated strings

From Dev

Encoding and null terminated strings

From Dev

Converting Assembly to C

From Dev

Converting Assembly to C

From Dev

Reading a null terminated string from a binary file c++

From Dev

How are strings terminated in C#?

From Dev

Converting from C Loop to MIPS assembly language (bad address error)

From Dev

Converting from c name sorting code to NASM assembly code crashes

From Dev

What is the different between a null terminated string and a string that is not terminated by null in x86 assembly language

From Dev

Rails NoMethodError when converting strings to date from submitted form

From Dev

Converting C array operations to Assembly

From Dev

x86 assembly compare with null terminated array

From Dev

C++ Combining two zero terminated strings?

From Dev

Converting an SEXP from R into a vector of strings in C++

From Dev

Length of null terminated string in C/C++

From Dev

How to handle null exception when converting from object to string?

From Dev

Ignore trailing NULL characters when converting string from char array

From Dev

How to handle null exception when converting from object to string?

From Dev

Where is the "2+2" in this Assembly code (translated by gcc from C)

From Dev

Set default value to null when converting to double in c#

From Dev

Clean way to read a null-terminated (C-style) string from a file?

From Dev

C# linq using null or empty strings in a where statment

From Dev

C - why is only char array null terminated?

From Dev

Non null terminated array of characters in c++

From Dev

NULL-terminated array of struct types in C

From Dev

C++ ZLib GZipStream Decompression NULL terminated

From Dev

c++ null terminated array of objects

Related Related

  1. 1

    How are null-terminated strings terminated in C++11?

  2. 2

    How are null-terminated strings terminated in C++11?

  3. 3

    Encoding and null terminated strings

  4. 4

    Encoding and null terminated strings

  5. 5

    Converting Assembly to C

  6. 6

    Converting Assembly to C

  7. 7

    Reading a null terminated string from a binary file c++

  8. 8

    How are strings terminated in C#?

  9. 9

    Converting from C Loop to MIPS assembly language (bad address error)

  10. 10

    Converting from c name sorting code to NASM assembly code crashes

  11. 11

    What is the different between a null terminated string and a string that is not terminated by null in x86 assembly language

  12. 12

    Rails NoMethodError when converting strings to date from submitted form

  13. 13

    Converting C array operations to Assembly

  14. 14

    x86 assembly compare with null terminated array

  15. 15

    C++ Combining two zero terminated strings?

  16. 16

    Converting an SEXP from R into a vector of strings in C++

  17. 17

    Length of null terminated string in C/C++

  18. 18

    How to handle null exception when converting from object to string?

  19. 19

    Ignore trailing NULL characters when converting string from char array

  20. 20

    How to handle null exception when converting from object to string?

  21. 21

    Where is the "2+2" in this Assembly code (translated by gcc from C)

  22. 22

    Set default value to null when converting to double in c#

  23. 23

    Clean way to read a null-terminated (C-style) string from a file?

  24. 24

    C# linq using null or empty strings in a where statment

  25. 25

    C - why is only char array null terminated?

  26. 26

    Non null terminated array of characters in c++

  27. 27

    NULL-terminated array of struct types in C

  28. 28

    C++ ZLib GZipStream Decompression NULL terminated

  29. 29

    c++ null terminated array of objects

HotTag

Archive