Skip to main content
Engineering LibreTexts

16: Command Line Arguments

  • Page ID
    19966
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    This chapter provides a summary of how the operating system handles command line arguments and how assembly language routines can access the arguments. The term “command line arguments” is used to refer to the characters, if any, entered on the command line after the program name. The command line arguments are often used to provide information or parameters to the program without the need for the I/O associated with interactive prompting. Of course, if the parameters are not correct, the entire line must be re-entered. Command line arguments are used by the assembler and linker to provide information for input files, output files, and various other options.

    • 16.1: Parsing Command Line Arguments
      It is the responsibility of the program to determine what is considered correct and incorrect. When reading the command line, the operating system will consider an argument a set of non-space characters (i.e., a string). The space or spaces between arguments is removed or ignored by the operating system. In addition, the program name itself is considered the first, and possibly only, argument. If other arguments are entered, at least one space is required between each argument.
    • 16.2: High-Level Language Example
      This section presents a brief summary of how the C/C++ language handles the delivery of command line information to the program.
    • 16.3: Argument Count and Argument Vector Table
      Since the operating system will call the main program as a function, the standard calling convention applies. The argument count and the argument vector address are passed as parameters in rdi and rsi in accordance with the standard calling convention. The first argument, in rdi, is the integer argument count. The second argument, in rsi, is the argument vector table address.
    • 16.4: Assembly Language Example
    • 16.5: Exercises


    This page titled 16: Command Line Arguments is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Ed Jorgensen.

    • Was this article helpful?