Adding a list of integers Here apply our previous lessons about command-line arguments and numeric conversions Our ‘add.s’ demo • This program allows a user to type a list of unsigned integers as program arguments • Example: $ ./add 11 22 33 • Then the program converts these strings of digit-characters into the integer values they represent, adds those integers, does a number-to-string conversion and shows us the result: ‘The total is 66’ The stack upon entry command-line: $ ./add 11 22 33 application’s stack NULL argv[3] argv[2] argv[1] argv[0] SS:RSP 4 Symbols and data # manifest constants (to make code understandable) .equ STDOUT, 1 # device-file ID-number .equ sys_write, 1 # system-call ID-number .equ sys_exit, 60 # system-call ID-number ten: total: msg: buf: len: # static data (for storing data values in memory) .section .data .quad 10 # the decimal-system radix .quad 0 # for sum of the arguments .ascii “The total is “ # title for program’s output .ascii “ \n” # buffer for the result-string .quad . – msg # length of output message ‘atoi’ and ‘itoa’ • We employ a pair of subroutines that will perform the needed data-conversions: – From a digit-string to an unsigned integer – From an unsigned integer to a digit-string • We call ‘atoi’ for doing ascii-to-integer • We call ‘itoa’ for doing integer-to-ascii Project #1 • Can you design modifications of these two subroutines so that the ‘add.s’ program is able to correctly handle signed integers? • Example: $ ./add 115 -50 10 -200 The total is -125 ‘unsigned’ versus ‘signed’ 9 8 7 -7 6 10 11 12 3 2 15 0 1 6 -5 4 14 7 -6 5 13 -8 5 -4 4 -3 3 2 -2 -1 0 1 Unsigned interpretation Position: 7 6 5 4 3 2 1 0 Weight: 128 64 32 16 8 4 2 1 1 0 0 1 1 1 0 1 Digit: 128 + 0 + 0 + 16 + 8 + 4 + 0 + 1 = 157 Signed interpretation Position: 7 6 5 4 3 2 1 0 Weight: -128 64 32 16 8 4 2 1 1 0 0 1 1 1 0 1 Digit: -128 + 0 + 0 + 16 + 8 + 4 + 0 + 1 = - 99 Negating an 8-bit integer • For the ‘twos complement’ representation of signed integers, you can quickly obtain the negative of a number by applying this two-step rule: “Flip the bits, then add one” Example: Flip the bits: Now add one: Result: negative seven = 11111001 00000110 +1 -------------------------positive seven = 00000111