//---------------------------------------------------------------- // add.s // // This program reports the sum of two nonnegative integers // that are supplied by the user as command-line arguments. // // assemble using: $ as add.s -o add.o // and link using: $ ld add.o base10io.o -o add // // programmer: ALLAN CRUSE // written on: 27 FEB 2006 //---------------------------------------------------------------- # manifest constants .equ sys_exit, 1 .equ sys_write, 4 .equ STDOUT, 1 .equ STDERR, 2 .section .data errmsg: .ascii "syntax: $ add \n" errlen: .int . - errmsg # length of error-message report: .ascii "\nThe total is " zout: .ascii " \n\n" rptlen: .int . - report # length of report-string .section .bss x: .space 4 # storage for integer x y: .space 4 # storage for integer y z: .space 4 # storage for integer z .section .text _start: # verify that the command-line arguments are present cmp $3, (%esp) # three command-line strings? je argsok # yes, calculation can proceed # display error-message and exit movl $sys_write, %eax # service-ID for 'write' movl $STDERR, %ebx # device-ID for errors leal errmsg, %ecx # address of message movl errlen, %edx # length of message int $0x80 # request kernel service jmp exit # terminate the program argsok: # convert argument 1 to an integer x movl 8(%esp), %esi # point ESI to the argument call asc2eax # convert string to integer movl %eax, x # save this integer as 'x' # convert argument 2 to an integer y movl 12(%esp), %esi # point ESI to the argument call asc2eax # convert string to integer movl %eax, y # save this integer as 'y' # compute z = x + y movl x, %eax # get value of integer x addl y, %eax # add value of integer y movl %eax, z # store sum as integer z # format the report movl z, %eax # setup z in register EAX leal zout, %edi # point EDI to the buffer call eax2asc # convert EAX to a string # display the report movl $sys_write, %eax # service-ID for 'write' movl $STDOUT, %ebx # device-ID for output leal report, %ecx # address of the message movl rptlen, %edx # length of the message int $0x80 # request kernel service exit: # terminate this program movl $sys_exit, %eax # service-ID for 'exit' movl $0, %ebx # setup zero as exit-code int $0x80 # request kernel service .global _start # make entry-point public .end # no more lines to assemble