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:

    [The Republicans] offer ... a detailed agenda for national renewal.... [On] reducing illegitimacy ... the state will use ... funds for programs to reduce out-of-wedlock pregnancies, to promote adoption, to establish and operate children’s group homes, to establish and operate residential group homes for unwed mothers, or for any purpose the state deems appropriate. None of the taxpayer funds may be used for abortion services or abortion counseling.
    Newt Gingrich (b. 1943)

    Whether in the field of health, education or welfare, I have put my emphasis on preventive rather than curative programs and tried to influence our elaborate, costly and ill- co-ordinated welfare organizations in that direction. Unfortunately the momentum of social work is still directed toward compensating the victims of our society for its injustices rather than eliminating those injustices.
    Agnes E. Meyer (1887–1970)