//---------------------------------------------------------------- // add2.s // // This program reports the sum of two nonnegative integers // that are supplied by the user as command-line arguments. // // assemble using: $ as add2.s -o add2.o // and link using: $ ld add2.o base10io.o -o add2 // // programmer: ALLAN CRUSE // written on: 27 FEB 2006 // revised on: 27 FEB 2006 -- solution to in-class exercise //---------------------------------------------------------------- # manifest constants .equ sys_exit, 1 .equ sys_write, 4 .equ STDOUT, 1 .section .data report: .ascii "\nThe total is " zout: .ascii " \n\n" rptlen: .int . - report # length of report-string z: .int 0 # storage for integer z .section .text _start: leal 8(%esp), %ebx # setup initial arg-pointer nextarg: cmpl $0, (%ebx) # check: pointer is null? je finis # yes, no more srguments # convert the string to a number and add that to z movl (%ebx), %esi # point ESI to the string call asc2eax # convert string to integer addl %eax, z # add this integer to 'z' addl $4, %ebx # advance our arg-pointer jmp nextarg # and process another arg finis: # 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 # 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