Compare commits
2 Commits
db8065c2ad
...
86d5de0aeb
Author | SHA1 | Date | |
---|---|---|---|
86d5de0aeb | |||
6aa1dfd37b |
197
Fall-2024/CS-3843/Assignments/11/Assignment.typ
Normal file
197
Fall-2024/CS-3843/Assignments/11/Assignment.typ
Normal 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
|
BIN
Fall-2024/CS-3843/Assignments/11/assets/bad-scaling-factors.png
Normal file
BIN
Fall-2024/CS-3843/Assignments/11/assets/bad-scaling-factors.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 122 KiB |
Binary file not shown.
BIN
Fall-2024/CS-3843/Slides/Part-Two/Lecture 01- Introduction.pptx
Normal file
BIN
Fall-2024/CS-3843/Slides/Part-Two/Lecture 01- Introduction.pptx
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Fall-2024/CS-3843/Slides/Part-Two/Lecture 06 - Procedures.pptx
Normal file
BIN
Fall-2024/CS-3843/Slides/Part-Two/Lecture 06 - Procedures.pptx
Normal file
Binary file not shown.
BIN
Fall-2024/CS-3843/Slides/Part-Two/Lecture 07 - data.pptx
Normal file
BIN
Fall-2024/CS-3843/Slides/Part-Two/Lecture 07 - data.pptx
Normal file
Binary file not shown.
Binary file not shown.
BIN
Fall-2024/CS-3843/Slides/Part-Two/RISCvsCISC.pdf
Normal file
BIN
Fall-2024/CS-3843/Slides/Part-Two/RISCvsCISC.pdf
Normal file
Binary file not shown.
BIN
Fall-2024/CS-3843/Slides/Part-Two/register conventions.pptx
Normal file
BIN
Fall-2024/CS-3843/Slides/Part-Two/register conventions.pptx
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user