//---------------------------------------------------------------- // squares.s // // This assembly language program displays a numerical table // showing the squares of the first twenty positive integers // with 'add' and 'inc' as its only arithmetical operations. // // NOTE: Can assemble-and-link using: $ make squares.s // // programmer: ALLAN CRUSE // written on: 31 AUG 2003 //---------------------------------------------------------------- .equ MAX, 20 # total number of lines .section .data arg: .int 1 # stores current number val: .int 1 # stores current square head: .string "\n Table of Squares\n\n" body: .string " %2d %3d \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 val # numerical argument #2 pushl arg # numerical argument #1 pushl $body # format-string argument call printf # call the runtime library addl $12, %esp # discard the 3 arguments # compute the next square movl arg, %eax # fetch the current number addl %eax, %eax # compute twice its value incl %eax # add one to that result addl %eax, val # add this to prior square # and compute next number incl arg # increment current number # check loop exit-condition cmpl $MAX, arg # is number above maximum? jle again # no, display another line # 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 .globl main # makes entry-point public