Uploaded by Ka-Yen Yau

2022SP-HW1

advertisement
COMP_SCI 213, Spring 2022
Homework 1
This is a team assignment. All handins are electronic through Canvas. Each team should submit
through their group account; multiple submissions are allowed, only the last will be graded.
Group Member 1 Name: _______________________________________________
Group Member 1 NetID: _______________________________________________
Group Member 2 Name: _______________________________________________
Group Member 2 NetID: _______________________________________________
Reminder: Group Sizes other than 2 require the written consent of the instructor.
Problem 1. (12 points ):
Assume the C code below on a unix system where sizeof(char)=1, sizeof(int)=4 and
sizeof(long)=8.
char c[10] = {'f', 'e', 'd', 'c', 'b', 'a', '3', '2', '1', '0' };
long x[2] = {0xABCDEF123456L, 0x201904C50213L};
Assume that when your program runs array c is allocated in the memory starting from address 0x100 and
array x is allocated in the memory starting from address 0x200. Remember that in C, consecutive array
elements (i.e., at increasing array index) are stored in consecutive memory addresses (e.g., for array x
above, the first 8 bytes in the memory where the array is located will be occupied by x[0], the immediately
following 8 bytes in memory will be occupied by x[1], etc.). Also, remember that the suffix L in a constant
declares this constant as a signed long.
Write the contents of the memory in each address, for both Big-Endian and Little-Endian memory ordering.
Write hexadecimal values only. Fill in all fields, putting “?” whenever the contents of a cell cannot be
known for certain. The numerical encoding of characters is shown in the table below (i.e., to store the
character ‘c’ the system stores the value 0x63 in the corresponding location.)
Character
a
b
c
d
e
f
0
1
2
3
Hex Value
0x61
0x62
0x63
0x64
0x65
0x66
0x30
0x31
0x32
0x33
LITTLE ENDIAN:
Mem
Addr
0x
100
0x
101
0x
102
0x
103
0x
104
0x
105
0x
106
0x
107
0x
108
0x
109
0x
0x
0x
0x
0x
10A 10B 10C 10D 10E
0x
10F
0x
200
0x
201
0x
202
0x
203
0x
204
0x
205
0x
206
0x
207
0x
208
0x
209
0x
0x
0x
0x
0x
20A 20B 20C 20D 20E
0x
20F
0x
100
0x
101
0x
102
0x
103
0x
104
0x
105
0x
106
0x
107
0x
108
0x
109
0x
0x
0x
0x
0x
10A 10B 10C 10D 10E
0x
10F
0x
200
0x
201
0x
202
0x
203
0x
204
0x
205
0x
206
0x
207
0x
208
0x
209
0x
0x
0x
0x
0x
20A 20B 20C 20D 20E
0x
20F
Content
Mem
Addr
Content
BIG ENDIAN:
Mem
Addr
Content
Mem
Addr
Content
Problem 2. (6 points ):
Assume the memory layout and contents as described in Problem 1. Now assume you are running a
program consisting of only the following line in main():
printf("%x\n", &x[0]);
Remember that the “%x” modifier of printf means “print the value in hexadecimal format”. When you
run the program, you see 0x200 printed on the screen.
Assume you are running on a Little Endian machine. Write the corresponding hexadecimal values for
these expressions:
(int) x[0]
______________________
(int *)(& x[0])
______________________
*(int *)(& x[0])
______________________
Assume you are running on a Big Endian machine. Write the corresponding hexadecimal values for these
expressions:
(int) x[0]
______________________
(int *)(& x[0])
______________________
*(int *)(& x[0]) ____________________
Problem 3. (12 points):
Consider an 8-bit signed integer representation in two’s complement. Fill in the empty boxes in the
following table. Addition and subtraction should be performed based on the rules for 8-bit signed arithmetic
in two’s complement representation, ignoring undefined behavior.
Description
Decimal
Representation
Binary Representation
Hexadecimal
Representation
Zero
Just a number
Just a number
Just a number
TMax
TMin
TMin-1
TMax+1
-TMax
42
1010 1100
0xFF
Problem 4. (14 points):
Assume we are running code on an Intel x86-64 Linux machine. Consider the definitions:
int x = a_random_int_number;
int y = another_random_int_number;
unsigned ux = (unsigned) x;
unsigned uy = (unsigned) y;
Fill in the empty boxes in the table below. For each of the C expressions in the first column, either:
• State that it is TRUE for all possible argument values, or
• State that it is FALSE and give an example where it is false.
Note 1: “FALSE” without a counter-example does not constitute a correct answer.
Note 2: Remember that the right shift operation “>>” is typically arithmetic on signed data and logical on
unsigned data. Assume this is the case also here.
Note 3: For all problems below assume that the compiler emitted the corresponding arithmetic instructions
to the hardware, ignoring any undefined behavior.
Puzzle
x != TMax à ((x << 1) >> 1) == x
((x >> 1) << 1) <= x
(x >= 0) || (x < ux)
-y = ˜y - (TMax + TMin)
-y != y
(x < y) == (-x > -y)
ux - uy == x - y
True or (False + Counter-Example)
Download