2016-03-01 120 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>