cs-3843: add assignment 11

This commit is contained in:
Price Hiller 2024-12-03 22:19:03 -06:00
parent 6aa1dfd37b
commit 86d5de0aeb
Signed by: Price
GPG Key ID: C3FADDE7A8534BEB
2 changed files with 197 additions and 0 deletions

View File

@ -0,0 +1,197 @@
#show link: set text(blue)
#set text(font: "Calibri")
#show raw: set text(font: "Fira Code")
#set table.cell(breakable: false)
#set table(stroke: (x, y) => (
left: if x > 0 {
.1pt
},
top: if y == 1 {
0.5pt
} else if y > 1 {
0.1pt
},
))
#set page(margin: (y: .25in, x: .5in))
#let solve(solution) = {
block(
outset: 3pt,
inset: 3pt,
stroke: blue + .3pt,
fill: rgb(0, 149, 255, 15%),
radius: 4pt,
)[#solution]
}
#let solvein(solution) = {
let outset = 3pt
h(outset)
box(
outset: outset,
stroke: blue + .3pt,
fill: rgb(0, 149, 255, 15%),
radius: 4pt,
)[#solution]
}
#let note(content) = {
block(
outset: 3pt,
inset: 5pt,
stroke: luma(20%) + .3pt,
fill: luma(95%),
radius: 4pt,
)[#content]
}
#let notein(content) = {
let outset = 3pt
h(outset)
box(
outset: outset,
stroke: luma(20%) + .3pt,
fill: luma(95%),
radius: 4pt,
)[#content]
}
#align(center)[
= CS 3843 Computer Organization
HW 11\
#underline[Price Hiller] *|* #underline[zfp106]
]
#line(length: 100%, stroke: .25pt)
1. You are given the following information. A function with the prototype `void decode1(long *xp, long *yp, long *zp);` is compiled into assembly code yielding the following:
#text(blue)[`void decode1(long *xp, long *yp, long *zp)` \ `xp` in `%rdi`, `yp` in `%rsi`, `zp` in `%rdx`]
```asm
decode1:
movq (%rdi), %r8
movq (%rsi), %rcx
movq (%rdx), %rax
movq %r8, (%rsi)
movq %rcx, (%rdx)
movq %rax, (%rdi)
ret
```
Parameters `xp`, `yp` and `zp` are stored in registers `%rdi`, `%rsi`, and `%rdx`, respectively.
Write C code for `decode1` that will have an effect equivalent to the assembly code shown.
#solve[
```c
void decode1(long *xp, long *yp, long *zp) {
long r8 = *xp; // movq (%rdi), %r8
long rcx = *yp; // movq (%rsi), %rcx
long rax = *zp; // movq (%rdx), %rax
*yp = r8; // movq %r8, (%rsi)
*zp = rcx; // movq %rcx, (%rdx)
*xp = rax; // movq %rax, (%rdi)
}
```
]
2. For a function with the prototype
`long decode2(long x, long y, long z);`
GCC generates the following assembly code:
```asm
decode2:
subq %rdx, %rsi
imulq %rsi, %rdi
movq %rsi, %rax
salq $63, %rax
sarq $63, %rax
xorq %rdi, %rax
ret
```
Parameters `x`, `y`, and `z` are passed in registers `%rdi`, `%rsi`, and `%rdx`. The code stores the return value in register `%rax`.
Write C code for `decode2` that will have an effect equivalent to the assembly code shown.
#solve[
```c
long decode2(long x, long y, long z) {
y -= z; // subq %rdx, %rsi
x *= y; // imulq %rsi, %rdi
long rax = y; // movq %rsi, %rax | We can just use `y` in place of this assignment!
rax <<= 63; // salq $63, %rax
rax >>= 63; // sarq $63, %rax
rax ^= x; // xorq %rdi, %rax
return rax;
}
```
]
#align(center + horizon)[#note[#text(size: 2em)[SEE NEXT PAGE]]]
#pagebreak()
3. The assembly version is given below, write the C version of the exchange function:
Function prototype: `long exchange(long *xp, long y)`
`xp` in `%rdi`, `y` in `%rsi`
```asm
exchange:
movq (%rdi), %rax ; Get x at xp. Set as return value
movq %rsi, (%rdi) ; Store y at xp
ret
```
#solve[
```c
long exchange(long *xp, long y) {
long rax = *xp; // movq (%rdi), %rax
*xp = y; // movq %rsi, (%rdi)
return rax;
}
```
]
4. Consider the following code, in which we have omitted the expression being computed:
```c
short scale3(short x, short y, short z) {
short t = ___;
return t;
}
```
Compiling the actual function with GCC yields the following assembly code:
#text(blue)[`short scale3(short x, short y, short z)`\ `x` in `%rdi`, `y` in `%rsi`, `z` in `%rdx`]
```asm
scale3:
leaq (%rsi,rsi,9), %rbx
leaq (%rbx,%rdx), %rbx
leaq (%rbx,%rdi,%rsi), %rbx
ret
```
Fill in the missing expression in the C code.
#solve[
```c
short scale3(short x, short y, short z) {
short t = (y * 10) + z + (x * y);
return t;
}
```
#note[The given assembly is _*invalid*_ as a heads up. The scaling factor in the first and second instructions are invalid in AT&T syntax. Only values of 1, 2, 4, or 8 are allowed. #text(red)[*As such, this problem is _impossible_ to actually solve.*]
#figure(
image("assets/bad-scaling-factors.png", width: 80%),
caption: [Invalid scaling factors in the provided assembly],
) <fig-bad-scaling-factors>
]
]
Suggestion: First, try to read and figure out what is happening in your C code/ assembly code. Then try to translate your code using gcc (the compiler). Again, the version of your gcc will be different for different computers (it depends on your Operating Systems+Processor combo). Therefore, sometimes you will generate different instructions/sequence of instructions due to different versions of gcc, and that's fine, as long as the program (C program or assembly program) performs the same type of work at higher level (C language) and lower level (assembly language). Remember, the textbook is using a certain version of gcc, therefore, not all the time your answer will agree with the textbook.
Note: You need to submit a pdf file containing all your C codes/Assembly for questions. You can include your C code as screenshots in the pdf file

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 KiB