Week 2.b CS6640 09/14 2023 https://naizhengtan.github.io/23fall/ 1. C pointers (cont'd) 2. C arrays 3. Some C keywords 4. Bitwise operators 5. labs setup ---- Admin: - mixed feedback; will be a survey next week 1. C pointers (cont'd) - uninitialized local variables on stack [demo: void bar() { int l; // local variables don't have a default value printf("uninitialized local variable: %x\n", l); } void foo() { int xyz = 0x12345678; printf("quit foo w/ local var = %x\n", xyz); } ] Q: In main(), call bar(), then foo(); what do you expect to see? Q: change "int l" to "char l". what do you expect to see? - pointer arithmetic [demo: int main() { char *c = (char *)0x08200000; int *i = (int *) 0x08200000; printf("%p %p\n", (c+1), (i+1)); return 0; } ] Q: what is the value of c+1 and i+1? 2. C arrays - an array: contiguous memory holding same data type (char, int, etc.) no bound checking, no growing - two ways to access arrays: through index: buf[0], buf[1] through pointer: *buf, *(buf+1) [demo: int a[3] = {1, 2, 3}; // an array of 3 int's char b[3] = {'a', 'b', 'c'}; // an array of 3 char's int main() { // first element is at index 0 printf("%d\n", a[0]); a[1] += 1; // use index access *(a+2) = 5; // pointer access printf("%d %d\n", a[1], a[2]); // pointers to array elements printf("a %p a1 %p a2 %p a2 %p\n", a, a+1, a+2, &a[2]); // pointer arithmetic uses type printf("%p %p\n", b, b+1); return 0; } ] 3. keywords: [skipped] - static: to make a variable's visibility limited to the file it is declared but global within the file - union: a composite data type that allows you to store different types of data in the same memory location. This is useful for data that can be interpreted as multiple types. For example, see library/file/file.h [draw union as memory] - bit fields (not a keyword): a bit field is a way to define and use variables with a specified number of bits, allowing you to represent and manipulate data at the bit level within a structure. for example: struct valid_addr { int valid : 1; int addr : 31; }; struct valid_addr a; a.valid = 1; a.addr = 0xdeadbeef; // will fail; needs 0x7deadbeef 4. bitwise operators - you can manipulate bits with |, &, ~, ^ 10001 & 10000 = 10000 // bit-wise and 10001 | 10000 = 10001 // bit-wise or 10001 ^ 10000 = 00001 // bit-wise xor ~1000 = 0111 // bit-wise not - Q: how to set an int X's 11th--12th bits to be 01? - an example: earth/earth.c asm("csrr %0, mstatus" : "=r"(mstatus)); asm("csrw mstatus, %0" ::"r"((mstatus & ~(3 << 11)) /* clear MPP */ | (1 << 11) )); /* set MPP to S */ Many questions arise: -- Q: what is this "asm"? a way to directly write assembly code in C [will talk about this in the next lecture] -- Q: what are "csrr" and "csrw"? they are the instructions to read and write "control and status registers" [risc-v instruction listing (on Reference page)] -- Q: what is "mstatus"? a "control and status register" [risc-v privileged spec (on Reference page) S3.1.6] -- MPP in mstatus: * three privilege modes of a CPU (0: U, 1: S, 3: M) * M in MPP stands for Machine mode... * ...meaning when "mret", which privilege mode setting to the CPU 5. labs setup - updated Makefile [a tutorial , if you are interested: https://makefiletutorial.com/ ] - draw upstream, origin, local - lab2 will be released by today