diff options
author | Kamil Kaminski <kyle@kkaminsk.com> | 2012-01-18 04:23:30 -0600 |
---|---|---|
committer | Kamil Kaminski <kyle@kkaminsk.com> | 2012-01-18 04:23:30 -0600 |
commit | 1c411a3946dec2976aa3456b2a8b39b9f6d42a8f (patch) | |
tree | ab42b15c6664f0fa06701b280035435c855aef33 | |
parent | 4aec0d6aa631902a8aa576dc2594a1807466f123 (diff) | |
download | sandbox-1c411a3946dec2976aa3456b2a8b39b9f6d42a8f.tar.gz sandbox-1c411a3946dec2976aa3456b2a8b39b9f6d42a8f.tar.bz2 sandbox-1c411a3946dec2976aa3456b2a8b39b9f6d42a8f.zip |
add few more
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | asm.c | 13 | ||||
-rw-r--r-- | asm2.c | 65 | ||||
-rw-r--r-- | c99.c | 46 | ||||
-rw-r--r-- | playground_05-25-2011.c | 141 |
5 files changed, 266 insertions, 1 deletions
@@ -1,6 +1,6 @@ BINS = ascii class depipe_strings dup fpipe pipe realloc strpbrk strsep \ tokenizer getopt prime_mask linked_list pi_bbp string_tokenizer \ - tcpserver tcpclient regexp + tcpserver tcpclient regexp c99 asm playground_05-25-2011 CURL = curl_tomem curl_tofile @@ -0,0 +1,13 @@ +#include <stdio.h> + +int main(int argc, char *argv[]) +{ + int i = 12; + + asm(".att_syntax prefix"); + asm("movl %0, %%eax\n" : : "r" (i) : "eax"); + printf("i is: %d\n", i); + + return 0; +} + @@ -0,0 +1,65 @@ +/* + * So far I cannot figure out how I used to compile this :/ + * asm(".intel_syntax prefix"); + * + */ + +#include <stdio.h> + +void asm_summation(int *n) +{ + asm("xor eax, eax \n" /* summation */ + "mov ecx, 0 \n" /* counter */ + "mov edx, %0 \n" /* end */ + "_loop: \n" + "add eax, ecx \n" + "inc ecx \n" + "cmp ecx, edx \n" + "jle _loop \n" + "mov %0, eax \n" : "=r" (*n) : "r" (*n) : "eax"); +} + +void asm_summation2(int *n) +{ + if (*n <= 1) + { + *n = 0; + return; + } + + asm("xor eax, eax \n" /* summation */ + "mov ecx, %0 \n" /* counter */ + "_loop2: \n" + "add eax, ecx \n" + "loop _loop2 \n" + "mov %0, eax \n" : "=r" (*n) : "r" (*n) : "eax"); +} + +void asm_rotate(int *n) +{ + asm("mov eax, %0 \n" + "rol eax, 1 \n" + "mov %0, eax \n" : "=r" (*n) : "r" (*n) : "eax"); +} + +int main(int argc, char *argv[]) +{ + int i = 12; + + + + /* output, input (r) = register, clobbered */ + asm("mov eax, %0\n" : : "r" (i) : "eax"); + printf("i is: %d\n", i); + + int j = -1; + asm_summation2(&j); + printf("summation is %d\n", j); + + int k = 2; + asm_rotate(&k); + printf("k = %d\n", k); + + return 0; +} + @@ -0,0 +1,46 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <memory.h> + +struct node +{ + int x; + int y; + int z; + + char name[21]; +}; +typedef struct node node_t; + +void mymemset(void *mem, char c, unsigned int n) +{ + char *p = (char *) mem; + + while (n--) + *p++ = c; +} + +void func(int arr[]) +{ + printf("the size of arr is: %lu\n", sizeof(arr)); + arr[0] = 9; +} + +int main(int argc, char *argv[]) +{ + node_t *p = (node_t *) malloc(sizeof(node_t) + 21); /* c99 */ + strcpy(p->name, "I have a long name"); + + printf("%s", p->name); + + int myarr[] = { 1,2,3,4,5,6,7,8,9,10 }; + func(myarr); + + printf("%d\n", myarr[0]); + + mymemset(p, -1, sizeof(node_t)); + + return 0; +} + diff --git a/playground_05-25-2011.c b/playground_05-25-2011.c new file mode 100644 index 0000000..20249a7 --- /dev/null +++ b/playground_05-25-2011.c @@ -0,0 +1,141 @@ +/* + * playground_05-25-2011.c + * + * + * Playing around with unions and macros, and how both + * of these things look in the memory + * + */ + +#include <stdio.h> +#include <stdint.h> + +#define EXP(base, exp, ret) do { \ + int i; \ + int acc = 1; \ + for (i = 0; i < exp; i++) \ + acc *= base; \ + ret = acc; \ + } while(0) \ + +#define MUL(base, mult, ret) do { \ + int i; \ + int acc = 0; \ + for (i = 0; i < mult; i++) \ + acc += base; \ + ret = acc; \ + } while(0) \ + +/* trace a variable, requires ANSI compliant compiler */ +#define TRACE(var, fmt) \ + printf("TRACE: " #var " = " #fmt ", %s:%d\n", var, __FILE__, __LINE__) + +#pragma pack(1) +struct UART_tag +{ + union + { + struct + { + uint32_t baud_rate; /* bits [0:31] */ + + uint8_t tx_en : 1; + uint8_t rx_en : 1; + uint8_t parity : 1; /* bit [34], 35th */ + + uint32_t : 29; + + } B; + + uint64_t R; + } SET; + +}; +typedef struct UART_tag UART_t; + +void UART_info(UART_t *dev) +{ + printf("baud rate: %u\n" + "tx enabled: %s\n" + "rx enabled: %s\n" + "parity enabled: %s\n", dev->SET.B.baud_rate, + dev->SET.B.tx_en ? "yes" : "no", + dev->SET.B.rx_en ? "yes" : "no", + dev->SET.B.parity ? "yes" : "no"); +} + +void dumpHex(UART_t *p, unsigned int sz) +{ + int i; + for (i = 0; i < sz; i++) + { + if (i % 4 == 0) + printf("%s0x%08x: ", i == 0 ? "" : "\n", i); + + printf(" 0x%02x", ((unsigned char *) p)[i] ); + } + printf("\n\n"); +} + +int main(int argc, char **argv) +{ + /* assigning a struct within the union */ + puts("\t assigning a struct within the union"); + UART_t myUart; + myUart.SET.R = 0; + myUart.SET.B.baud_rate = 19200; + myUart.SET.B.tx_en = 1; + myUart.SET.B.rx_en = 1; + myUart.SET.B.parity = 0; + + UART_info(&myUart); + puts(""); + + puts("\t this is how UART_t looks in the memory"); + dumpHex(&myUart, sizeof(UART_t)); + + /* disable a flag within the struct */ + puts("\t disable a flag within the struct"); + myUart.SET.B.tx_en = 0; + UART_info(&myUart); + puts(""); + + /* modifying the whole structure at once */ + puts("\t modifying the whole structure at once"); + myUart.SET.R = 0; + UART_info(&myUart); + puts(""); + + /* enable the parity bit */ + myUart.SET.R |= (uint64_t) \ + ((uint64_t) 1 << (64 - 30)); /* this is inverted compared + * to how it would be done on + * a register */ + + puts("\t setting the parity bit to ON using logical OR operator"); + UART_info(&myUart); + puts(""); + + printf("size of UART_t is: %lu bytes\n", sizeof(UART_t)); + printf("size of UART_tag is: %lu bytes\n", sizeof(myUart.SET.B)); + + int x; + EXP(2, 20, x); + printf("a megabyte is %d bytes\n", x); + + int y; + MUL(x, 4, y); + printf("4 megabytes is %d bytes\n", y); + + /* endianess */ + int z = 1; + if (*((unsigned char *) &z) == 1) + puts("machine has a little-endian byte ordering"); + else + puts("machine has a big-endian byte ordering"); + + TRACE(y, %d); + + return 0; +} + |