From dd1c090713908aa0ddc4b133f6df90c6fd52b9ec Mon Sep 17 00:00:00 2001 From: Nayoung Kim <skdud1201@ajou.ac.kr> Date: Sun, 23 Jan 2022 15:43:03 +0000 Subject: [PATCH] update lec09 --- README.md | 187 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 187 insertions(+) diff --git a/README.md b/README.md index 2f14fcb..c25dadd 100644 --- a/README.md +++ b/README.md @@ -1328,3 +1328,190 @@ int main() paster(9); // output : token9 = 9 } ``` + +### Macro Definition (2) +* Multiple lines + * 여러 줄 define 하고 싶을 때 +``` + #define NUMBERS 1, \ + 2, \ + 3 + + int x[] = { NUMBERS }; + //int x[] = {1,2,3}; +``` +* Define Where? +``` + foo = X; + #define X 4 + bar = X; // foo = X and bar = 4 + + #define TABLESIZE BUFSIZE + #define BUFSIZE 1024 + // TABLESIZE == BUFSIZE ==1024 + + #define BUFSIZE 1020 + #define TABLESIZE BUFSIZE + #undef BUFSIZE + #define BUFSIZE 37 // TABLESIZE = 37 +``` + +### Macro Definition (3) - Function like Macro +* if문 보다 성능이 좋음 +``` + #define min (X, Y) ((X) < (Y) ? (X) : (Y)) + x = min(a,b); -> ((a) < (b) ? (a) : (b)); + y = min(1,2); -> ((1) < (2) ? (1) : (2)); + z = min(a + 28, *p) -> z = ((a+28) < (*p) ? (a+28) : (*p)); +``` + +### Macro Definition (4) - Stringization and concat +* Stringization + * Parameter leading with # +``` + #define str(s) #s + #define foo 4 + str(foo) // "foo" +``` +* Concatenation +``` + #define COMMAND(name) { #name, name##_command } + COMMAND(quit) // { "quit", quit_command } +``` + +### Macro Definition (5) - Variadic +* #define eprintf(...) fprintf (stedrr, __VA_ARGS__) +``` + eprintf ("%s:%d: ", input_file, lineno) + -> fprintf (stderr, "%s:%d: ", input_file, lineno) + + #define eprintf(format, ...) fprintf (stderr, format __VA_OPT__(,) __VA_ARGS__) +``` + +## **Lecture 09** + +### Linux Command +1. cc -g + * gdb에게 제공하는 정보를 바이너리에 삽입 + * -g 옵션을 사용하지 않고 gdb로 디버깅하면 역어셈블리 코드에서 어셈블리 코드로만 디버깅 가능 + +2. cc -pg + * 프로파일을 위한 코드 삽입 + * -pg 옵션으로 컴파일 → gmon.out(프로파일 정보) → gprof로 gmon.out 파일 분석 + +### gdb Command +* 실행파일과 소스파일이 같은 디렉토리에 있어야 제대로 동작 +1. gdb (실행파일) + * 시작 명령 + +2. run + * 프로그램 실행 + +3. break (line or *address) + * 정지 명령 + * break n : n번째 라인에서 정지 (정지점 생성) + * break Add : Add가 포함된 라인에서 정지 (정지점 생성) + +4. clear + * 정지점 삭제 + * clear n : n번째 라인 정지점 삭제 + +5. delete breakpoints + * 정지점 모두 지움 + +6. step + * 현재 라인 수행 후 정지 + * 함수 호출 시 함수 안으로 들어감 + * step n : step 명령 n번 수행 + +7. next + * 현재 라인 수행 후 정지 + * 함수 호출 시 함수 다음 라인으로 넘어감 + * next n : next 명령 n번 실행 + +8. print + * 주소나 변수 등을 출력 + +9. list + * 소스 파일 내용 출력 + * list a, b : a ~ b번째 라인의 소스 출력 + +10. continue + * 다음 정지점까지 코드 실행 + +11. watch + * 해당 변수에 watchpoint를 설정하여 변수가 바뀔 때마다 현재 값을 출력함 + +12. info reg + * 특정 레지스터의 정보 출력 + +13. set + * 특정 메모리 혹은 레지스터에 값을 지정 + +14. finish + * 현재 함수를 수행하고 빠져나감 + +15. Quit (q) + * 종료 명령 + +### CPU +* ALU (Arithmetic / Logic Unit) +* CU (Control Unit) + +### 고정소수점 관련 코드 +* 부동소수점 대신 고정소수점을 사용하는 이유 + * 부동소수점은 컴퓨터에 따라 연산에 많은 에너지를 필요로 함 + +* S32_31 => signed long long +* S16_15 => signed int +* S8_7 => signed short +* S4_3 => signed character + +``` +#include <stdio.h> +// #### #### #### #### . #### #### #### #### +// S 15 . 16 +#define FX_Q_NUM 16 +#define FX_2_MINUS_16 1.52587890625e-05F +#define FX_2_PLUS_16 (1<<16) +#define FX_S_15_16 11516 +#define FX_SYSTEM FX_S_15_16 + +typedef int fixed32; + +fixed32 fromFloat(float fa) +{ + return (fixed32) (fa * FX_2_PLUS_16); +} + +float toFloat(fixed32 xa) +{ + return ((float) (xa)) * FX_2_MINUS_16; +} + +fixed32 fxAdd(fixed32 a, fixed32 b) +{ + return fromFloat(toFloat(a)+toFloat(b)); +} + + +fixed32 fxAdd2(fixed32 a, fixed32 b) +{ + return a+b; +} + +int main() +{ + int i = 0; + int ia, ib, ic, ic2; + float fa; + fscanf(stdin, "%d %d", &ia, &ib); + for (i = 0; i < 64*256*256*256; i+=(256*256)){ + ic = fxAdd(i, i); + ic2 = fxAdd2(i, i); + fprintf(stdout, "%f + %f : %f, %f diff = %d \n", toFloat(i), toFloat(i), toFloat(ic), toFloat(ic2), ic-ic2); + } + + fprintf(stdout, "%d + %d : %d \n", ia, ib, ic); +} +``` -- GitLab