q1_fall03_sol

q1_fall03_sol - Department of Electrical Engineering and...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY 6.828 Operating System Engineering: Fall 2003 Quiz I Solutions All problems are open-ended questions. In order to receive credit you must answer the question as precisely as possible. You have 80 minutes to answer this quiz. Write your name on this cover sheet AND at the bottom of each page of this booklet. Some questions may be much harder than others. Read them all through first and attack them in the order that allows you to make the most progress. If you find a question ambiguous, be sure to write down any assumptions you make. Be neat. If we can’t understand your answer, we can’t give you credit! THIS IS AN OPEN BOOK, OPEN NOTES QUIZ. 1 (xx/19) 2 (xx/15) 3 (xx/25) 4 (xx/20) 5 (xx/15) 6 (xx/6) Total (xx/100) Name: I 6.828 FALL 2003, Quiz 1 Solutions Page 2 of 16 Calling conventions In lab 2 you extended printf . Here is the core of printf relevant for this question: void printk(const char *fmt, va_list ap) { register char *p, *q; register int ch, n; u_quad_t uq; int base, lflag, qflag, tmp, width; char padc; for (;;) { padc = ’ ’; width = 0; while ((ch = *(u_char *) fmt++) != ’%’) { if (ch == ’\0’) return; cons_putc(ch); } lflag = 0; qflag = 0; reswitch: switch (ch = *(u_char *) fmt++) { case ’d’: uq = getint(&ap, lflag, qflag); if ((quad_t) uq < 0) { cons_putc(’-’); uq =-(quad_t) uq; } base = 10; goto number; [... other cases omitted ... not relevant to the question] number: p = ksprintn(uq, base, &tmp); if (width && (width-= tmp) > 0) while (width--) cons_putc(padc); while ((ch = *p--) != ’\0’) cons_putc(ch); break; } } } Name: 6.828 FALL 2003, Quiz 1 Solutions Page 3 of 16 static u_quad_t getint(va_list *ap, int lflag, int qflag) { if (lflag) return va_arg(*ap, u_long); else if (qflag) return va_arg(*ap, u_quad_t); else return va_arg(*ap, u_int); } int printf(const char *fmt,...) { va_list ap; va_start(ap, fmt); kprintf(fmt, ap); va_end(ap); return 0; } 1. [10 points]: What values does gcc on the x86 push on the stack for the call: printf(‘‘the class number is %s and used to be %d \n’’, ‘‘6828’’, 6097) 1. The number 6097, as a 4-byte integer. 2. The address of the (null-terminated) string “ 6828 ”, as a 4-byte pointer. 3. The address of the string “ the class number ... ”, as a 4-byte pointer. Name: 6.828 FALL 2003, Quiz 1 Solutions Page 4 of 16 2. [4 points]: gcc pushes the arguments in a particular order. What is the order and why? gcc pushes arguments in reverse order, last argument first. Because the stack grows down on the x86 (and PDP-11), this means that the first argument (last one pushed) will have the lowest address in memory, just above the function’s return address. This way, the called function can find its first argument without knowing exactly how many and what types of other arguments it was called with. Knowing the location and type of the first argument, the function can then...
View Full Document

This note was uploaded on 05/29/2010 for the course 6 828 taught by Professor Guy during the Spring '08 term at MIT.

Page1 / 16

q1_fall03_sol - Department of Electrical Engineering and...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online