From 99d51bfcdee5c0a91329a65d37910422d4f8e248 Mon Sep 17 00:00:00 2001 From: Brandt Redd Date: Sat, 17 Feb 2024 15:39:56 -0700 Subject: [PATCH] Recitation 5 source code. --- _pages/recitations.md | 3 +- recitation/recitation05_Noon.c | 90 +++++++++++++++++++++++++++++++++ recitation/recitation05_Three.c | 84 ++++++++++++++++++++++++++++++ 3 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 recitation/recitation05_Noon.c create mode 100644 recitation/recitation05_Three.c diff --git a/_pages/recitations.md b/_pages/recitations.md index bbd6a06..0c6bfde 100644 --- a/_pages/recitations.md +++ b/_pages/recitations.md @@ -23,7 +23,8 @@ Recitations are on Fridays at Noon and 3:00pm in EB 423 (same room as the labs). * 🚶‍♀️[Make Walkthrough](/ecen224/recitation/make-walkthrough) ## R05 - C Programming -* Source code from in-class programming coming soon! +* [Source code from Noon Session](/ecen224/recitation/recitation05_Noon.c){: download="stuff.c"} +* [Source Code from 3:00 Session](/ecen224/recitation/recitation05_Three.c){: download="stuff_1.c"} ## R06 - Introduction to Assembly Language * 🖼️[Slides - Assembly Language](https://1drv.ms/p/s!AsDairlA1Y6-luYxDcaLb5bN0-1QIQ?e=upwXCQ) \ No newline at end of file diff --git a/recitation/recitation05_Noon.c b/recitation/recitation05_Noon.c new file mode 100644 index 0000000..3ef3486 --- /dev/null +++ b/recitation/recitation05_Noon.c @@ -0,0 +1,90 @@ +#include +#include + +int mydivide(int a, int b, int *p) { + *p = a % b; + return a / b; +} + +/* +void printbinary(short x) { + for (int i=15; i>=0; --i) { + printf("%d", (x >> i) & 1); + } + printf("\n"); +}*/ + +void printbinary(short x) { + for (int i=0; i<16; ++i) { + printf("%c", (x & 0x8000) ? '1' : '0'); + x <<= 1; + } + printf("\n"); +} + +void printstringln(char *p) { + while (*p != (char)0) { + fwrite(p, 1, 1, stdout); + p++; + } + fwrite("\n", 1, 1, stdout); +} + +struct thing { + int ivalue; + char cvalue; +}; +typedef struct thing thg; + +int main() { + unsigned char c = 0x9c; + unsigned short s = 0x9a7b; + unsigned int i = 0xbc8fe301; + unsigned long l = 0xabcdef0123456789; + uint16_t i16 = 0xA49D; + size_t t = 25; + + /* + printf("c = %02x sizeof(c)=%ld\n", c, sizeof(c)); + printf("s = %04x sizeof(s)=%ld\n", s, sizeof(s)); + printf("i16 = %04x sizeof(s)=%ld\n", i16, sizeof(i16)); + printf("i = %08x sizeof(i)=%ld\n", i, sizeof(i)); + printf("l = %016lx sizeof(l)=%ld\n", l, sizeof(l)); + printf("t = %016lx sizeof(t)=%ld\n", t, sizeof(t)); + */ + + int r; + int q = mydivide(12, 7, &r); + printf("q = %d, r = %d\n", q, r); + + short a = 0x0002; + short b = 0x0102; + printbinary(a); + printbinary(b); + printbinary(a | b); + printbinary(a & b); + printbinary(a << 2); + a <<= 2; + printbinary(a); + + printstringln("Hello, world!"); + + char *pchar = 0; + //*pchar = 'Q'; // Cause seg fault by writing to null pointer. + printf("pchar = %lx\n", (size_t)pchar); + pchar++; + printf("pchar = %lx\n", (size_t)pchar); + + int *pint = 0; + printf("pint = %lx\n", (size_t)pint); + pint++; + printf("pint = %lx\n", (size_t)pint); + + struct thing th; + th.ivalue = 42; + th.cvalue = 'B'; + + thg *pt; + pt = &th; + printf("%d\n", pt->ivalue); +} \ No newline at end of file diff --git a/recitation/recitation05_Three.c b/recitation/recitation05_Three.c new file mode 100644 index 0000000..0c28004 --- /dev/null +++ b/recitation/recitation05_Three.c @@ -0,0 +1,84 @@ +// Compile: gcc stuff.c +// Compile for 32-bit: gcc -m32 stuff.c +// Compile to assembly: gcc -S stuff.c +#include +#include + +int mydivide(int a, int b, int* pr) { + *pr = a % b; + return a / b; +} + +int16_t mydivide16(int16_t a, int16_t b, int16_t* pr) +{ + *pr = a % b; + return a / b; +} + +void setbit(int bit, int* px) { + *px |= (1 << bit); +} + +void resetbit(int bit, int* px) { + *px &= ((1 << bit) ^ -1); +} + +void printbinary(int x) { + for (int i=31; i>=0; --i) { + printf("%d", (x >> i) & 1); + } + printf("\n"); +} + +int main() { + unsigned char c = 0x9c; + unsigned short s = 0x9a7b; + unsigned int i = 0xbc8fe301; + uint64_t l = 0xabcdef0123456789; + uint16_t i16 = 0xA49D; + size_t t = 25; + + printf("c = %02x sizeof(c)=%zd\n", c, sizeof(c)); + printf("s = %04x sizeof(s)=%zd\n", s, sizeof(s)); + printf("i16 = %04x sizeof(s)=%zd\n", i16, sizeof(i16)); + printf("i = %08x sizeof(i)=%zd\n", i, sizeof(i)); + printf("l = sizeof(l)=%zd\n", sizeof(l)); + printf("t = %016zx sizeof(t)=%zd\n", t, sizeof(t)); + + int r; + int d = mydivide(12, 5, &r); + printf("12 / 5 = %d remainder %d\n", d, r); + int16_t r16; + int16_t d16 = mydivide16(12, 5, &r16); + printf("12 / 5 = %d remainder %d\n", d16, r16); + + char* pc = 0; + int* pint = 0; + printf("pc = %8zx pint = %8zx\n", (size_t)pc, (size_t)pint); + ++pc; + ++pint; + printf("pc = %8zx pint = %8zx\n", (size_t)pc, (size_t)pint); + + int a[5]; + a[0] = 10; + a[1] = 11; + a[2] = 12; + a[3] = 13; + a[4] = 14; + + for (int i=0; i<5; ++i) { + printf("a[%d]=%d\n", i, i[a]); // Translates to *(i+a) + } + + printf("3[a]=%d\n", 3[a]); // Translates to *(3+a); + + printf("\n"); + int x = 0x0800; + printbinary(x); + setbit(3, &x); + printbinary(x); + x <<= 1; + printbinary(x); + resetbit(12, &x); + printbinary(x); +}