//---------------------------------------------------------------- // powers3.s (One solution for Midterm I, Question V) // // This assembly language program displays a short numerical // table showing nonnegative integer powers of the number 3, // with 'add' and 'inc' as its only arithmetical operations. // // NOTE: You can assemble-and-link this application using: // // $ g++ powers3.s -o powers3 // // You can use the UNIX 'diff' command to find where changes // were made in the original 'powers.s' source-code: // // $ diff powers3.s powers.s // // // programmer: ALLAN CRUSE // written on: 23 FEB 2005 // revised on: 25 FEB 2005 -- powers of 3, with efficiency //---------------------------------------------------------------- .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 Three \n\n" body: .string " %2d %8d \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 # now check loop-exit condition cmpl $MAX, expon # exponent equals maximum? je finis # yes, no more lines to do # prepare the next exponent and power movl power, %eax # get the previous power addl %eax, %eax # now double its value addl power, %eax # and triple its value movl %eax, power # save this as new power incl expon # add one to the exponent jmp again # and 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