CS 208 s21 — Learning Block #12

Table of Contents

1 Activity

What affect does this assembly program have on registers and memory given the initial values below?1

f:
        movl    $1, (%rdi)
        movl    $1, 4(%rdi)
        movl    $2, %edx
        jmp     .L2
.L3:
        movslq  %edx, %rax
        salq    $2, %rax
        movl    -8(%rdi,%rax), %ecx
        addl    -4(%rdi,%rax), %ecx
        movl    %ecx, (%rdi,%rax)
        addl    $1, %edx
.L2:
        cmpl    %esi, %edx
        jl      .L3
        rep ret
main:
        subq    $32, %rsp
        movl    $7, %esi
        movq    %rsp, %rdi
        call    f
        movl    $0, %eax
        addq    $32, %rsp
        ret

lb12-activity-diagram-start.png

2 Exercise

Given the C code and the register to variable mapping below, see how far you can get filling in the corresponding assembly:2

for (long i = 0; i < size; i++) {
    total += arr[i];
}
Register Use
%rdi arr
%rsi size
%rdx i
%rax total
init:
        ________________
        ________________
body:
        ________________
        ________________
test:
        ________________
        ________________

3 Practice

CSPP practice problems 3.36 (p. 256) and 3.37 (p. 258)

Footnotes:

1

Here's video walkthrough. The panopto video is below (view it in panopto here), along with the initial memory/register diagram, the C code and assembly side-by-side in godbolt, and the final memory/register diagram.






2
init:
        movl $0, %edx
        jmp  test
body:
        addl (%rdi, %rdx, 4), %eax
        addq $1, %rdx
test:
        cmpq %rsi, %rdx
        jl   body