Homework #9
Functions (Ch. 15) and Pointers and Arrays (Ch. 16)
In order to automate the grading process, please follow the exact instructions for each
questions and don’t use extra or unnecessary words.
1. (5 points) What is the output of the program below?
void MyFunc(int z);
int main()
{
int z = 4;
MyFunc(z);
MyFunc(z);
}
void MyFunc(int z)
{
printf ("%d ", z);
z++;
}
Answer :
2. (5 points) What is the output of the following program?
int x = 0;
int main()
{
int *px = &x;
int x = 10;
*px = 20;
printf("x = %d\n", x);
}
Answer :
3. (5 points) The following code segment has three declared variables, namely a, b, and c.
Which of these variables is equivalent to the expression &b[0] ?
int main()
{
int a;
int *b;
int **c;
b=&a;
c=&b;
...
}
Answer :
4.
(5 points) A 10 element character array is defined in MSP430 assembly code as follows:
BYU, ECEn/CS 124
Homework #9
Page 1/3
.bss hold,10
.text
mov #hold,r12
?????????
Replace the ???????? with ONE assembly line of code to perform the following C statement:
hold[5] = 13;
Answer :
5.
(6 points) In the following piece of code, the variable argv is an array of the command line
arguments passed to the function main.
int main(int argc, char* argv[])
{
int i;
for (i=0;i
{
printf("%c\n", ???????? );
}
return 0;
}
Write TWO expressions to replace the ????? that will print just the 3rd character of each
argument. Use arrays only for the first expression and pointers only for the second expression.
Answer 1:
Answer 2:
BYU, ECEn/CS 124
Homework #9
Page 2/3
6. (9 points) Compile the following C statements into MSP430 assembly code. (Variables are
to be defined on the stack (SP) and referenced from the top of the stack.)
Assuming the stack pointer has 0x027E before the instruction SUB.W
#0x0006,SP
What are the stack assignments (shown below) and values for variables x, y, and z just before
the final brace?
x = ?
y = ?
z = ?
SUB.W
#0x0006,SP
MOV.W
MOV.W
INCD.W
MOV.W
SP,0x0002(SP)
SP,R15
R15
R15,0x0004(SP)
MOV.W
MOV.W
@R15,R15
; **z = 10;
#0x000a,0x0000(R15)
ADD.W
RET
#0x0006,SP
{
; {
; int x;
; int* y;
; int** z;
int x;
int* y;
int** z;
}
y = &x;
z = &y;
**z = 10;
Answers:
Address
Data
0x0280
0x027E Return address
0x027C
0x027A
0x0278
0x0276
BYU, ECEn/CS 124
Homework #9
; y = &x;
; z = &y;
; }
SP
Page 3/3