GNU Assembler - Example Programs

Example Programs

A standard “Hello, world!” program for Linux on IA-32:

.data msg: .asciz "Hello, world!\n" # "asciz" will cause the assembler to append # the null byte that many C library routines # expect. The C library isn't being used in # the example. MSGLEN = . - msg # this will cause the assembler to count the number of bytes # between the label "msg" and here. The number will be 16, # as the null byte is included. .text .globl _start _start: movl $MSGLEN,%edx # argument 3 for write. Number of bytes to write. # A macro is used, which means the MSGLEN will be # replaced with the immediate value 0xf (15) during # assembly. Comparable to C preprocessor macros. movl $msg,%ecx # argument 2 for write. Pointer to data. movl $1,%ebx # argument 1. file descriptor number. (stdout=1, see article on file descriptors) movl $4,%eax # syscall number 4 = write(2) int $0x80 xorl %ebx,%ebx # argument for syscall. exit code = 0 (xor of a value with itself always equals 0) movl $1,%eax # syscall number 1 = exit(2) int $0x80

A standard “Hello, world!” program for FreeBSD and Mac OS X on IA-32:

.data msg: .asciz "Hello, world!\n" MSGLEN = . - msg .text .globl _start _start: pushl $MSGLEN pushl $msg pushl $1 movl $4,%eax subl $4,%esp int $0x80 addl $16 pushl $0 movl $1,%eax subl $4,%esp int $0x80

Read more about this topic:  GNU Assembler

Famous quotes containing the word programs:

    There is a delicate balance of putting yourself last and not being a doormat and thinking of yourself first and not coming off as selfish, arrogant, or bossy. We spend the majority of our lives attempting to perfect this balance. When we are successful, we have many close, healthy relationships. When we are unsuccessful, we suffer the natural consequences of damaged and sometimes broken relationships. Children are just beginning their journey on this important life lesson.
    —Cindy L. Teachey. “Building Lifelong Relationships—School Age Programs at Work,” Child Care Exchange (January 1994)

    Government ... thought [it] could transform the country through massive national programs, but often the programs did not work. Too often they only made things worse. In our rush to accomplish great deeds quickly, we trampled on sound principles of restraint and endangered the rights of individuals.
    Gerald R. Ford (b. 1913)