






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Exam 2023 2022 final midterm solution
Typology: Exams
1 / 10
This page cannot be seen from the preview
Don't miss anything!
Student ID: Lab Section: First Name(s): ______________________ Last Name: ______________________
This exam contains 8 multi-part questions and you have 90 minutes to earn 9 0 marks.
I hereby declare that I have completed this exam individually, without support from anyone else. I hereby accept that only the below listed sources are approved to be used during this open-source quiz: (i) Coursebook, (ii) All material that is made available to students via Blackboard for this course, and (iii) Notes taken by me during lectures. I have not used, accessed or taken any unpermitted information from any other source. Hence, all effort belongs to me. Signature: ________________________
Question 1 A. Integer Representations, Bitwise Operations [9 POINTS] Assume that you are working on a 1 0 - bit machine, in which
Question 3. C-Strings In the company you are employed, your boss asked you to implement the strtok() function in the standard C library that you used in your second assignment. Recall that strtok() splits a given string into a series of tokens using a set of delimiters provided as a second input (e.g. " \t\r\n"). The function returns a pointer to the first token found in the string, and if there are no tokens left, it simply returns NULL. In doing so, strtok() basically identifies both the beginning and the end of the token so that the next the function is called, it continues the search from the previous ending position. strtok() utilizes a static variable to store the pointer to the beginning of the next token (the end of the extracted token), and let’s call this pointer pNextToken. The implementation of strtok() is given below: char ∗ strtok (char ∗str, const char ∗delim) { /∗ initialize ∗/ if (!str) str = pNextToken; /∗ find start of token in str ∗/ str += strspn(str, delims ); if (∗str == ’\0’) return NULL; /∗ find end of token in text ∗/ pNextToken = str + strcspn(str, delim); /∗ insert null−terminator at end ∗/ if (∗pNextToken != ’\0’) ∗pNextToken++ = ’\0’; return str; } As you may notice, the above implementation uses the two common string functions strspn() and strcspn() and you have to implement these functions by yourself, too. To make things a little bit simpler, you first implemented an helper function named strwhere(), declared as: int strwhere(const char ∗str , const char ch); This function can be used to locate a character in a given string. (a) [4 POINTS] Using the above helper function, implement the strspn() function by writing down the missing pieces in the code below: unsigned int strspn(const char ∗ str, const char ∗ delims) { unsigned int i ; for (i = 0; str[i] != ’\0’ && strwhere(delims, str[i]) > −1; i++); return i ; } (b) [ 4 POINTS] Similarly, using the helper function strwhere(), implement the strcspn() function which computes the index of the first delimiter character in our string. unsigned int strcspn(const char ∗ str, const char ∗ delims) { unsigned int i ; for (i = 0; str[i] != ’\0’ && strwhere(delims, str[i]) == −1; i++); return i ; }
Question 4 A. Pointers and Working with Dynamic Memory In this question, you will try to debug a set of programs and identify the problems associated with them. Here are some common errors and mistakes that you can refer to in your answers:
Question 6 A. Disassembly Please answer the questions below considering the following fragment of the assembly code generated by running the objdump - d command. 4004ed: b9 00 00 00 00 mov $0x0,%ecx 4004f2: eb 16 jmp 40050a <myfunc+0x1d> (a) [ 2 POINTS] The objdump has not included the corresponding suffix to the mov instruction to explicitly state the size information. That being said, you can still identify the size of the destination operand. What is the size and how do you tell? 32 bits/4 bytes (a) [ 2 POINTS] What is the value of %rip register as the CPU executes the mov instruction in the sequence of two instructions? 400 4f (b) [ 10 POINTS] Let’s now look at the entire function from which we took these two instructions. 00000000004004ed
Question 7A. Makefiles [1 POINTS] In your free time you are working on a large project involving multiple source files, which have the following #include dependencies:
#ifndef BEANS_H #define BEANS_H ... #endif
#ifndef COFFEE_H #define COFFEE_H #include "beans.h" ... #endif
#ifndef MILK_H #define MILK_H
#include "beans.h" ...
#include "coffee.h" #include "milk.h"
#include "coffee.h" int main(int argc, char *argv[]) { ... } You can always compile your project using gcc - Wall - g - std=c11 - o main *.c to create an executable program. But as you learned in COMP 201 that this is not a good idea as it is not necessary to recompile and relink the files each time you make a change in just one of the files. Think about the dependencies between the source files (.c), the compiled object (.o) files, and the final executable file (main) that is created by linking the object files, and write down the Makefile that you can use for the project. CC = gcc CFLAGS = - Wall - g - std=c main: beans.o coffee.o main.o $(CC) $(CFLAGS) – o main beans.o coffee.o main.o beans.o: beans.c beans.h $(CC) $(CFLAGS) – c beans.c coffee.o: coffee.c coffee.h beans.h milk.h $(CC) $(CFLAGS) – c coffee.c main.o: main.c beans.h coffee.h $(CC) $(CFLAGS) main.c Each target is of 2 points, and you lose 1 point if you don’t consider the dependencies correctly.
(c) [ 3 POINTS] What is the output of the following C program? #include <stdio.h> #define SQUARE(a) a * a #define TRIPLE(b) 3 * b int main() { int a = 2; int b = 3; int x = TRIPLE(a); int y = SQUARE(a+b); int z = TRIPLE(1+a+b); printf("%d %d %d\n", x, y, z); return 0; } 6 11 8