Section Exercises
True or False
printf takes only one argument which represents the memory address of the first byte of a string.
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 printf functionality try A6: Printf). More important, the first argument is always the format string.
scanf takes a variable number of arguments, the first one being a format string.
True!
Note: Because of this, for scanf, the number of vector registers specified in the al registers will be 0.
Stack pointer relative addressing is useful when the values read with scanf are needed throughout the program/subroutines.
False!
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.
Base pointer relative addressing should be used for values that are used for a short time.
False!
Base pointer relative addressing is useful when value is used as a local variable, for example when it is needed throughout a subroutine.
Stack pointer relative addressing should be used for values that are only accessed once.
True!
Stack pointer relative addressing is useful when the value shpuld 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.
The following assembly code shows a correct usage of scanf: .text .input: .asciz "%d" main: # prolog subq $8, %rsp # ... leaq input(%rip), %rsi leaq -8(%rbp), %rdi movb $0, %al call scanf
False!
First of all, the first argument of scanf, stored in %rdi, should be the address of the string/format that it will read and the second argument, stored in %rsi, should be the location on the stack where the read value will be stored. Moreover, regading calling conventions, the stack is misaligned as we only subtract 8 from %rsp instead of 16 or a multiple of 16.
In the previous question, we should have used movq $0, %a instead of movb $0, %a.
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 bytes), so we need to use movb to indicate that we move one byte.
Multiple Choice
Which directive tells the assambler that a string should be placed in a text section as ASCII-encoded characters?
.asciz
Wrong!
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.
.text
Wrong!
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.
Which format specifiers indicate a signed long, a zero-terminated string and hexadecimal integer?
Last updated