Clemson University -- CPSC 231 -- Fall 2009 Command line arguments (section 9.10 of text) arguments to main are passed by the command interpreter int main( int argc, char * argv[] ) argc is the count of command line arguments argv is an array of pointers to character strings each argument is formatted as a separate character string by placing a '\0' after each token argv[0] is the command name (the name under which the program was run) by convention, a leading "-" indicates a program option (usually with one letter following) on Unix systems, the environment variables follow the command line arguments consider /* print out command line arguments */ int main( int argc, char * argv[] ){ int i; for( i = 0; i < argc; i++ ){ printf( "arg %d is \"%s\"\n", i, argv[i] ); } return 0; } for "./a.out -n temp", argc will be 3 and the program will print arg 0 is "./a.out" arg 1 is "-n" arg 2 is "temp" /* Clemson University -- CPSC 231 -- Fall 2000 * * printing command line arguments * * macro processing and assembly commands: * m4 ex3.s * gcc ex3.s * * you should see: * * % a.out * number of strings on command line is 1 * a.out * * % a.out x y z * number of strings on command line is 4 * a.out * x * y * z * +-------+---+---+---+ * |"a.out"|"x"|"y"|"z"| * +----------+ +-------+---+---+---+ * i0 | argc==4 | ^ ^ ^ ^ * +----------+ +---------+ | | | | * i1 | &argv[0]-+---->| argv[0]-+---' | | | * +----------+ +---------+ | | | * | argv[1]-+-----------' | | * +---------+ | | * | argv[2]-+---------------' | * +---------+ | * | argv[3]-+-------------------' * +---------+ * * input parameters (to main): * %i0 (%argc_r) - count of strings on command line * %i1 (%argv_r) - pointer to array of pointers to strings on command line * * return value (%i0): * value of 0 * * other output parameters: * none * * effect/output: * command line strings are printed */ define(`argc_r',i0) define(`argv_r',i1) .global main main: save %sp,-96,%sp set hdr,%o0 mov %argc_r,%o1 call printf nop loop: ! compare this loop with the assembly cmp %argc_r,0 ! code generated for the C loop ble done ! nop ! while( argc != 0 ){ ! set str,%o0 ! printf("%s\n",*argv); ld [%argv_r],%o1 ! call printf ! argv++; nop ! argc--; ! add %argv_r,4,%argv_r ! } dec %argc_r ba loop nop done: mov 0,%i0 ret restore .section ".data" hdr: .asciz "number of strings on command line is %d\n" str: .asciz "%s\n" if the assembly language program is called with %o0 = 5 and %o1 = 0xffbff904 and these memory contents: memory contents ASCII character address in hex equivalents ---------- ---------- --------------- 0xffbff904 : 0xffbffa4c 0xffbff908 : 0xffbffa54 0xffbff90c : 0xffbffa59 0xffbff910 : 0xffbffa5c 0xffbff914 : 0xffbffa5e ... 0xffbffa4c : 0x2e2f612e -- . / a . 0xffbffa50 : 0x6f757400 -- o u t \0 0xffbffa54 : 0x74686973 -- t h i s 0xffbffa58 : 0x00697300 -- \0 i s \0 0xffbffa5c : 0x61007465 -- a \0 t e 0xffbffa60 : 0x73740000 -- s t \0 \0 What will it print?