-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
176 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |