Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

test2-v1.fm CS350 Spring XXXXXXXXXXTest 2, Thur, 4/9/2020 CS350 Spring XXXXXXXXXXTest 2, Thur, 4/9/2020 1. (5 points) Complete the C function below. Convert shift to a number. Do not use >> or : 2...

1 answer below »
test2-v1.fm
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/9/2020
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/9/2020
1. (5 points) Complete the C function below. Convert shift to a number. Do not use
or
:
unknown:
movq %rsi, %rax
sarq $1, %rax
addq %rdi, %rax
ret
long unknown(long x, long y) {
return ______________________________________;
}
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/9/2020
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/9/2020
2. (10 points) Assume the following values are stored at the indicated memory addresses and registers,
fill in the following table showing the values for the indicated operands in hexadecimal:
Address Value Register Value Instruction Destination Value
0x1000 0xAA %rdi 0x1000 addq (%rdi),%rsi
0x1004 0xBB %rsi 0x1 andq %rsi,%rdi
0x1008 0xCC %rdx 0x2 subq %rsi,(%rdi)
0x100C 0xDD -- XXXXXXXXXXincq %rsi
---------- --------- -- XXXXXXXXXXleaq 4(%rdi,%rdx,2),%rsi
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/9/2020
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/9/2020
3. (10 points) Using the jump to middle logic used for a while loop, fill in the C code for the assembly
elow. Again use the reverse logic discussed in class.
(a)C code
long loop_while(long x, long y){
long out = _________________;
while (________________) {
out = _____________________;
y = _____________________________;
}
return out;
}
loop_while:
movl $1, %eax
jmp .L2
.L3:
movq %rdi, %rdx
subq %rsi, %rdx
addq %rdx, %rax
addq $1, %rsi
.L2:
cmpq %rdi, %rsi
jl .L3
rep ret
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/9/2020
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/9/2020
4. (10 points) For a C function switcher below and the assembly code along with a jump table, fill in the
missing parts of the C code.
(a)C code
long switche
(long x, long y, long z){
long w = 1;
switch(x) {
case 1:
________________________

eak;
case 2:
________________________
case 3:
________________________

eak;
case 4:
case 6:
________________________

eak;
default:
________________________
}
return w;
}
(b) Assembly Code
switcher:
.L3:
leaq(%rsi,%rdx), %rax
ret
.L5:
movq %rdi, %rax
subq %rsi, %rax
ret
.L6:
movq %rsi, %rax
imulq %rdx, %rax
ret
.L7:
movl $2, %eax
ret
(c)Jump Table
.L4:
.quad .L7
.quad .L3
.quad .L5
.quad .L5
.quad .L6
.quad .L7
.quad .L6
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/9/2020
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/9/2020
5. (10 points) Given the a
ay declarations, fill in the following table describing the element size, the total
size, and the address of element i for each of these a
ays.
A
ay declaration Element size
Total
size
Base
address Element i
(a) char a[3]; base_a
(b) char *b[4]; base_
(c) short c[5]; XXXXXXXXXXbase_c
(d) short **d[6]; base_d
(e) int e[5]; base_e
(f) long *f[4]; base_f
(g) double *g[3]; base_g
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/9/2020
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/9/2020
6. (10 points) Fill the table with 0's and 1's for four flags: CF (ca
y flag), SF (sign flag):
Number Type %rdi/edi/di %rsi/esi/si Instruction CF SF ZF OF
(a) Unsigned 0xFFFE 0x4 addw %di, %si
(b) Signed two's
complement
0xFFFD 0x3 addw %di, %si
(c) Signed two's
complement
0xFFFFFFFF 0x XXXXXXXXXXaddl %edi, %esi
(d) Signed two's
complement
-0xFFFF 0xFFFF subl %si, %di
(e) Unsigned 0xFF 0xF shlq 64, %rdi
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/9/2020
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/9/2020
7. (15 points) Suppose basex, the base address of short integer a
ay X and long integer index j are stored
in registers %rax and %
x, respectively. For each of the following expressions, give its type, a for-
mula for its value and an asembly codd implementation. The result shoud be stored in register %rcx if
it is a pointer and register element %cx if it has data type short. For address computation, use
lea offset(base,index,scale factor).
Expression Type Value Assembly code
(a) X[2]
(b) X+3
(c) &X[j]
(d) X[4*j+1]
(e) X+j-4
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/9/2020
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/9/2020
8. (30 points) Consider the C as well as disassembled code below:
int main(int argc,char **argv)
{
long x=3,y=9,z=0;
z = top(x,y);
return z;
}
long top(long x,long y) {
x = x + y;
return leaf(x,y);
}
long leaf(long y,long z) {
z = y - z;
return z;
}
1 00000000004004d6 :
2 4004d6:48 89 f XXXXXXXXXXmov %rdi,%rax
3 4004d9:48 29 f XXXXXXXXXXsub %rsi,%rax
4 4004dc:c XXXXXXXXXXretq
5 00000000004004dd :
6 4004dd:48 01 f XXXXXXXXXXadd %rsi,%rdi
7 4004e0:b XXXXXXXXXXmov $0x0,%eax
8 4004e5:e8 ec ff ff ff callq 4004d6
9 4004ea:f3 c XXXXXXXXXXrepz retq
10 00000000004004ec
:
11 4004ec:be XXXXXXXXXXmov $0x9,%esi
12 4004f1:bf XXXXXXXXXXmov $0x3,%edi
13 4004f6:b XXXXXXXXXXmov $0x0,%eax
14 4004fb:e8 dd ff ff ff callq 4004dd
XXXXXXXXXX:f3 c XXXXXXXXXXrepz retq
Fill the table with appropriate values.
Line
# PC Instruction %rdi %rsi %rax %rsp *%rsp
XXXXXXXXXX0x7fffffffe820 -------------------
6
7
8
2

2021-spring-test2.fm
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/1/2021
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/1/2021
1. (5 points) Complete the C function below. Convert shift to a number. Do not use
or
:
unknown:
movq %rsi, %rax
sarq $1, %rax
addq %rdi, %rax
ret
long unknown(long x, long y) {
return ______________________________________;
}
2. (10 points) Assume the following values are stored at the indicated memory addresses and registers,
fill in the following table showing the values for the indicated operands in hexadecimal. You do not
need to write 0x. Instructions are independent.
3. (10 points) Using the jump to middle logic, fill in the C code for the assembly below.
Address Value Register Value Instruction Destination Value
0x1000 0xAA %rdi 0x1000 addq (%rdi),%rsi
0x1004 0xBB %rsi 0x1 andq %rsi,%rdi
0x1008 0xCC %rdx 0x2 subq %rsi,(%rdi)
0x100C 0xDD -- XXXXXXXXXXincq %rsi
---------- --------- -- XXXXXXXXXXleaq 4(%rdi,%rdx,2),%rsi
(a)C code
long loop_while(long x, long y){
long z = _______(a)__________;
while (_______(b)_________) {
z = _________(c)____________;
y = _________(d)____________________;
}
return z;
}
loop_while:
movl $0, %eax
jmp .L2
.L3:
addq %rsi, %rax
subq $1, %rsi
.L2:
cmpq %rsi, %rdi
jl .L3
rep ret
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/1/2021
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/1/2021
4. (10 points) Fill in the missing parts of the C code for the assembly and jump table shown below.
5. (10 points) Given the a
ay declarations, fill in the following table describing the element size in bytes,
the total size in bytes, and the address of element i for each of these a
ays using the base addresses
given in the “Base address” column.
A
ay declaration Element size Total size Base address Address of element i
char a[3]; base_a
char *b[4]; base_
short c[5]; base_c
short **d[6]; base_d
int e[5]; base_e
long *f[4]; base_f
double *g[3]; base_g
C code
long switche
(long x, long y, long z){
long w = 1;
switch(x) {
case 1:
_________(a)____________

eak;
case 2:
_________(b)____________
case 3:
_________(c)____________

eak;
case 4:
case 6:
_________(d)____________

eak;
default:
_________(e)____________
}
return w;
}
Assembly Code
switcher:
.L3:
leaq(%rsi,%rdx), %rax
ret
.L5:
movq %rdi, %rax
subq %rsi, %rax
ret
.L6:
movq %rsi, %rax
imulq %rdx, %rax
ret
.L7:
movl $2, %eax
ret
Jump Table
.L4:
.quad .L7
.quad .L3
.quad .L5
.quad .L5
.quad .L6
.quad .L7
.quad .L6
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/1/2021
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/1/2021
6. (10 points) Fill the table with 0's and 1's for four flags: CF (ca
y flag), SF (sign flag). Write N/A if
either the instruction or values do not conform to the AT&T (Linux) assembly:
7. (15 points) Suppose basex, the base address of short integer a
ay X and long integer index j are stored
in registers %rax and %
x, respectively. For each of the following expressions, give its type, a for-
mula for its value and an asembly code implementation. The result shoud be stored in register %rcx if
it is a pointer and register element %cx if it has data type short. For address computation, use
leax offset(base,index,scale factor), where x needs to be replaced with an appropri-
ate char.
Number Type %rdi/edi/di %rsi/esi/si Instruction CF SF ZF OF
Unsigned 0xFFFE 0x4 addw %di, %si
Signed two's
complement
0xFFFD 0x3 addw %di, %si
Signed two's
complement
0xFFFFFFFF 0x XXXXXXXXXXaddl %edi, %esi
Signed two's
complement
-0xFFFF 0xFFFF subl %si, %di
Unsigned 0xFF 0xF shlq 64, %rdi
Expression Type Value Assembly code
X[2]
X+3
&X[j]
X[4*j+1]
X+j-4
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/1/2021
CS350 Spring XXXXXXXXXXTest 2, Thur, 4/1/2021
8. (30 points) Consider the C as well as disassembled code below:
int main(int argc,char **argv)
{
long x=3,y=9,z=0;
z = top(x,y);
return z;
}
long top(long x,long y) {
x = x + y;
return leaf(x,y);
}
long leaf(long y,long z) {
z = y - z;
return z;
}
1 00000000004004d6 :
2 4004d6:48 89 f XXXXXXXXXXmov %rdi,%rax
3 4004d9:48 29 f XXXXXXXXXXsub %rsi,%rax
4 4004dc:c XXXXXXXXXXretq
5 00000000004004dd :
6 4004dd:48 01 f XXXXXXXXXXadd %rsi,%rdi
7 4004e0:b XXXXXXXXXXmov
Answered 2 days After Mar 21, 2023

Solution

Baljit answered on Mar 23 2023
29 Votes
1.
%rdi is for argument z and %rsi is for arguments x.
1. movl command move bit pattern of -1 into eax accummumlator .In C it is same as initialization of y=-1;
2. now jump .l2 will jump to the .l2 block in which it will compare the values in register rsi and rdi that same as x and z in C language.
3. jg command is jump to l3 block if value in rsi is greater than value in rdi otherwise return. In C it is x>z.
4. subq %rdi,%rax will subtract the value of rdi from rax and than save it into rax .In c it is equal to y=y-z.
5. addq $2,%rsi means and 2 to value in rsi and store it in rsi. C equivalent is x=x+2
So code is
(a)C code
long loop_while(long z, long x)
{
long y = -1;
while (x>z)
{
y =y-z;
x =x+2;
}
return y;
}
2.
    A
ay declaration
    Element size
(In byte)
    Total size
    Base address
    Address of element k
    char d[2];
    1
    2*1=2
    base_d
    base_d +1*k
    char *e[4];
    8
    4*8=32
    base_e
    base_e+8*k
    short...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here