Uploaded by Michael Philip

Midterm 1 Review - Priya

advertisement
Midterm 1 review
CS 211
Fall 2019
1
List of topics
• von Neumann architecture
• Architecture overview
• C programming
– pointers
– arrays
– structures
– malloc and free
– stack and heap
– memory leaks
– scanf and printf
– fopen and fclose
– stack frames
– pass-by-value and pointers
– bitwise operations
• number systems
– binary
– decimal
– octal
– hexadecimal
• number representations
– negative integers
∗ signed-magnitude
∗ one’s complement
∗ two’s complement
– fractions
– floating point
2
Review questions
2.1
Architecture overview
1. In the von-Neumann model, what two things can we store in memory? Data, Instructions
2. Name two components inside the CPU.
ALU --> Arithmetic-Logic Unit, PC --> Program Counter
3. What is a bus? Transfers data between components
4. In what direction does the stack grow? Grows toward lower addresses
5. What is the stack used for? Stores activation records (stack frames), local variables, function arguments
6. What is the heap used for? Dynamic memory allocation using malloc
7. What is an activation record? Pushed onto stack for each function call
8. Name two things contained in an activation record.
Return address, function args, old ebp, callee-saved registers, local variables
2.2
C programming
9. What will the following code do?
Crash bc ptr is not initialized. Also, ptr not modified bc ptr is local, not global.
#include <s t d i o . h>
#include < s t d l i b . h>
void getMem ( i n t ∗ p )
{
p = malloc (5 ∗ sizeof ( int ) ) ;
}
i n t main ( i n t arg c , char∗ a r g v [ ] )
{
int ∗ ptr ;
getMem ( p t r ) ;
f o r ( i n t i = 0 ; i < 5 ; i ++) {
ptr [ i ] = i ∗ i ;
p r i n t f ( ”%d\n” , p t r [ i ] ) ;
}
f r e e ( ptr ) ;
}
(hint: if your answer starts with “It prints ...”, try running it.)
10. Write a C statement to extract bits 13-17 (as a 5-bit number) from a 32-bit integer x.
int result = (x >> 12) & 0x1f
11. Write a C program that converts a string of bits like “1011” to the corresponding decimal
number.
Need pointers because this only edits this function's local copies of x
12. Explain why void swap(int x, int y) won’t work. and y, not the original inputs
13. When should you call free()? Once you no longer need the data associated with a dynamically allocated pointer that used malloc.
14. Is C considered a strongly typed language? Why or why not? Not really bc you can type cast to work around the
protection of the type system
2
15. In the following program fragment, where in memory are x and p allocated?
x is in the stack. p is also in the stack, but it points to memory in the heap
void f ( )
{
int x = 1 0 ;
int ∗ p = malloc (100 ∗ sizeof ( int ) ) ;
}
(hint: for p, it’s not what you might first think)
2.3
Number representations
16. What are signed magnitude, one’s complement, and two’s complement used to represent?
Signed integers.
17. Which representation do modern systems use?
Two's complement.
18. What is one advantage of one’s complement over signed magnitude representation?
For the most part, addition can be done like normal binary addition.
19. What is one disadvantage of one’s complement?
Has two zeroes (positive and negative).
20. What are the three parts of a floating point number in IEEE754 representation?
Sign, exponent, mantissa
21. In an 8-bit universe, the binary representation of 42 is 00101010. What is the binary representation of -42 in two’s complement? 11010101 --> 11010110
22. What is 212 ? 4096
23. Convert 127 to binary. (hint: there’s a way to do this very quickly) 01111111
24. Convert 47 to binary. 101111
25. Convert 10110110 to decimal. 128+32+16+4+2 = 182
26. Convert 72 to hexadecimal. 1001000 --> 48
27. Convert 0x2c to decimal. 12*16^0 + 2*16^1 = 12+32 = 44
28. Convert 0x7a6b to binary. 0111 1010 0110 1011
29. Convert 55 to octal. 110111 --> 67
30. Convert 045 (octal) to decimal. 5+32 = 37
31. Convert 34.3125 to binary (not floating point). 100010.0101
32. Show how the binary number 101010.1101101 would be represented in IEEE754 floating point.
0 10000100 01010110110100000000000
33. Given the following floating point number in binary, calculate the corresponding decimal value.
11000001111011000000000000000000
sign = - exp = 131-127 = 4
-11101.1 = -29.5
3
Download