CS61C Lab2
# Lab 2
# Exercise 0: Makefiles
这个 Exercise 要求我们学会使用 Makefile 来编译 C 语言程序
题目给出了一个 makefile
UNAME_S := $(shell uname -s)
CC=gcc
LD=gcc
CFLAGS=-ggdb -Wall -std=c99
LDFLAGS=
ifeq ($(UNAME_S), Darwin)
MEMCHECK=valgrind --tool=memcheck --leak-check=full --track-origins=yes --dsymutil=yes
endif
ifeq ($(UNAME_S), Linux)
MEMCHECK=valgrind --tool=memcheck --leak-check=full --track-origins=yes
endif
BIT_OPS_OBJS = bit_ops.o test_bit_ops.o
BIT_OPS_PROG = bit_ops
LFSR_OBJS = lfsr.o test_lfsr.o
LFSR_PROG = lfsr
VECTOR_OBJS=vector.o vector-test.o
VECTOR_PROG=vector-test
BINARIES=$(VECTOR_PROG) $(BIT_OPS_PROG) $(LFSR_PROG)
all: $(BINARIES)
$(BIT_OPS_PROG): $(BIT_OPS_OBJS)
$(CC) $(CFLAGS) -g -o $(BIT_OPS_PROG) $(BIT_OPS_OBJS) $(LDFLAGS)
$(LFSR_PROG): $(LFSR_OBJS)
$(CC) $(CFLAGS) -g -o $(LFSR_PROG) $(LFSR_OBJS) $(LDFLAGS)
lfsr.c: lfsr.h
test_lfsr.c: lfsr.h
bit_ops.c: bit_ops.h
test_bit_ops.c: bit_ops.h
.c.o:
$(CC) -c $(CFLAGS) $<
vector-memcheck: $(VECTOR_PROG)
$(MEMCHECK) ./$(VECTOR_PROG)
clean:
-rm -rf core *.o *~ "#"*"#" Makefile.bak $(BINARIES) *.dSYM
vector.c: vector.h
vector-test.c: vector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
然后给出了 10 个问题
- Which target is part of a rule that deletes all the compiled programs? 哪个目标是删除所有编译程序的规则的一部分?
clean
1
- Which target is part of a rule that makes all the compiled programs? 哪个目标是创建所有编译程序的规则的一部分?
all
1
- Which compiler is currently being used?
CC = gcc
1
- What C standard are we currently using?
CFLAGS = -ggdb -Wall -std=c99
1
- How would we reference a variable “FOO” in a makefile?
$(FOO)
1
- What operating system does the term “Darwin” represent?
"Darwin"代表的是 macOS(和 iOS 等苹果操作系统)的底层核心操作系统。
1
- What line creates the lfsr program from its object files? (Give its line number.)
$(LFSR_PROG): $(LFSR_OBJS) # 行号:假设为第 24 行
$(CC) $(CFLAGS) -g -o $@ $^ $(LDFLAGS) # 行号:第 25 行(实际编译命令)
1
2
2
# Exercise 1: Bit Operations
编写程序完成 get_bit
,set_bit
,flip_bit
#include <stdio.h>
#include "bit_ops.h"
// Return the nth bit of x.
// Assume 0 <= n <= 31
unsigned get_bit(unsigned x,
unsigned n) {
// YOUR CODE HERE
return (x >> n) & 1;
// Returning -1 is a placeholder (it makes
// no sense, because get_bit only returns
// 0 or 1)
return -1;
}
// Set the nth bit of the value of x to v.
// Assume 0 <= n <= 31, and v is 0 or 1
void set_bit(unsigned * x,
unsigned n,
unsigned v) {
// YOUR CODE HERE
if (v == 1)
*x |= (1 << n);
else
*x &= ~(1 << n);
}
// Flip the nth bit of the value of x.
// Assume 0 <= n <= 31
void flip_bit(unsigned * x,
unsigned n) {
(*x) ^= (1 << n);
// YOUR CODE HERE
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Exercise 2: Linear Feedback Shift Register
这个 Exercise 要求你实现一个线性反馈移位寄存器,这个图好像挂了
根据描述很容易
void lfsr_calculate(uint16_t *reg) {
/* YOUR CODE HERE */
int lfsr = *reg;
uint16_t bit = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 5)) & 1;
lfsr = (lfsr >> 1) | (bit << 15);
*reg = lfsr;
}
1
2
3
4
5
6
7
2
3
4
5
6
7