Skip to main content
Engineering LibreTexts

3.2: Comments on the printName program

  • Page ID
    76104
  • \( \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}}\)

    The printName program changes two things.

    1. The first is it uses the scanf function to read the values from the user. The scanf program uses two registers, r0 and r1, to do this. The first register contains the pointer to (or address of) the string that specifies the value to be read. The value to be read is a string, so the formatting specifier %s is used. In this case, the address is loaded into r0 using the ldr r0,=input1 instruction. Note that the address of the string, or where the string is in memory, is loaded, not the string itself. The string value is generally too large to load into register, so only the reference, which is a 32-bit address, is loaded.

    The second register also contains the address of where to store the result of the scanf function. Space for the input string is allocated in the .data section of the program using the instruction name: space 40, allocating 40 bytes of space to store the user entered name. When the bl scanf instruction is executed, the user can enter a name that will be stored in memory at the allocated space associated with name.

    2. The second change to this program is that the format string for the printf function now includes the format specifier %s. This format specifier indicates that the printf command should look for the address of a string in r1 and will insert that string into the format string at the position of the %s command. It is interesting that the format string passed to the printf command is used to determine what other arguments will be used by the printf function. In C, these are called variadic parameters. Some exercises at the end of this chapter will explore the use of simple variadic parameters.

    Before continuing to the next program, the user is advised to understand the difference between a variable (the memory allocated for data value), the reference to a variable (the address where that variable can be found), and the value of the variable (the data that is assigned to that variable, or the value that variable currently contains). When covering variables in introductory programming, variables are often presented as a box that contains a variable. The box has a name and contains a value. Such a metaphor is at best incomplete, and while useful in an introductory class, it becomes untenable when trying to work with references and values.

    This distinction of variables and references will become even more apparent in the next example, where the inconsistency of the C scanf and printf functions when handling data, particularly data other than strings, is more apparent.


    This page titled 3.2: Comments on the printName program is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Charles W. Kann III via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.