> For the complete documentation index, see [llms.txt](https://computerscienceeducation.gitbook.io/co-lab-manual/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://computerscienceeducation.gitbook.io/co-lab-manual/reference-documentation/input-output/section-exercises.md).

# Section Exercises

### True or False

<details>

<summary><code>printf</code> takes only one argument which represents the memory address of the first byte of a string.</summary>

**False!**

`printf` takes a variable number of arguments depending on how many format specifiers are in your string (if you want to deepen you knowledge regarding the `printf` functionality try [A5: Printf](/co-lab-manual/assignments/extra-assignments/a6-printf.md)). More important, the first argument is always the format string.

</details>

<details>

<summary><code>scanf</code> takes a variable number of arguments, the first one being a format string.</summary>

**True!**

Note: Because of this, for `scanf`, the number of vector registers specified in the `al` registers will be 0.

</details>

<details>

<summary>Stack pointer relative addressing is useful when the values read with <code>scanf</code> are needed throughout the program/subroutines.</summary>

**False!**&#x20;

The base pointer relative addressing is useful in this case. Stack pointer relative addressing is useful for values used once or for a short period of time.

</details>

<details>

<summary>Base pointer relative addressing should be used for values that are used for a short time.</summary>

**False!**

Base pointer relative addressing is useful when the value of a variable is needed throughout a program/subroutine.

</details>

<details>

<summary>Stack pointer relative addressing should be used for values that are only accessed once.</summary>

**True!**

Stack pointer relative addressing is useful when the value should only be stored on the stack until it can be moved to registers for further usage, so for values that are only used once or for a short time.

</details>

{% tabs %}
{% tab title="Question" %}
The following assembly code shows a correct usage of `scanf`:

<pre class="language-c"><code class="lang-c">.text
.input: .asciz "%d"

main:
    # prolog
    subq $8, %rsp
    # ...
    leaq input(%rip), %rsi
<strong>    leaq -8(%rbp), %rdi
</strong><strong>    movb $0, %al
</strong>    call scanf
</code></pre>

{% endtab %}

{% tab title="Answer" %}
**False!**

The first argument of `scanf`, stored in `rdi`, is the address of the string/format that it will read and the second argument, stored in `rsi`, is the location on the stack where the read value will be stored. Moreover, regarding calling conventions, the stack is misaligned as we only subtract 8 from `rsp` instead of 16 or a multiple of 16.&#x20;
{% endtab %}
{% endtabs %}

<details>

<summary>In the previous question, we should have used <code>movq $0, %al</code> instead of <code>movb $0, %al</code>.</summary>

**False!**

In the register section, we saw that the name of the register can indicate its size and location. In this case, `al` stores 8 bits (1 byte), so we need to use `movb` to indicate that we move one byte.&#x20;

</details>

### Multiple Choice

Which directive tells the assembler that a string should be placed in a text section as ASCII-encoded characters?

<details>

<summary><code>.asciz</code></summary>

Correc&#x74;**!**&#x20;

The `.asciz` directive indicates that a string should be placed in a text section as ASCII-encoded characters, but it also indicates the fact that the string will be followed by a zero byte.

</details>

<details>

<summary><code>.ascii</code></summary>

**Correct!**

</details>

<details>

<summary><code>.text</code></summary>

**Wrong!**&#x20;

The .text directive specifies that the following line should be placed in the text section of the program, but does not indicate the type or encoding of the content of the line.

</details>

<details>

<summary><code>.equ</code></summary>

**Wrong!**

The `.equ` directive can be used to define symbolic names for expressions, such as numeric constants.

</details>

Which format specifiers indicate a signed long, a zero-terminated string and hexadecimal integer?

<details>

<summary><code>d</code>, <code>s</code>, <code>x</code></summary>

**Wrong!**&#x20;

`d` indicates a signed integer.

</details>

<details>

<summary><code>ld</code>, <code>s</code>, <code>lx</code></summary>

**Wrong!**&#x20;

`lx` indicates a hexadecimal long.

</details>

<details>

<summary><code>ld</code>, <code>s</code>, <code>x</code></summary>

**Correct!**

</details>

<details>

<summary><code>d</code>, <code>s</code>, <code>lx</code></summary>

**Wrong!**&#x20;

`d` indicates a signed integer, while %lx indicates a hexadecimal long.

</details>
