cs-3843: add hw 2

This commit is contained in:
Price Hiller 2024-09-19 13:19:14 -05:00
parent e83d847342
commit 5689457fac
Signed by: Price
GPG Key ID: C3FADDE7A8534BEB
8 changed files with 302 additions and 0 deletions

View File

@ -0,0 +1,302 @@
#show link: set text(blue)
#set text(font: "Calibri")
#show raw: set text(font: "Fira Code")
#import "@preview/tablex:0.0.4": tablex, rowspanx, colspanx, hlinex, vlinex
#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 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 2\
#underline[Price Hiller] *|* #underline[zfp106]
]
#line(length: 100%, stroke: .25pt)
= #text(fill: red)[1#super[st] Computer/Environment (15 points)]
At first, compile and run these two programs on your own Laptop/Desktop. Then answer these questions:
+ What is the name and size of the Operating System?
#solve[
- The name of my operating system is `GNU/Linux` according to `uname -o`.
- The size/architecture of the OS is `64 bit/x86_64` according to `uname -m`.
]
+ What is the name and size of the Processor?
#solve[
- The name of the Processor is `13th Gen Intel(R) Core(TM) i9-13900H` from `/proc/cpuinfo`
- The size/architecture of the processor is `64 bit/x86_64`
]
+ Paste the screenshot of the output of your 1st program.
#solve[
#figure(
image("./assets/first-prog-part-one.png", width: 80%),
) <fig-first-prog-part-one>
]
+ Paste the screenshot of the output of your 2nd program.
#solve[
#figure(
image("assets/second-prog-part-one.png", width: 80%),
) <fig-second-prog-part-one>
]
+ Analyze the output and mention your computer is little-Endian or big-Endian. Explain how you reached the conclusion of endianness of your machine
#solve[My computer is Little Endian. Firstly I know this because `lscpu | grep "Byte Order"` states as much, but secondly I know this because the ordering of bytes from the first program has the most significant bytes placed at higher addresses than the least significant bytes.]
= #text(fill: red)[2#super[nd] Computer/Environment (15 points)]
At first, compile and run these two programs on UTSA Linux machine/ any Linux machine/ any
remote Linux machine/ Any Virtual Linux Machine such as Oracle VM VirtualBox. Then answer
these questions (if you cant find the answer for the first two questions of a remote machine, thats fine, you wont lose points):
+ What is the name and size of the Operating System?
#solve[
- The name of my operating system is `GNU/Linux` according to `uname -o`.
- The size/architecture of the OS is `64 bit/x86_64` according to `uname -m`.
]
+ What is the name and size of the Processor?
#solve[
- The name of the Processor is `Intel(R) Core(TM) i9-7980XE CPU @ 2.60GHz` from `/proc/cpuinfo`
- The size/architecture of the processor is `64 bit/x86_64`
]
+ Paste the screenshot of the output of your 1st program.
#solve[
#figure(
image("assets/prog-one-part-two.png", width: 80%),
) <fig-prog-one-part-two>
]
+ Paste the screenshot of the output of your 2nd program.
#solve[
#figure(
image("assets/prog-two-part-two.png", width: 80%),
) <fig-prog-two-part-two>
]
+ Analyze the output and mention your computer is little-Endian or big-Endian. Explain how you reached the conclusion of endianness of your machine
#solve[
Seeing as this is my server, the reasoning is much the same as for the previous computer. Firstly, due to `lscpu | grep "Byte Order"` I know it's Little Endian in the first place, and secondly when looking at the output of the first program, once again, the most significant bytes are at higher addresses than less significant bytes indicating a Little Endian byte order.
]
= #text(fill: red)[3#super[rd] Computer/Environment (15 points)]
At first, compile and run these two programs with online `gcc` compiler (any online web-based
`gcc` compiler is fine). Then answer these questions:
+ What is the name of the online compiler?
#solve[
The name of the online compiler is `OnlineGDB`, found \@ https://www.onlinegdb.com/online_c_compiler
]
+ If you run your programs, it should print out memory addresses, what is the size of the address?
#solve[
The size of the address is `64 bit`.
]
+ Paste the screenshot of the output of your 1st program.
#solve[
#figure(
image("assets/prog-one-part-three.png", width: 80%),
) <fig-prog-one-part-three>
]
+ Paste the screenshot of the output of your 2nd program.
#solve[
#figure(
image("assets/prog-two-part-three.png", width: 80%),
) <fig-prog-two-part-three>
]
+ Analyze the output and mention your underlying environment/computer is little-Endian or big-Endian. Explain how you reach to the conclusion of endianness of your machine
#solve[
This time I don't have `lscpu` to use to make it easier on me, right? Wrong. Using the following c program we can get `lscpu` output from the online compiler I have used:
```c
#include <stdio.h>
int main(int argc, char *argv[]) {
FILE *p = popen("lscpu", "re");
char buf[16384];
while (fgets(buf, sizeof(buf), p) != 0) {
printf("%s", buf);
}
pclose(p);
}
```
This informs me that the system is Little Endian as well and has an architecture of `x86_64` — so 64 bit.
Outside of abusing `lscpu` I can also tell that the system is Little Endian because, yet again, the first program outputs the most significant byte at the highest address and least significant byte at the lowest address.
]
#line(length: 100%, stroke: .25pt)
Bonus Question (7 Points)
+ Write a C-function that will detect the "Endianness" of a computer. Provide the function (with the whole C program) and provide the screenshot of your output.
#solve[
```c
#include <stdbool.h>
#include <stdio.h>
bool is_little_endian() {
volatile int val = 1;
return (*(char *)(&val)) == 1;
}
int main(void) {
if (is_little_endian()) {
printf("System Byte Order: Little Endian\n");
} else {
printf("System Byte Order: Big Endian\n");
}
}
```
#figure(
image("assets/little-endian-func.png", width: 70%),
caption: [Notably, `volatile` is used to ensure the compiler does _not_ do any sort of fancy optimization to the test value. Likely unnecessary, but doesn't hurt to be sure.],
) <fig-little-endian-func>
]
#line(length: 100%, stroke: .25pt)
== Program 1:
```c
#include <stdio.h>
int main(void)
{
unsigned int value = 0x12345678;
char *r = (char *) &value;
int i;
for (i=0; i<4; i++) {
printf("Address of 0x%x = %u \n", r[i], &r[i]);
}
return 0;
}
```
== Program 2:
```c
/* $begin show-bytes */
#include <stdio.h>
/* $end show-bytes */
#include <stdlib.h>
#include <string.h>
/* $begin show-bytes */
typedef unsigned char *byte_pointer;
void show_bytes(byte_pointer start, size_t len) {
size_t i;
for (i = 0; i < len; i++)
printf(" %.2x", start[i]); //line:data:show_bytes_printf
printf("\n");
}
void show_int(int x) {
show_bytes((byte_pointer) &x, sizeof(int)); //line:data:show_bytes_amp1
}
void show_float(float x) {
show_bytes((byte_pointer) &x, sizeof(float)); //line:data:show_bytes_amp2
}
void show_pointer(void *x) {
show_bytes((byte_pointer) &x, sizeof(void *));
//line:data:show_bytes_amp3
}
/* $end show-bytes */
/* $begin test-show-bytes */
void test_show_bytes(int val) {
int ival = val;
float fval = (float) ival;
int *pval = &ival;
show_int(ival);
show_float(fval);
show_pointer(pval);
}
/* $end test-show-bytes */
/* $begin simple-show-a */
void simple_show_a() {
int val = 0x87654321;
byte_pointer valp = (byte_pointer) &val;
show_bytes(valp, 1); /* A. */
show_bytes(valp, 2); /* B. */
show_bytes(valp, 3); /* C. */
}
/* $end simple-show-a */
/* $begin simple-show-b */
void simple_show_b() {
int val = 0x12345678;
byte_pointer valp = (byte_pointer) &val;
show_bytes(valp, 1); /* A. */
show_bytes(valp, 2); /* B. */
show_bytes(valp, 3); /* C. */
}
/* $end simple-show-b */
void float_eg() {
int x = 3490593;
float f = (float) x;
printf("For x = %d\n", x);
show_int(x);
show_float(f);
x = 3510593;
f = (float) x;
printf("For x = %d\n", x);
show_int(x);
show_float(f);
}
/* $begin show-ustring */
void string_ueg() {
const char *s = "ABCDEF";
show_bytes((byte_pointer) s, strlen(s));
}
/* $end show-ustring */
/* $begin show-lstring */
void string_leg() {
const char *s = "abcdef";
show_bytes((byte_pointer) s, strlen(s));
}
/* $end show-lstring */
/* $begin show-twocomp */
void show_twocomp()
{
short x = 12345;
short mx = -x;
show_bytes((byte_pointer) &x, sizeof(short));
show_bytes((byte_pointer) &mx, sizeof(short));
}
/* $end show-twocomp */
int main()
{
int val = 12345;
printf("calling test_show_bytes\n");
test_show_bytes(val);
return 0;
}
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 224 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB