//---------------------------------------------------------------- // powers.s // // This assembly language program displays a short numerical // table showing nonnegative integer powers of the number 2, // with 'add' and 'inc' as its only arithmetical operations. // // NOTE: You can assemble-and-link this application using: // // $ g++ powers.s -o powers // // programmer: ALLAN CRUSE // written on: 23 FEB 2005 //---------------------------------------------------------------- .equ MAX, 15 # total number of lines .section .data expon: .int 0 # stores current exponent power: .int 1 # stores current power head: .string "\n Nonnegative Powers of Two \n\n" body: .string " %2d %6d \n" foot: .string "\n" # newline format-string .section .text main: # print the table's title pushl $head # address of format-string call printf # call the runtime library addl $4, %esp # discard the one argument # program loop, to print table's body again: # print next line of the table pushl power # numerical argument #2 pushl expon # numerical argument #1 pushl $body # format-string argument call printf # call the runtime library addl $12, %esp # discard the 3 arguments # prepare the next exponent and power movl power, %eax # get the previous power addl %eax, %eax # now double its value movl %eax, power # save this as new power incl expon # add one to the exponent # but check loop-exit condition cmpl $MAX, expon # exponent exceeds maximum? jnle finis # yes, no more lines to do jmp again # else display another line finis: # print a blank bottom line pushl $foot # format-string argument call printf # call the runtime library addl $4, %esp # now discard the argument ret # return control to caller .global main # makes entry-point public