Problem 1. Each of the following assembly code snippets corresponds to the body of a C function consisting of a single control structure: a loop, if statement, or case switch. (Compiler directives, like cfi_startproc, and instructions that do not affect the semantics, like endbr64, have been removed for readability. And yes, of course the functions are all named foo.) For each one, write C code that would compile to similar assembly for the body. In other words, figure out what the code is doing and write C code to do that. You will have to figure out how many variables are being computed with, and make up names for them. (The code was compiled with -Og.)

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter10: Pointers
Section10.1: Addresses And Pointers
Problem 7E
icon
Related questions
Question
Problem 1. Each of the following assembly code snippets corresponds to the body of a C function
consisting of a single control structure: a loop, if statement, or case switch. (Compiler directives,
like cfi_startproc, and instructions that do not affect the semantics, like endbr64, have been
removed for readability. And yes, of course the functions are all named foo.) For each one, write
C code that would compile to similar assembly for the body. In other words, figure out what the
code is doing and write C code to do that. You will have to figure out how many variables are being
computed with, and make up names for them. (The code was compiled with -0g.)
foo:
a.
b.
C.
.L3:
.L2:
foo:
.L2:
foo:
.L3:
.L2:
movl $0, %edx
movl $0, %eax
jmp .L2
addl
(%rdi), %edx
addl
$1, %eax
leaq 4(%rdi), %rdi
cmp1 %esi, %eax
jl
.L3
movl
%edx, %eax
ret
movl (%rdi), %eax
cmpl $10, %eax
jle .L2
subl $10, %eax
movl %eax, (%rdi)
ret
leal 1(%rax,%rax, 2), %eax
movl %eax, (%rdi)
ret
movl $0, %eax
jmp .L2
addl $1, %eax
cmpb $0, (%rdi)
jne .L3
ret
Transcribed Image Text:Problem 1. Each of the following assembly code snippets corresponds to the body of a C function consisting of a single control structure: a loop, if statement, or case switch. (Compiler directives, like cfi_startproc, and instructions that do not affect the semantics, like endbr64, have been removed for readability. And yes, of course the functions are all named foo.) For each one, write C code that would compile to similar assembly for the body. In other words, figure out what the code is doing and write C code to do that. You will have to figure out how many variables are being computed with, and make up names for them. (The code was compiled with -0g.) foo: a. b. C. .L3: .L2: foo: .L2: foo: .L3: .L2: movl $0, %edx movl $0, %eax jmp .L2 addl (%rdi), %edx addl $1, %eax leaq 4(%rdi), %rdi cmp1 %esi, %eax jl .L3 movl %edx, %eax ret movl (%rdi), %eax cmpl $10, %eax jle .L2 subl $10, %eax movl %eax, (%rdi) ret leal 1(%rax,%rax, 2), %eax movl %eax, (%rdi) ret movl $0, %eax jmp .L2 addl $1, %eax cmpb $0, (%rdi) jne .L3 ret
d. (Note: this one has nested control structures. Hint: one is a conditional.)
foo:
.L3:
.L2:
.L5:
movl $0, %ecx
movl $0, %eax
jmp .L2
sarl %edi
sarl %esi
addl $1, %eax
cmp1 $31, %eax
jg .L5
movl %edi, %edx
xorl %esi, %edx
testb $1, %d1
jne
.L3
addl $1, %ecx
jmp
.L3
movl %ecx, %eax
ret
3
Transcribed Image Text:d. (Note: this one has nested control structures. Hint: one is a conditional.) foo: .L3: .L2: .L5: movl $0, %ecx movl $0, %eax jmp .L2 sarl %edi sarl %esi addl $1, %eax cmp1 $31, %eax jg .L5 movl %edi, %edx xorl %esi, %edx testb $1, %d1 jne .L3 addl $1, %ecx jmp .L3 movl %ecx, %eax ret 3
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
ADT and Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr