college/Fall-2024/CS-3843/Assignments/6/Assignment.typ

208 lines
7.1 KiB
Plaintext
Raw Normal View History

2024-10-20 15:48:32 -05:00
#show link: set text(blue)
#set text(font: "Calibri")
#show raw: set text(font: "Fira Code")
#set page(margin: (x: .5in, y: .5in))
#let solve(solution) = [
#let solution = align(
center,
block(
inset: 5pt,
stroke: blue + .3pt,
fill: rgb(0, 149, 255, 15%),
radius: 4pt,
)[#align(left)[#solution]],
)
#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) = [
#align(
center,
block(
inset: 5pt,
stroke: luma(20%) + .3pt,
fill: luma(95%),
radius: 4pt,
)[#align(left)[#content]],
)
]
#align(center)[
= CS 3843 Computer Organization
HW 6\
#underline[Price Hiller] *|* #underline[zfp106]
]
#line(length: 100%, stroke: .25pt)
Note: Handwritten HWs are allowed, but you need to scan it and submit a PDF file. Show your work.
\
\
1. (a) Pick two positive integer numbers. Show that if you multiply them, then you will get a negative integer as your result because of overflow. Therefore, you need to demonstrate what happens when overflow happens with twos complement integer multiplication. You have a 4-bit processor, and the number representation is twos complement.
#solve[
+ First recognize that the max positive value for two's complement in a 4-bit processor is $7$
+ Pick two numbers, say $4$ and $2$
+ Multiply: $4 ⋅ 2 = 8$
+ $8 > 7$, thus we will have an overflow
+ Convert $8$ to binary: $1000$
+ Recognize that $1000$ is $-8$ in binary
+ An overflow has occured, resulting in a value of $-8$ from the multiplication of $4$ and $2$
]
(b) Pick two unsigned integer numbers. Show that if you multiply them, then you will get an unexpected integer as your result because of overflow. Therefore, you need to demonstrate what happens when overflow happens with unsigned integer multiplication. You have a 4-bit processor, and the number representation is unsigned representation.
#solve[
#underline[First way]
+ First recognize that the max value for an unsigned integer in a 4-bit processor is $15$
+ Pick two numbers, say $10$ and $2$
+ Multiply: $10 ⋅ 2 = 20$
+ $20 > 15$, thus we will have an overflow
+ Convert $20$ to binary: $10100$
+ Truncate all bits beyond the 4th bit: $10100$ -> $0100$
+ The value overflows to $4$
#underline[Second way]
+ Use the same two numbers, $10$ and $2$
+ Multiply with a modulo of $16$ this time
+ $(10 ⋅ 2) mod 16 = 20 mod 16 = 4$
+ The value also overflows to $4$
]
2. Let's say we have an 8-bit floating point representation.
The sign bit is in the most significant bit.\
The next four bits are the exponent, with a bias of 7.\
The last three bits are the frac.
(a) Convert the number $4/512$ to binary. Use the denormalized format. Also, use the bias equation for the denormalized form to find the EXP value.
#note[
+ Firstly, to simplify, we can recognize that $4/512 = 1/128 = 2^(-7)$
+ $1/128$ in binary: $0.00000001$
+ Denormalized representation: $0.000000001$
+ $"EXP" = -7 + 7 = 0000$
+ Sign bit: $0$
+ Exponent: $0000$
+ Fraction: $100$
+ #solvein[`0 0000 100`]
_I am really, REALLY, hoping the above is correct. This stuff kinda hurts._
]
(b) Convert the number $221.0$ to binary. Use the normalized format. Also, use the bias equation for the normalized form to find the EXP value.
#note[
+ Convert $221$ to binary: `11011101`
+ Normalize: $221.0 = 1.1011 101$
+ Exponent: $7 + 7 -> 14 -> 1110$
+ Round: $1.011$
+ #solvein[`0 1110 011`]
]
3. [Show your work] We have two integer numbers, $x=-8, y=-5$. First represent them in two's complement form, then perform addition. Try to find if overflow happens or not. Try to detect what type (positive or negative) of overflow happens. You have a 4-bit processor.
#note[
#align(center)[#table(
columns: (auto, auto, auto, auto),
stroke: none,
align: center,
[$x$], [$y$], [$x + y$], [$x +^t_4 y$],
table.hline(),
[$-8$], [$-5$], [$-13$], solvein[$3$],
[$[1000]$], [$[1011]$], [$[10011]$], solvein[$[0011]$],
table.hline(),
)
#solvein[A negative overflow occurred.]
#note[Alternatively, instead of bit fiddling: $(-8 + -5) "mod" 4 = #solvein[3]$]
By the way, this problem is in the Lecture 6 slides.
]
]
4. Convert 16-bit binary versions of $2_("ten")$ and $-2_("ten")$ to 32-bit binary numbers. Both of the numbers are in two's complement representation. Show your work.
#solve[
#underline[Working $2_("ten")$]
+ Convert $2$ to binary: $0010$
+ Pad the most significant bit length of 16-bits
+ `00000000 00000010`
+ Since $2$ is shockingly _still_ positive, we can pad those zeroes out to 32-bits
+ `00000000 00000000 00000000 00000010`
]
#solve[
#underline[Working $-2_("ten")$ :(]
+ Convert $-2$ to two's complement binary: $0010$
+ Negate $0010 -> 1101$
+ Add $1$: $1101 + 1 = 1110$
+ Pad the most significant bit for the desired bit length of 16-bits
+ `11111111 11111110`
+ Now pad out to 32-bits
+ `11111111 11111111 11111111 11111110`
]
5. [Show your work] Negate $2_("ten")$, first you need to convert this two's complement integer number into 32-bits binary and then do the negation. Show that the result is $-2_("ten")$.
#solve[
+ $2_10$ to binary, $2_10 -> 2_8$
+ `00000000 00000000 00000000 00000010`
+ Negate `00000000 00000000 00000000 00000010`:
#table(
stroke: none,
align: right,
[`~ 00000000 00000000 00000000 00000010`],
table.hline(),
[`11111111 11111111 11111111 11111101`]
)
+ Add `1`: `11111111 11111111 11111111 11111101` $+$ `1`
+ `11111111 11111111 11111111 11111110`
+ `11111111 11111111 11111111 11111110` is $-2$
+ ∴ Negation of $2_8$ is $-2_8$
]
6. [Show your work] What is the decimal value of this 32-bit two's complement number? `11111111 11111111 11111111 11111100`#sub("two")
#note[
There's _two_ ways of going about this.
#underline[The first and lazier way is to just write a C program:]
```c
#include <stdint.h>
#include <stdio.h>
int main(void)
{
int32_t num = 0b11111111111111111111111111111100;
printf("%d\n", num);
}
```
Which yields our decimal answer to be #solvein[$-4$].
#underline[And then there's the likely _intended_ way of doing this by hand:]
+ Firstly, recongnized that we have a negative number because the first bit is `1`.
+ Now we flip all the bits, `~11111111 11111111 11111111 11111100`
#table(
stroke: none,
align: right,
[`~ 11111111 11111111 11111111 11111100`],
table.hline(),
[`00000000 00000000 00000000 00000011`]
)
+ Add $1$: `00000000 00000000 00000000 00000011 + 1 = 00000000 00000000 00000000 00000110`
+ Now convert `00000000 00000000 00000000 00000110` to decimal, which yields $4$
+ Remember that we had a _*negative*_ number, so multiply by $-1$: $4 ⋅-1 = -4$
+ #solvein[We get $-4$]
]