//---------------------------------------------------------------- // born.s // // Here is one possible solution to the programming question // (Question V) that appeared on our Midterm Examination II. // // assemble-and-link using: $ gcc born.s -o born // // programmer: ALLAN CRUSE // written on: 05 APR 2005 //---------------------------------------------------------------- .equ NOW, 2005 # defines symbol for current year .data year: .int 0 # stores the user's year of birth age: .int 0 # stores the user's age this year okmsg: .asciz "You will be %d years old this year \n" ermsg: .asciz "You forgot to type the year you were born \n" .text main: # check for the presence of a command-line argument cmpl $1, 4(%esp) # does 'argc' exceed 1? jg hasarg # yes, we can proceed # display an error-message, then exit to the shell pushl $ermsg # push address of the message call printf # call standard library function addl $4, %esp # discard the function argument jmp finis # and terminate this program hasarg: # convert the argument-string to an integer value movl 8(%esp), %ebx # get pointer to 'argv[]' array pushl 4(%ebx) # push the 'argv[1]' entry call atoi # convert ascii to integer addl $4, %esp # discard function argument mov %eax, year # and save function's value # subtraction gives user's age now: NOW - birthyear movl $NOW, %eax # get current year subl year, %eax # minus birth year movl %eax, age # store difference # report the result of this calculation pushl age # value of the computed age pushl $okmsg # address of format-string call printf # call standard library function addl $8, %esp # discard two function arguments finis: # return control to the command-shell ret # exit to the 'bash' shell .global main .end