Midterm Examination Version 1 I. Arithmetic (10 points) Do the necessary binary-decimal-hexadecimal conversion to fill in the missing entries in the following table. binary Decimal hex char 1. 0 1 1 0 0 0 1 0 _______ _ _ �___� 2. _ _ _ _ _ _ _ _ 50 _ _ �___� 3. _ _ _ _ _ _ _ _ _______ 5 0 �___� 4. _ _ _ _ _ _ _ _ _______ _ _ �9� Convert each of the following 32-bit signed hexadecimal int�s to decimal. 5. _______________ 0xFFFFFF00 6. _______________ 0x00000AB3 Convert each of the following decimal numbers into 32-bit signed hexadecimal int�s. (8 hexadecimal digits). 7. 0x _ _ _ _ _ _ _ _ 1025 8. 0x _ _ _ _ _ _ _ _ -1025 9. 0x _ _ _ _ Add 678 (base 16) and ABC (base 16), answer in base 16. 10. Count from 0 (base 3) to 100 (base 3) in base 3. (Hint � there is no 3 in base 3). ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ II. Bytes (10 points) Assume that r, s, and t are defined as follows. What value (IN HEX) would variable r have after each of the following statements was executed. The answer must be in the form of TWO HEXADECIMAL DIGITS. char r, s, t; s = 0x5B; t = 0xB5; 1. r = -s; 0x __ __ 2. r = ~t; 0x __ __ 3. r = !s; 0x __ __ 4. r = 'z'; 0x __ __ 5. r = s<2; 0x __ __ 6. r = s<<2; 0x __ __ 7. r = s-t; 0x __ __ 8. r = s&t; 0x __ __ 9. r = s&&t; 0x __ __ 10. r = s++; 0x __ __ III. Data Declarations (10 points) 1. ____ int var; a. A function taking a pointer to an int that returns void 2. ____ int *var; b. A function taking void and returning a pointer to an int 3. ____ int var[3]; c. A pointer to a function taking void that returns an int 4. ____ int *(var[3]); d. A pointer to an integer 5. ____ int (*var)[3]; e. A pointer to an array of 3 ints 6. ____ int f(void); f. A (long or short) integer 7. ____ int *f(void); g. A function taking void that returns an int 8. ____ int (*p)(void); h. An array of 3 pointers to ints 9. ____ void f(int *); i. An array of 3 ints IV. sizeof(). (10 points) #include main () { short s = 10; short *ps = &s; short svec[8] = {9, 8, 7, 6, 5, 4, 3, 2}; short *psvec = svec; /* 1. __________*/ printf("sizeof(&s) = %d\n", sizeof(&s)); /* 2. __________*/ printf("sizeof(s) = %d\n", sizeof(s)); /* 3. __________*/ printf("sizeof(&ps) = %d\n", sizeof(&ps)); /* 4. __________*/ printf("sizeof(ps) = %d\n", sizeof(ps)); /* 5. __________*/ printf("sizeof(*ps) = %d\n", sizeof(*ps)); /* 6. __________*/ printf("sizeof(svec) = %d\n", sizeof(svec)); /* 7. __________*/ printf("sizeof(psvec) = %d\n", sizeof(psvec)); /* 8. __________*/ printf("sizeof(*(psvec+5)) = %d\n", sizeof(*(psvec+5))); /* 9. __________*/ printf("*(psvec+6) = %d\n", *(psvec+6)); /*10. __________*/ printf("(*psvec)+6 = %d\n", (*psvec)+6); V. What will it print (10 points) /* If memory has the following layout, what will the following program print? */ /* +----------------+ */ /* a 0x0000100c | | actually 0xbf951c9c when I ran it */ /* +----------------+ */ /* b 0x00001008 | | actually 0xbf951c98 when I ran it */ /* +----------------+ */ /* p 0x00001004 | | actually 0xbf951c94 when I ran it */ /* +----------------+ */ /* q 0x00001000 | | actually 0xbf951c90 when I ran it */ /* +----------------+ */ #include int main(void) { int a = 10; int b = 20; int *p = &b; int *q = p; q++; (*p)--; /* 1. __________________*/ printf("a = %i\n", a); /* 2. __________________*/ printf("a = %p\n", &a); /* 3. __________________*/ printf("a = %i\n", b); /* 4. __________________*/ printf("a = %p\n", &b); /* 5. __________________*/ printf("a = %p\n", p); /* 6. __________________*/ printf("a = %p\n", &p); /* 7. __________________*/ printf("a = %i\n", *p); /* 8. __________________*/ printf("a = %p\n", q); /* 9. __________________*/ printf("a = %p\n", &q); /*10. __________________*/ printf("a = %i\n", *q); } VI. What is in %eax? (10 points) What would be in register %eax after each of the following instructions is executed. You may leave the answer in hexadecimal (if you precede the number by "0x") or in decimal. However, if I see a number like 123 (or even 12A), I WILL ASSUME IT IS DECIMAL. Use 0x123 or 0x12A to leave a hexadecimal number. You to not need to provide leading zeros (0x123 instead of 0x00000123). Assume the following values are stored at the indicated memory addresses and registers. Note that %eax is reset to 0x00000100 after each question. address hex value register hex value 0x00000000 0x0000000C %eax 0x00000100 0x00000004 0x00000010 %ebx 0x00000004 0x00000008 0x00000000 %ecx 0x00000008 0x0000000C 0x00000004 %edx 0x0000000C 0x00000010 0x00000008 1. _______________ movl $8,%eax 2. _______________ movl 8,%eax 3. _______________ movl (%ebx),%eax 4. _______________ movl 8(%ebx),%eax 5. _______________ leal 8,%eax 6. _______________ leal (%eax),%eax 7. _______________ leal 8(%eax),%eax 8. _______________ leal (%ebx,%ecx),%eax 9. _______________ orl $0xff,%eax 10. _______________ orl $-16,%eax VII. Disassemble. (10 points) The following assembly language output was produced with the command "gcc -S seven.c". Write function seven.c (in C of course) The function prototype is �int seven(int a, int b);�. (My code is 7 lines long.) ___________________________________________________________________ ___________________________________________________________________ ... .globl seven .type seven, @function seven: pushl %ebp movl %esp, %ebp subl $16, %esp movl 12(%ebp), %eax b movl 8(%ebp), %edx a leal (%edx,%eax), %eax movl %eax, -12(%ebp) x movl 12(%ebp), %eax b movl 8(%ebp), %edx a movl %edx, %ecx subl %eax, %ecx movl %ecx, %eax movl %eax, -8(%ebp) y movl 12(%ebp), %eax b movl 8(%ebp), %edx a andl %edx, %eax movl %eax, -4(%ebp) z movl -4(%ebp), %eax leave ret .size seven, .-seven .ident "GCC: (GNU) 4.4.5 20101112 (Red Hat 4.4.5-2)" .section .note.GNU-stack,"",@progbits VIII. Disassemble. (10 points) The following assembly language output was produced with the command "gcc -S eight.c". Write function eight.c (in C of course) The function prototype is �int eight(int a, int b[])�. My code was 7 lines long. (The space for your answer is on the right of the assembly listing to keep the question on a single page .globl eight .type eight, @function ___________________________________________ eight: pushl %ebp ___________________________________________ movl %esp, %ebp subl $16, %esp ___________________________________________ movl 12(%ebp), %eax b movl (%eax), %eax ___________________________________________ movl %eax, -8(%ebp) x movl $1, -4(%ebp) y ___________________________________________ jmp .L4 .L6: ___________________________________________ movl -4(%ebp), %eax y sall $2, %eax ___________________________________________ addl 12(%ebp), %eax b movl (%eax), %eax ___________________________________________ cmpl -8(%ebp), %eax x jge .L5 ___________________________________________ movl -4(%ebp), %eax y sall $2, %eax ___________________________________________ addl 12(%ebp), %eax b movl (%eax), %eax ___________________________________________ movl %eax, -8(%ebp) x .L5: ___________________________________________ addl $1, -4(%ebp) y .L4: movl -4(%ebp), %eax y cmpl 8(%ebp), %eax a jl .L6 movl -8(%ebp), %eax x leave ret .size eight, .-eight .ident "GCC: (GNU) 4.4.5 20101112 (Red Hat 4.4.5- IX. Kernighan and Ritchie squeeze.c (10 points) Write the functions squeeze(s,c) which removes all occurrences of the character c from string s. With the addition of your function, the program below should output "Letsremovetheblanksfromthisstring" (without the quotes). (The K&R solution is 8 lines long). #include void squeeze(char s[], int c); main() { char string[] = "Lets remove the blanks from this string"; squeeze(string, ' '); printf("%s\n", string); } for (i = j = 0; s[i] != '/0'; i++) if (s[i] != c) s[j++] = s[i]; s[j] = '\0'; ___________________________________________________________________ ___________________________________________________________________ ... X. Kernighan and Ritchie atoi.c (10 points) Write the function atoi which converts string s to an integer. With the addition of you function, the program below should output "12345" (without the quotes). (The K&R solution is 8 lines long.) #include int atoi(char s[]); main() { int i; char string[] = "12345"; i = atoi(string); printf("%d\n", i); } for (i = 0; s[i] >= '0' && s[i] <= '9'; i++) n = 10 * n + (s[i] - '0'); cis-lclient19:~/test2>more sizeof.c /*----------------------------------------------*/ #include int funct(char []); main() { char string[10]; printf("%d\n", funct(string)); } int funct(char string[]) { return sizeof(string); } /*----------------------------------------------*/ cis-lclient19:~/test2>gcc sizeof.c cis-lclient19:~/test2>./a.out 4 cis-lclient19:~/test2>