224-15b-final-exam-examples-1

txt

School

Harvard University *

*We aren’t endorsed by this school

Course

224

Subject

Computer Science

Date

Jan 9, 2024

Type

txt

Pages

3

Uploaded by CountHeron146

Report
CS 224 Final Exam Example Problems Consider the following C code and corresponding X86 assembly code. void bob() { int x = 0x11111111; short y = 0x2222; char z = 0x88; char buffer[2]; gets(buffer); printf("%x %x %s\n", y, z, buffer); scanf("%x %hx %hhx\n", &x, &y, &z); int i = 0; while (scanf("%x", &x) != EOF) buffer[i++] = (char)x; } bob: pushq %rbp movq %rsp, %rbp subq $16, %rsp movl $286331153, -8(%rbp) movw $8738, -10(%rbp) movb $-120, -11(%rbp) leaq -13(%rbp), %rax movq %rax, %rdi movl $0, %eax call gets@PLT movzbl -11(%rbp), %eax movsbl %al, %edx movzwl -10(%rbp), %eax cwtl leaq -13(%rbp), %rcx movl %eax, %esi leaq .LC0(%rip), %rdi movl $0, %eax call printf@PLT leaq -11(%rbp), %rcx leaq -10(%rbp), %rdx leaq -8(%rbp), %rax movq %rax, %rsi leaq .LC1(%rip), %rdi movl $0, %eax call __isoc99_scanf@PLT movl $0, -4(%rbp) jmp .L4 .L5: movl -8(%rbp), %ecx movl -4(%rbp), %eax leal 1(%rax), %edx movl %edx, -4(%rbp) movl %ecx, %edx cltq movb %dl, -13(%rbp,%rax) .L4: leaq -8(%rbp), %rax
movq %rax, %rsi leaq .LC2(%rip), %rdi movl $0, %eax call __isoc99_scanf@PLT cmpl $-1, %eax jne .L5 nop nop leave ret Problem 1 After the 'subq' instruction function in 'bob' completes, the stack contains a number of items listed below. Determine where on the stack each item is located. Give the byte offset of each item relative to %rsp. Express these as decimal offsets (positive or negative) relative to register %rsp: return address saved %rbp i x y z buffer Problem 2 Consider the case when 'bob' is called and we type 'ABCDEFGH' in response to gets. For each item in the following list, indicate which are/are not corrupted by the response from gets, i.e., their values were altered by some action within the call to gets. return address saved %rbp i x y z buffer What will the printf function print for the following: y z buffer Problem 3 You will now design an input string that,
when given as input to the scanf in the while loop, will set variable 'y' to the value 0x4444. Your input will consist of typed characters, which will be interpreted as hex integers. Your hex ints will be separated from each other by spaces, and each in turn will be scanned by scanf and assigned to x. How many hex ints are in your input string? How many hex ints in your input string could be anything? (We will call these filler hex ints.) How many hex ints in your input string must be precisely correct? (We will call these precise hex ints.) Give the precise hex ints exactly as they would be included in your input string.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help