Why does this program loop?

samuelbrody1249

I have written the following program, and I was wondering if the behavior of it (1) will always loop; or (2) it has unexpected behavior. I know that I'm missing a ret at the end of the function (and this makes the program run fine), but I was wondering why the program loops when it's not included:

# file.s
.include "utils.s"

.data
str:    .string "Tony"

.text
.globl _start
_start:
    mov $str,   %rdi
    call print_string
    mov $10, %edi
    mov $SYS_EXIT, %eax
    syscall
# utils.o
SYS_EXIT    = 60
SYS_WRITE   = 1
SYS_STDOUT  = 1

print_string:
    # void print_string(char *)
    mov %rdi,       %rsi
    mov $1,         %rdi
    mov $4,         %rdx
    mov $SYS_WRITE, %eax
    syscall

To assemble/link/run:

$ as utils.s -o utils.o && as file.s -o file.o && ld file.o utils.o -o file
$ ./file

When viewing this in gdb after the print_string.syscall it goes back to mov $str, %rdi but why does that occur?

Note: it segfaults when all in one file, but when the print_string is moved to a second file it loops so maybe this is more related to the assembling/linking than the code itself.

Peter Cordes

You did .include "utils.s", so the call print_string will call the copy of print_string you included, not the one in the other .o file (which isn't .globl so isn't even reachable from other files, like a static void foo(char*) function in C).

Execution falls through from the end of print_string to _start.

Don't put code in a file you're going to .include, only definitions of constants and macros. If you did use .globl print_string, then you'd have two conflicting definitions of the same global symbol. And of course you'd be duplicating the definition of every function into every other file that included it, defeating the purpose of linking and functions.


In more detail, your file.s after .include is processed will look like this:

# implicit  .text  - the default section at the top of the file
SYS_EXIT    = 60
SYS_WRITE   = 1
SYS_STDOUT  = 1

print_string:
    # void print_string(char *)
    mov %rdi,       %rsi
    mov $1,         %rdi
    mov $4,         %rdx
    mov $SYS_WRITE, %eax
    syscall

.data
str:    .string "Tony"

.text
.globl _start
_start:
    mov $str,   %rdi
    call print_string
    mov $10, %edi
    mov $SYS_EXIT, %eax
    syscall

Notice that ld won't complain if you omit utils.o because this file has no unresolved symbol references.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why does inputting an unexpected value type via scanf() cause this program to enter an infinite loop

분류에서Dev

Why this Ansi C program does not give result?

분류에서Dev

Why does this C program crash? No bugs are reported

분류에서Dev

Why does this for loop run into an ArrayIndexOtOfBounds error? (Java)

분류에서Dev

Why does this loop break at the first iteration?

분류에서Dev

Why does this for loop that iterates an array throw an exception?

분류에서Dev

Why does the printf function affect my speller program?

분류에서Dev

Why does Bash give "No such file or directory" for a program that's in my PATH?

분류에서Dev

Why this Windows message loop does not handle shorcut/tab keys?

분류에서Dev

Why does a certain if statement only trigger once in a while loop?

분류에서Dev

Why does my program print something before it ends when I press ctrl + D?

분류에서Dev

Why does C program(to delete a line) delete the first character of my .txt file?

분류에서Dev

Why does using WMI to get CPU usage freeze my program for a few seconds?

분류에서Dev

Why does c++ program crash after temporary object is destroyed at end of scope

분류에서Dev

Why does my "Quiz" program keep making two options correct answers?

분류에서Dev

Why does my C program print out the same output no matter what I put for the input?

분류에서Dev

Java thread loop stucks program

분류에서Dev

Why does this `do-while/for loop` never end if you give an invalid input?

분류에서Dev

The program ends straight away, why?

분류에서Dev

Why is this program not accessing child nodes?

분류에서Dev

How does a shell execute a program?

분류에서Dev

React simple fetch program run into an infinite loop

분류에서Dev

Program will not exit a do-while loop

분류에서Dev

How to end program while loop in python

분류에서Dev

Program goes into infinite loop in case of wrong input

분류에서Dev

When does garbage collection happen, between program start and program end?

분류에서Dev

Does Ubuntu automatically release memory allocated by a C program as the program terminates?

분류에서Dev

Why and how does this work?

분류에서Dev

Why does '/' have an '..' entry?

Related 관련 기사

  1. 1

    Why does inputting an unexpected value type via scanf() cause this program to enter an infinite loop

  2. 2

    Why this Ansi C program does not give result?

  3. 3

    Why does this C program crash? No bugs are reported

  4. 4

    Why does this for loop run into an ArrayIndexOtOfBounds error? (Java)

  5. 5

    Why does this loop break at the first iteration?

  6. 6

    Why does this for loop that iterates an array throw an exception?

  7. 7

    Why does the printf function affect my speller program?

  8. 8

    Why does Bash give "No such file or directory" for a program that's in my PATH?

  9. 9

    Why this Windows message loop does not handle shorcut/tab keys?

  10. 10

    Why does a certain if statement only trigger once in a while loop?

  11. 11

    Why does my program print something before it ends when I press ctrl + D?

  12. 12

    Why does C program(to delete a line) delete the first character of my .txt file?

  13. 13

    Why does using WMI to get CPU usage freeze my program for a few seconds?

  14. 14

    Why does c++ program crash after temporary object is destroyed at end of scope

  15. 15

    Why does my "Quiz" program keep making two options correct answers?

  16. 16

    Why does my C program print out the same output no matter what I put for the input?

  17. 17

    Java thread loop stucks program

  18. 18

    Why does this `do-while/for loop` never end if you give an invalid input?

  19. 19

    The program ends straight away, why?

  20. 20

    Why is this program not accessing child nodes?

  21. 21

    How does a shell execute a program?

  22. 22

    React simple fetch program run into an infinite loop

  23. 23

    Program will not exit a do-while loop

  24. 24

    How to end program while loop in python

  25. 25

    Program goes into infinite loop in case of wrong input

  26. 26

    When does garbage collection happen, between program start and program end?

  27. 27

    Does Ubuntu automatically release memory allocated by a C program as the program terminates?

  28. 28

    Why and how does this work?

  29. 29

    Why does '/' have an '..' entry?

뜨겁다태그

보관