//----------------------------------------------------------------- // mynameis.s // // Here is a "polished up" version of the program we wrote // in class, to illustrate use of a command-line argument. // // assemble: $ as mynameis.s -o mynameis.o // and link: $ ld mynameis.o -o mynameis // // programmer: ALLAN CRUSE // written on: 14 FEB 2007 //----------------------------------------------------------------- # manifest constants .equ dev_STDOUT, 1 # device-file ID-number .equ sys_WRITE, 4 # system-call ID-number .equ sys_EXIT, 1 # system-call ID-number .section .data msg1: .ascii "\nHey, you forgot to give your name\n\n" len1: .int . - msg1 # length of message #1 msg2: .ascii "\nHow are you today, " len2: .int . - msg2 # length of message #2 eoln: .ascii "?\n\n" # end-of-line string .section .text _start: # check for presence of at least one command-line argument cmpl $1, (%esp) # any argument supplied? je noname # no, show message #1 # show message #2 in case a name was supplied mov $sys_WRITE, %eax # system-call ID-number mov $dev_STDOUT, %ebx # device-file ID-number lea msg2, %ecx # string-address in ECX mov len2, %edx # string's size in EDX int $0x80 # invoke kernel service # setup address and length of the command-line string mov 8(%esp), %ecx # string-address in ECX xor %edx, %edx # start count from zero nxchr: cmpb $0, (%ecx, %edx) # null-byte reached? je gotlen # yes, stop counting inc %edx # else count this byte jmp nxchr # then check next one gotlen: # show the name that was supplied on the command-line mov $sys_WRITE, %eax # system-call ID_number mov $dev_STDOUT, %ebx # device-file ID-number int $0x80 # invoke kernel service # show a question-mark, then skip over a blank line mov $sys_WRITE, %eax # system-call ID-number mov $dev_STDOUT, %ebx # device-file ID-number lea eoln, %ecx # string-address in ECX mov $3, %edx # string length in EDX int $0x80 # invoke kernel service jmp exit # then quit the program noname: # show message #1 in case no name was supplied mov $sys_WRITE, %eax # system-call ID-number mov $dev_STDOUT, %ebx # device-file ID-number lea msg1, %ecx # string-address in ECX mov len1, %edx # string length in EDX int $0x80 # invoke kernel service exit: # terminate this program mov $sys_EXIT, %eax # system-call ID-number xor %ebx, %ebx # setup exit-code zero int $0x80 # invoke kernel kervice .global _start # make entry-point visible .end # nothing else to assemble