关键词 > IntroductiontoComputerSystems

Homework #2: Introduction to Computer Systems - 18x13: Summer 2024

发布时间:2024-06-14

Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Homework #2: Introduction to Computer Systems - 18x13: Summer 2024

Question 1 1 / 1 pts

Consider the following assembly:

addl    %eax, %edx          # %eax here is an "int x" and %edx here is an "int y"

movl    %edx, %eax

sall    $2, %eax

addl    %edx, %eax

sall    $3, %eax

Which of the following expressions matches the assembly?

32x + 32y

Correct!

40(x + y)

8x + 4y

4x + 8y

None of the above

Question 2 3 / 3 pts

Please consider the following assembly:

leaq    12(%rbx), %rax     # Assume that %rbx is the base of an "int array[ 10 ]"

movl    $6, (%rax)

leaq    16(%rbx), %rax

movl    $8, (%rax)

leaq    12(%rbx), %rax

movl    (%rax), %edx

leaq    16(%rbx), %rax

(a) Into what element of the array is the value 6 being moved? Specify the index. 3

(b) Into what element of the array is the value 8 being moved? Specify the index. 4

(c) What element is represented by 16(%rbx)? Specify the index. 4

Question 3 6 / 6 pts

Loops and Conditionals (6 points)

Consider the following code, under the assumption that it was compiled from C code in the same environment using the same "shark machine" toolset you’ve used all semester:

loop:

.LFB0:

pushq   %rbp

movq    %rsp, %rbp

pushq   %rbx

movl    %edi, -12(%rbp)

movl    %esi, -16(%rbp)

movl    %edx, -20(%rbp)

movl    $0, %ebx

movl    -12(%rbp), %eax

cmpl    -20(%rbp), %eax

jl      .L2

movl    -20(%rbp), %eax

cmpl    -16(%rbp), %eax

jg      .L2

movl    %ebx, %eax

jmp     .L3

.L2:

jmp     .L4

.L5:

addl    $1, %ebx

addl    $1, -12(%rbp)

.L4:

movl    -12(%rbp), %eax

cmpl    -20(%rbp), %eax

jl      .L5

jmp     .L6

.L7:

addl    $1, %ebx

subl    $1, -20(%rbp)

.L6:

movl    -20(%rbp), %eax

cmpl    -16(%rbp), %eax

jg      .L7

movl    %ebx, %eax

.L3:

popq    %rbx

popq    %rbp

ret

(A) (2 points): How many loops does this function have? 2

(B) (2 points): How many “if” statements does this function have? 2

Hint: Control flow from loops is not counted as "if" statements.

(C) (2 points) Consider calling the function as below. What value does the function return?

6

Hint: It returns an integer type. Enter nothing but an integer number.

loop(3, 5, 7, 9, 11, 13);

Switch Statements (8 points)

Consider the following code, which was compiled from C Programming Language source code containing one switch statement and no (zero) if statements:

(gdb) disassemble foo
Dump of assembler code for function foo:
0x000000000040052d <+0>:     cmp    $0x5,%esi
0x0000000000400530 <+3>:     ja     0x40055d
0x0000000000400532 <+5>:     mov    %esi,%eax
0x0000000000400534 <+7>:     jmpq   *0x400620(,%rax,8)
0x000000000040053b <+14>:    mov    %edi,%eax
0x000000000040053d <+16>:    and    $0x1,%eax
0x0000000000400540 <+19>:    retq
0x0000000000400541 <+20>:    lea    0x0(,%rdi,4),%eax
0x0000000000400548 <+27>:    retq
0x0000000000400549 <+28>:    add    $0x2,%edi
0x000000000040054c <+31>:    mov    $0x55555556,%edx
0x0000000000400551 <+36>:    mov    %edi,%eax
0x0000000000400553 <+38>:    imul   %edx
0x0000000000400555 <+40>:    sar    $0x1f,%edi
0x0000000000400558 <+43>:    mov    %edx,%eax
0x000000000040055a <+45>:    sub    %edi,%eax
0x000000000040055c <+47>:    retq
0x000000000040055d <+48>:    lea    (%rdi,%rsi,1),%eax
0x0000000000400560 <+51>:    retq
End of assembler dump.

Consider also the following dump:

(gdb) x/16gx 0x400610
0x400610        0x0000000000020001      0x0000000000000000
0x400620:       0x000000000040053b      0x000000000040055d
0x400630:       0x0000000000400541      0x0000000000400549
0x400640:       0x000000000040055d      0x000000000040054c
0x400650:       0x0000003c3b031b01      0xfffffdb000000006
0x400660:       0xfffffdf000000088      0xfffffedd00000058
0x400670:       0xffffff11000000b0      0xffffff40000000c8
0x400680:       0xffffffb0000000e8      Cannot access memory at address 0x400688

Question 4 2 / 2 pts

Switch Statements (8 points)

(A)(2 points): What integer input values are managed by non-default cases of the switch statement?

Check all that apply.

Question 5 2 / 2 pts

Switch Statements (8 points)

(B)(2 points): If there is a default case, at what address, in hex, does the begin? If there isn't a default case, write NONE: 0x40055d

Question 6 2 / 2 pts

Switch Statements (8 points)

(C) (2 points): Which integer input values (if any), managed by non-default cases of the switch statement, consist of exactly the same code as least one other case? Check all that apply. [exact_same]

Question 7 2 / 2 pts

Switch Statements (8 points)

(D) (2 points): Which case(s), if any, fall through to the next case after executing some of their own code? [falls_through]