Skip to content

Commit

Permalink
Recitation 5 source code.
Browse files Browse the repository at this point in the history
  • Loading branch information
bredd committed Feb 17, 2024
1 parent b0a0d84 commit 99d51bf
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 1 deletion.
3 changes: 2 additions & 1 deletion _pages/recitations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
90 changes: 90 additions & 0 deletions recitation/recitation05_Noon.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include <stdio.h>
#include <stdint.h>

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);
}
84 changes: 84 additions & 0 deletions recitation/recitation05_Three.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Compile: gcc stuff.c
// Compile for 32-bit: gcc -m32 stuff.c
// Compile to assembly: gcc -S stuff.c
#include <stdio.h>
#include <stdint.h>

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);
}

0 comments on commit 99d51bf

Please sign in to comment.