Problem 1
a) Write a complete program that will prompt the user for person
records and save all the entered records into a file specified in the
command line.
Note that you should save all the records as binary C-structs in the data file.
You also do not need to do error checking on input data.
The following gives an example of how to run the program and enter the data.
>prob2 datafile Enter record (y/n): y Name: Ayse Cemal Age: 32 Enter record (y/n): y Name: Ali Cemal Age: 33 Enter record (y/n): n
b) What will be the output of the following program ?
#include <stdio.h>
main()
{
int a[3][3] = {1,2,3,4,5,6,7,8,9} ;
int *p, *q ;
p = a[0] ;
q = *a ;
printf("%d %d \n",p[1],q[2]) ;
p = a[1] ;
q = *(a+1) ;
printf("%d %d \n",p[1],q[2]) ;
}
Problem 2
Consider the C program:
/******** C program *********/
#include <stdio.h>
func()
{
int a,b,c ;
register int d ;
a = -4 ;
b = -8 ;
c = a + b ;
d = c + a ;
/* HERE */
}
main()
{
short x ;
x = -4 ;
func() ;
}
This C program is compiled with gcc -S example.c command on the linux which produces
the following gnu assembler output:
.file "exq.c"
.version "01.01"
gcc2_compiled.:
.text
.align 4
.globl func
.type func,@function
func:
pushl %ebp
movl %esp,%ebp
subl $12,%esp
movl $-4,-4(%ebp)
movl $-8,-8(%ebp)
movl -4(%ebp),%edx
addl -8(%ebp),%edx
movl %edx,-12(%ebp)
movl -12(%ebp),%eax
addl -4(%ebp),%eax
.L1:
leave
ret
.Lfe1:
.size func,.Lfe1-func
.align 4
.globl main
.type main,@function
main:
pushl %ebp
movl %esp,%ebp
subl $4,%esp
movw $-4,-2(%ebp)
call func
.L2:
leave
ret
.Lfe2:
.size main,.Lfe2-main
.ident "GCC: (GNU) 2.7.2.3"
Answer the following questions about the above programs:
Problem 3
State whether True or False:
int *p, *q ;
p = (int *) malloc(10*sizeof(int)) ;
q = &(p[2]) ;
free(&(q[-2])) ;
void func(int a[][5]) ;
void func(int (*a)[5]) ;
Problem 4
Answer all of the following short questions about 8086.
MOV AX,513d
MOV CL,3d
DIV CL
MOV DX,1d
MOV AX,8d
MOV CX,8d
MUL CX
code segment
mov bp,arry
mov bx,arry
mov ah,____ ; /* (a) */
input: int 21h
cmp al,10d ; read string until newline
je testpal
mov ____,al ; /* (b) */
inc bp
jmp input
testpal:
dec bp
mov dl,'n'
begloop: cmp bx,bp
jae ispal ; it is palindrome
mov cl,____ /* (c) */
cmp cl,____ /* (d) */
jne notpal ; not a palindrome
inc ____ ; /* (e) */
dec bp
___ ____ /* (f) (g) */
ispal: mov ____,'y' ; /* (h) */
notpal: mov ____,02h ; /* (i) */
___ 21h ; /* (j) */
finish: int 20h
arry:
db ?
code ends
Problem 6
Consider the following C program which generates prime numbers:
/**************************************************************/
#include <stdio.h>
main()
{
int prime[50] ;
int j ;
int k ;
int n ;
int quo,rem ;
P1: prime[0] = 2 ;
n = 3 ;
j = 0 ;
P2: j = j+1 ;
prime[j] = n ;
P3: if (j == 49) goto P9 ;
P4: n = n + 2 ;
P5: k = 1 ;
P6: quo = n / prime[k] ;
rem = n % prime[k] ;
if (rem == 0) goto P4 ;
P7: if (quo <= prime[k]) goto P2 ;
P8: k = k+1 ;
goto P6 ;
P9: for(j=0 ; j < 50 ; j++) printf("%d ",prime[j]) ;
}
/**************************************************************/
The following x86 program generates the 50 prime numbers using the same algorithm as the above C-program. Some instructions/operands have been intentionally left blank.
code segment
;;;;;;;;;;;;;;;;;;;;;;;;;;
; PART 1: Generate primes;
;;;;;;;;;;;;;;;;;;;;;;;;;;
P1: mov bx,prime
mov ___,2d ; /* (a) */
mov si,3
mov cx,0
P2: inc cx
add bx,2
mov w[bx],___ ; /* (b) */
P3: cmp cx,49
____________ ; /* (c) */
P4: ____________ ; /* (d) */
P5: mov bp,prime
add bp,2
P6: ____________ ; /* (e) */
xor dx,dx
div w[bp]
____________ ; /* (f) */
je P4
P7: cmp w[bp],___ ; /* (g) */
jae P2
P8: add bp,___ ; /* (h) */
jmp P6
;;;;;;;;;;;;;;;;;;;;;;
;PART 2: PRINT PRIMES;
;;;;;;;;;;;;;;;;;;;;;;
P9: mov bp,prime
mov si,10d
mov di,0
moreprimes:
cmp di,50
je progfinish
mov ax,[bp]
xor dx,dx
push 20h
mov cx,1d
nonzero:
div si
add dl,"0"
push dx
inc cx
xor dx,dx
cmp ax,0h
jne nonzero
writeloop:
pop dx
mov ah,02h
int 21h
dec cx
jnz writeloop
add bp,2
inc di
jmp moreprimes
progfinish:
int 20h
prime:
dw 2
code ends
a) Fill in the empty boxes shown in the above assembly source. (Write your answer in the comments area). b) Look at the C-source again, and write down the corresponding operand (in PART 1 of assembly source) to the following variables:
Problem 7
The following program reads a line and prints it.
a) Explain in DETAIL whether the following program has any bugs in it.
b) If there are bug(s), rewrite the program without bugs. Note that
you should keep the same functions. You are allowed to change
parameters of functions.
#include <stdio.h>
char *readline()
{
char line[80] ;
printf("Enter a line:\n") ;
fgets(line,79,stdin) ;
return(line) ;
}
void printline(char *line)
{
printf("The line is:\n") ;
printf("%s",line) ;
}
main()
{
int i ;
char *line ;
for(i=0 ; i < 1000000 ; i++) {
line = readline() ;
printline(line) ;
}
}
Problem 8
Write a program that reads a text file containing lines of
up to 60 characters and then prints it on
the standard output in such a way that each line has no more than 40
characters. Note that you should pack as many tokens on a line as possible.
You should also get the name of the text file from the command line.
As an example, the following input:
She sells sea shells by the seashore. The rain in Spain remains in the plains. Debbie bought a little bit of better butter.should produce:
She sells sea shells by the seashore. The rain in Spain remains in the plains. Debbie bought a little bit of butter.
Problem 9
Write a program that reads a text file containing tokens separated
by delimiters (blank,comma, semicolon, newline and period).
The output of your program should
be the list of tokens and their number of occurences in the file.
As an example, for the following file:
If you can dream - and not make dreams your master; If you can think - and not make thoughts your aim; - Rudyard Kipling
You should produce something like the following output: (order of words is not important)
IF 2 YOU 2 CAN 2 DREAM 1 - 3 AND 2 NOT 2 MAKE 2 DREAMS 1 YOUR 2 MASTER 1 THINK 1 THOUGHTS 1 AIM 1 RUDYARD 1 KIPLING 1
Problem 10
Write a C++ program which implements a generic sorting routine (Note:
you can use any sorting algorithm you prefer, e.g. insert sort, bubblesort
etc). You should use some features of C++ that are not available in C
to implement your sorting routine. The following program shows
how your routine is to be used. The mysort routine should accept
array of data items, the number of data items and the comparison
function which takes references to the two data items.
Note that your implementation
should conform to the usage as shown in the program below. If you do not
conform (e.g if you change the number of parameters mysort routine
etc), you will get 0.
#include <iostream.h>
/***** Your answer here *************
Implement mysort routine as well as
any other routine you may need
(e.g. swap) HERE
*************************************/
struct person {
char name[15] ;
int age ;
} ;
int person_compare(
person & p,
person & q)
{
return(p.age - q.age) ;
}
int int_compare(
int & p,
int & q)
{
return(p - q) ;
}
main()
{
int idata[9] = {1,5,3,6,2,7,8,1,1} ;
struct person pdata[] = {
{"Veli",23},{"Ayse",24},{"Can",11},
{"Mehmet",32},{"Ahmet",30},
{"Pamela",20}, {"Cindy",29}} ;
int i ;
// sort the integer array
mysort(idata,9,int_compare) ;
for(i=0 ; i < 9 ; i++) {
cout << idata[i] << endl ;
}
// sort person array according to age
mysort(pdata,7,person_compare) ;
for(i=0 ; i < 7 ; i++) {
cout << pdata[i].name
<< "'s age is "
<< pdata[i].age << endl ;
}
}