2016-03-01 74 views
1

我是一個彙編程序設計的新手,並且在我的PC上運行彙編程序時遇到困難。我不完全瞭解運行該程序的過程。我正在做的是試圖運行如下的「HELLO WORLD」程序。需要幫助,在linux mint中運行一個彙編程序

 #include <asm/unistd.h> 
    #include <syscall.h> 
    #define STDOUT 1 
    .data 
    hello: 
      .ascii "hello world\n" 
    helloend: 
    .text 
    .globl _start 
    _start: 
      movl $(SYS_write),%eax // SYS_write = 4 
      movl $(STDOUT),%ebx // fd 
      movl $hello,%ecx  // buf 
      movl $(helloend-hello),%edx // count 
      int  $0x80 

      movl $(SYS_exit),%eax 
      xorl %ebx,%ebx 
      int  $0x80 
      ret 

很抱歉,因爲我自己也不明白大部分不加評論。我只是試圖在我的電腦上設置環境,以便我可以學習組裝。

要運行上述代碼,我所做的是首先將文件保存爲「hello.S」。 然後在當前目錄中打開終端我跑以下命令:

 /lib/cpp hello.S hello.s 
    as -o hello.o hello.s //to generate an object file named hello.o 
    ld hello.o 
    ./a.out 

但運行可執行的a.out之後我沒有看到結果符合市場預期。我的意思是程序應該打印出「hello world」,但我沒有得到任何結果,也沒有任何錯誤信息。我知道這個程序是正確的。所以我的系統或運行程序的方式肯定有問題。

爲什麼運行可執行文件a.out後沒有得到任何結果?

回答

0

讓海灣合作委員會做「沉重的舉動」通常會更好。另請注意,如果您使用的是64位Linux並試圖組裝並運行32位代碼,那麼您可能需要提供-m32以生成32位代碼。

不管怎樣,我把你的代碼,編譯並運行它,如下所示:

linux:~/scratch> cat hello.S 
    #include <asm/unistd.h> 
    #include <syscall.h> 
    #define STDOUT 1 
    .data 
    hello: 
      .ascii "hello world\n" 
    helloend: 
    .text 
    .globl _start 
    _start: 
      movl $(SYS_write),%eax // SYS_write = 4 
      movl $(STDOUT),%ebx // fd 
      movl $hello,%ecx  // buf 
      movl $(helloend-hello),%edx // count 
      int  $0x80 

      movl $(SYS_exit),%eax 
      xorl %ebx,%ebx 
      int  $0x80 
      ret 
linux:~/scratch> gcc -m32 -nostartfiles -nodefaultlibs hello.S 
linux:~/scratch> ./a.out 
hello world 
linux:~/scratch>