//---------------------------------------------------------------- // demo2.s // // This is the same program as 'demo1.c' but rewritten using // the GNU assembly language for an Intel Pentium processor. // // assemble using: $ as demo2.s -o demo2.o // and link using: $ ld demo2.o sprintf.o -o demo2 // // programmer: ALLAN CRUSE // written on: 23 JAN 2005 //---------------------------------------------------------------- .equ sys_exit, 1 .equ sys_write, 4 .equ device_id, 1 .section .data x: .int 4 y: .int 5 z: .int 0 fmt: .asciz "%d + %d = %d \n" buf: .space 80 len: .int 0 .section .text _start: # perform the assignment: z = x + y; movl x, %eax addl y, %eax movl %eax, z # call library-function: len = sprintf( buf, fmt, x, y, z ); pushl z pushl y pushl x pushl $fmt pushl $buf call sprintf addl $20, %esp orl %eax, %eax js fini movl %eax, len # make Linux system-call: write( device_id, &buf, len ); movl $sys_write, %eax movl $device_id, %ebx movl $buf, %ecx movl len, %edx int $0x80 fini: # make Linux system-call: exit( 0 ); movl $sys_exit, %eax movl $0, %ebx int $0x80 .global _start .end