Sunteți pe pagina 1din 4

C

Programming and Assembly Language


Assignment-0
Instructions:
1. All numbers are in the decimal system by default.
2. Hexadecimal numbers should be prefixed with “Ox”
3. Unless otherwise stated all programs are assumed to compile successfully –
Don’t get stuck with syntax errors. That’s not the focus of the course.
4. Do not execute the program on a computer and write the output. This is for
your self-assessment. You are encouraged to execute the program and check
once you have completed the assignment though.

Consider the following C program for questions 1 and 2.

#include <stdio.h>
#define ADD_FN(X,Y) X+Y

int fn_A(int x, int y)
{
return (x+y);

}

int fn_A(int x, int y)
{
return (x-y);

}

int fn(int x, int y)
{
return (x/y);

}

void main(){
int x, y, w, z;
x=20;
y=30;
z = fn(x,y); //z= 20/30 (quotient) = 0;
w =ADD_FN(x,y)*ADD_FN(z, x); // w = x+y*z+x = 40
printf(“X=%d,Y=%d, Z=%d,W=%d”, x, y, z, w);
}

Answer the following:
1. State True or False (Marks 5X1=5)
a. This will result in a compilation error because variable names x and y
are common to the functions fn and main. – F – Local variable names
can be common across functions
b. This will result in a compilation error because two functions (fn_A)
are defined with the same name. – T – Functions need to have unique
names.
c. Functions in C programs can only return integer values. – F – You can
return any data type. Even a struct.
d. ADD_FN does not get compiled into a subroutine/ function. – T – This
is a macro and hence gets substituted inline by text replacement. After
that the expression for “w” will be w= x+y*z+x; Apply BODMAS and
solve that equation.
e. The size of the first argument to printf is 19 – F – The first argument
to printf is a char* pointer and hence is 4 bytes irrespective of the
string length.
2. Assume that all compilation errors (if any) have been fixed in the program
and is now executed. The output of the program is
a. X=__20___,Y=__30__,Z=__0___,W=__40__ Marks: (1+1+2+2 = 6)

Consider the following C program for question 3

#include <stdio.h>

int fn(int* x, int n)
{
*x= n;
return (n+100);
}

void main()
{
int x, y;
y = fn(&x,250); // After the execution of this function x=250 and y=350.
Since x is passed as a pointer it’s value is assigned inside the function “fn”.
printf(“OUTPUT:X=%d,Y=%d”+3, x, y);
}

3. The output of the program is
a. __PUT__:X=__250___,Y=___350___ (Marks: 2+1+1=4)
Comments: “OUTPUT:X=%d,Y=%d”+3 Should be evaluated as follows: The constant
string “OUTPUT:X=%d,Y=%d” will be stored in memory and it’s address will be a
char* pointer. So when you do +3 the pointer address increments by 3 bytes
pointing to the string “PUT:X=%d,Y=%d”.
Consider the following C program for question 4

#include <stdio.h>

int* fn(int*y)
{
int x= 27;
*y=x*4;
return &x;
}

void main()
{
int* pA;
int x, z, y=25;
z = y; // z = y =25
pA = fn(&y); // y = 27*4 = 108 since y is passed as a pointer into the function
“fn”, it gets initialized in there. “pA” will point to the local variable x in the function
“fn” which is initialized to 27 and hence *pA = 27.
printf(“X=%d,Y=%d,Z=%d”, *pA, y, z);
}
4. The output of the program is
a. X=___27___,Y=___108___ Z=___25____ (Marks: 2+2+1)


Consider the following for questions 5-8:

You are given an 8 bit microprocessor with three registers general purpose registers
A, B and C, an instruction pointer IP and the following instruction set:

Mnemonic Functionality Comment
MOV R1, R2 R1 = R2 Copy contents of Register R1 to R2
MOV R1, #N R1 = N Copy a constant N to R1
ADD R1, R2 R1 = R1 + R2 Addition
CALL #FN_ADDR IP = FN_ADDR Subroutine call – also PUSHES the return
address on stack
RET IP= TOS Top of stack loaded into IP
END Suspends execution of the program with the
IP unaltered.


The following Assembly Program is loaded in the code segment and executed with
the instruction pointer initialized to the address 0xF0.

ADDRESS INSTRUCTION Comments
0xE0 MOV C, B Move contents of B to C
0xE1 MOV B, A Move contents of A to B
0xE2 MOV A, C Move contents of C to A
0xE3 RET Function swaps registers A and B with C as a
temporary register

0xF0 MOV A, #2 A=2
0xF2 MOV B, #26 B=26
0xF4 CALL #E0 A=26, B=2 and C=26
0xF6 ADD C, A C = C + A = 52
0xF7 END Program terminates here

Answer the following: (Marks = 1 + 1 + 1 + 2)
5. The value of IP at the end of the program = ___0xF7___
6. Value of register A = ___26___
7. Value of register B = ___2____ Comment: This was incorrect in the assignment
evaluation. The correct answer is 2 and not 4. The assignment checked for 4.
8. Value of register C = ___52___

S-ar putea să vă placă și