forked from uva-cs/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exam1-s08.cpp
111 lines (97 loc) · 3.24 KB
/
exam1-s08.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// CS 216 exam 1 code
#include <iostream>
#include <stdio.h>
using namespace std;
/* This function will print out the results of the memory diagram
* question (question 13). The lines that are commented out in the
* function are the ones that cause errors: the first a compile-time
* error, the last two a run-time error.
*/
void memoryDiagramQuestion() {
cout << "Memory diagram question output: " << endl;
int a = 7;
int *b = &a;
int &c = a;
int d[5] = { 1, 2, 3, 4, 5 };
int *e = new int[5];
int *f = NULL;
int &g = *f;
//c = b;
c = *b;
cout << "a: " << a << endl;
cout << "b: " << b << endl;
cout << "*b: " << *b << endl;
cout << "c: " << c << endl;
cout << "d: " << d << endl;
cout << "e: " << e << endl;
cout << "*e: " << *e << endl;
cout << "f: " << f << endl;
//cout << "*f: " << *f << endl;
//cout << "g: " << g << endl;
}
// The array that is declared in question 3
int x[3][2] = { {1,2}, {3,4}, {5,6} };
// The union used for questin 12
union foo {
int x; // used to write the int value
float f; // used to read the floating point value
int *p; // used to print out the values in hex
unsigned char c[4]; // used to swap the bytes
};
/* This function will compute the result of the endian number question
* (question 12). Because the x86 writes both the floating point
* number and the int value both in the same endian format, the bytes
* have to be switched to simulate what was asked in the question.
*/
void endianNumberQuestion() {
// Write the int to the union, and print out the value in hex. This
// is also the value expected for the first part of the 'partial
// credit' option for this question.
foo bar;
bar.x = 2113;
cout << "Decimal value: " << bar.x << endl;
cout << "Hex value before endian swap: " << bar.p << endl;
// Any machine will write both floating point numbers and int
// numbers in the same endian format. To simulate the exam question
// (which has one type written in one endian format, and the other
// one written in the other endian format), the bytes have to be
// switched. Note that it doesn't matter if this is running on a
// big-endian machine or a little-endian machine, as the byte swap
// will just switch the bytes to the 'other' format.
unsigned char g;
g = bar.c[0];
bar.c[0] = bar.c[3];
bar.c[3] = g;
g = bar.c[1];
bar.c[1] = bar.c[2];
bar.c[2] = g;
// Extract the floating point value
cout << "Hex value after endian swap: " << bar.p << endl;
cout << "Floating point value: " << bar.f << endl;
// This is the second part of the 'partial credit' option for this
// question.
bar.x = 0x419c0000;
cout << "0x419c0000 as a float: " << bar.f << endl;
}
// Calls the two defined functions above, and then exits.
int main(int argc, char **argv) {
endianNumberQuestion();
memoryDiagramQuestion();
return 0;
}
/* Output:
Decimal value: 2113
Hex value before endian swap: 0x841
Hex value after endian swap: 0x41080000
Floating point value: 8.5
0x419c0000 as a float: 19.5
Memory diagram question output:
a: 7
b: 0xbfc8e07c
*b: 7
c: 7
d: 0xbfc8e068
e: 0x804a008
*e: 0
f: 0
*/