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.)
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
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](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F2b2b0246-74c5-427f-bbae-6a253749f017%2Fd58528d5-e29d-4ed0-b8f0-cd545548a6cf%2Ff5ez9b_processed.png&w=3840&q=75)
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](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F2b2b0246-74c5-427f-bbae-6a253749f017%2Fd58528d5-e29d-4ed0-b8f0-cd545548a6cf%2Fgsfkth_processed.png&w=3840&q=75)
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
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 3 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Knowledge Booster
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.Recommended textbooks for you
![C++ for Engineers and Scientists](https://www.bartleby.com/isbn_cover_images/9781133187844/9781133187844_smallCoverImage.gif)
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
![C++ for Engineers and Scientists](https://www.bartleby.com/isbn_cover_images/9781133187844/9781133187844_smallCoverImage.gif)
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr