简体   繁体   中英

Need help in running an assembly program in linux mint

I am a novice in assembly programming and am struggling in running assembly programs on my PC. I am not quite understanding the process of running the program. What I am doing is trying to run a "HELLO WORLD" program as below.

     #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

Sorry for not adding comments as I myself don't understand most of it. I am just trying to set up the environment on my PC so that I can learn assembly.

To run the above code what I did was first I saved the file as "hello.S". Then by opening the terminal in the current directory I ran the following commands:

     /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

But after running the executable a.out I don't see the result as expected. I mean the program is supposed to print out "hello world", but I don't get any result nor any error messages. I know that the program is correct. So there must be some problem with my system or the way I run the program.

Why am I not getting any result after running the executable a.out?

It's usually better to let gcc do the "heavy lifting". Note also that if you're using a 64 bit Linux and trying to assemble and run 32 bit code then you may need to supply -m32 to generate 32 bit code.

Anyway, I took your code, compiled and ran it as follows:

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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