Assignment 8 and 9

2 Questions

2.1 sort nodes

(80 points) You are given structure struct Node below, where next points to the next node in the list (or NULL if it is the last node in the list), and prev presents to the previous node in the list (or NULL if it is the ﬁrst node).(see Figure 1): 1 struct Node { struct Node ∗next ; 3 struct Node ∗prev ; }; 5 typedef struct Node Node;

Implement a function Node *sort nodes(Node *head) that returns head of list such that: &node1 &n2 &n3 … &last node You will submit sorter.c and node.h. sorter.c will contain the sort nodes function. node.h will contain the deﬁnition of the Node structure and the prototype for the sort nodes function.

2

Figure 1: Lists before and after sorting. Arrows going left to right indicate “next” pointers, and arrows going right to left indicate the “prev” pointers.

struct _Node 0x1300

struct _Node 0x1000

struct _Node 0x3000

struct _Node 0x2000

next next next next NULL NULL

prev prev prev prev

Node* head

struct _Node 0x3000

struct _Node 0x2000

struct _Node 0x1300

struct _Node 0x1000

next next next next NULL NULL

prev prev prev prev

Node* head

Input

Output

N1 N2 N3 N4

N3 N4 N1 N2

3

2.1.1 Hints and Notes

• You should mix nodes from stack (local variables), heap (dynamic variables) and global variables to generate a list comprising of nodes of varying addresses.

• Although you are free to use any sorting algorithm, it might be easier to use selection sort. In simple words, selection sort is where you iterate through the list to select the largest element and move it to the beginning of the list. (https://en.wikipedia. org/wiki/Selection_sort).

• You are guaranteed that your function will be tested against lists that contain at least 3 elements. So, you are not required to handle lists of smaller size.

• You are not allowed to make copies of the nodes. You are required to sort them by altering the next and prev pointers.

2.2 Estimator

(60 points) You are to write a program to estimate the throughput of nop instructions. That is, you are to ﬁnd the number of nop instructions that the processor can execute in 1 second. Your program must be called estimator ands its implementation is in estimator.c. It should print a single number in scientiﬁc notation indicating the number of nop instructions the processor can execute in 1 second with a trailing newline and nothing else. This number of instructions should reﬂect the average of at least 5 trials.

2.2.1 Hints and Notes

Using the timer code given in the Labs, run an assembly function comprising of 100 nop instruction a large number of times (e.g., (unsigned int) 0xffffffff), and run another assembly function that contains 0 nop instructions (only a ret instruction) the same number of times. In both cases, measure the time consumed over at least 5 trials. Compute the average number of nop instructions the processor executes per second. You can expect at least 2 x 109 nop’s per second.

4

2.3 Toggle

(60 points) You are to implement the following function in 64 bit assembly in ﬁle toggle.S, with the function prototype in toggle.h.

unsigned long toggle_bit(unsigned long num, unsigned long bit_num);

The function accepts an unsigned long num and toggles (A 0 bit changes to 1 and a 1 bit changes to 0) the bit at bit num position (0≤bit num≤64, where 0 is the least signiﬁcant bit and 63 is the most signiﬁcant bit).

Sale!