์ฐ์ ๊ธฐ๋ก ๐ช
์ปดํ์ผ ๊ณผ์ ๋ณธ๋ฌธ
์ด ๊ธ์์๋ ์ฐ๋ฆฌ๊ฐ ์ง ํ๋ก๊ทธ๋จ์ด ์ด๋ป๊ฒ ์ปดํ์ผ๋์ด ์คํ๋๋์ง ์ดํด๋ณผ ๊ฒ์ด๋ค.
๋จผ์ ์๋์ ๊ฐ์ ํ๋ก๊ทธ๋จ์ด ์๋ค๊ณ ๊ฐ์ ํด๋ณด์.
#include <stdio.h>
#define MAX 5
int main(){
printf("hello world\n"); //hello world๋ฅผ ์ถ๋ ฅ
int a = MAX;
return 0;
}
์ดํ ์ฐ๋ฆฌ๊ฐ gcc hello.c ๋ผ๋ ๋ช ๋ น์ด๋ฅผ ์ ๋ ฅํ๋ฉด ์๋์ ๊ฐ์ด a.outํ์ผ์ด ์์ฑ๋๋ค.
์ด a.outํ์ผ์ ์คํ ๊ฐ๋ฅํ ๋ชฉ์ ํ์ผ(executable object file)์ด๋ค.
์ฐ๋ฆฌ๊ฐ ๋ณด๊ธฐ์๋ hello.cํ์ผ์ด ๋ฐ๋ก a.outํ์ผ๋ก ๋ณํ ๊ฒ์ผ๋ก ๋ณด์ฌ์ง์ง๋ง ์ฌ์ค ๋ด๋ถ์ ์ผ๋ก ์ ์ฒ๋ฆฌ, ์ปดํ์ผ, ์ด์ ๋ธ๋ฆฌ, ๋งํน ๊ณผ์ ์ ๋ชจ๋ ๊ฑฐ์น ๊ฒ์ด๋ค. ์ด์ ๋ถํฐ ๊ทธ ๊ณผ์ ์ ํ๋ํ๋ ์ดํด๋ณด๋๋ก ํ์.
์ ์ฒ๋ฆฌ(hello.c→hello.i)
์ ์ฒ๋ฆฌ ๊ณผ์ ์์๋ ์ฃผ์ ์ ๊ฑฐ, ํค๋ ํ์ผ ์ฝ์ , ๋งคํฌ๋ก ์นํ ๋ฑ์ ์์ ์ ํ๋ค. ์ฆ ์ ์ฒ๋ฆฌ ๊ณผ์ ์ ๊ฑฐ์น ํ์ hello.cํ์ผ์ ์๋์ ๊ฐ์ด ๋ณํ๋ ๊ฒ์ด๋ค.
...
extern int ftrylockfile (FILE * __stream) __attribute__((__nowthrow__,__leaf__))
...
int main(){
printf("hello world\n");
int a = 5;
return 0;
}
#include <stdio.h>์ #define ๋งคํฌ๋ก๊ฐ ์นํ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค. ๋ํ ์ฃผ์๋ ์ญ์ ๋์๋ค.
์ด์ ์ค์ ๋ก ํ์ธํด๋ณด์. gcc -E hello.c -o hello.i
๋ช
๋ น์ด๋ฅผ ์
๋ ฅํ๊ณ cat hello.i
๋ช
๋ น์ด๋ฅผ ํตํด hello.iํ์ผ์ ์ฝ์ด๋ณด๋๋ก ํ์.
์์ํ๋๋๋ก ์ ์ฒ๋ฆฌ ๊ณผ์ ์ด ์ ์ฒ๋ฆฌ๋์๋ค. ์ฐธ๊ณ ๋ก ์์์ ์ ๋ ฅํ ๋ช ๋ น์ด๋ ์ ์ฒ๋ฆฌ ๊ณผ์ ๋ง ๊ฑฐ์น ๊ฒฐ๊ณผ๋ฌผ์ hello.i๋ผ๋ ์ด๋ฆ์ผ๋ก ์ ์ฅํ๋ผ๋ ๋ป์ด๋ค.
- o
filename
:filename
์ ๊ฐ์ง๋ ๊ฒฐ๊ณผ๋ฌผ์ ๋ง๋ ๋ค. - E: ์ ์ฒ๋ฆฌ ๊ณผ์ ๊น์ง๋ง ๋ง์น๊ณ ๊ฒฐ๊ณผ๋ฌผ ์ ์ฅ.
- S: ์ปดํ์ผ ๊ณผ์ ๊น์ง๋ง ๋ง์น๊ณ ์ด์ ๋ธ๋ฆฌ์ด ๊ฒฐ๊ณผ๋ฌผ ์ ์ฅ.
- c: ์ด์ ๋ธ๋ฆฌ ๊ณผ์ ๊น์ง๋ง ๋ง์น๊ณ ์ค๋ธ์ ํธ ํ์ผ ๊ฒฐ๊ณผ๋ฌผ์ ์ ์ฅ.
์ปดํ์ผ(hello.i→hello.s)
๋ค์์ ์ปดํ์ผ ๊ณผ์ ์ด๋ค. ์ปดํ์ผ ๊ณผ์ ์์๋ ์์คํ์ผ์ ์ด์ ๋ธ๋ฆฌ์ด๋ก ๋ฐ๊พผ๋ค. ๋ ๋ฌธ๋ฒ ๊ฒ์ฌ ๋ฐ staticํ ์์ญ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํ๋ค.
๋ง์ฝ ์๋์ ๊ฐ์ ๊ฐ๋จํ ์ฝ๋๊ฐ ์๋ค๋ฉด
int main(){
int a = 5;
return 0;
}
์ด๋ฐ์์ผ๋ก ๋๋ชจ๋ ์ฌ๋ณผ์ ์ด์ฉํด ๊ฐ์ ๋ฐ๊ฟ์ค๋ค.
main:
pushq %rbp
movq %rsp, %rbp
movl $5, -4(%rbp)
movl $0, %eax
popq %rbp
ret
์ค์ ๋ก ์ ์ฝ๋๋ฅผ ์ ๋ ฅํ ๋ค ์ปดํ์ผ ํด๋ณด๋ฉด ์๋์ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์๋ค.
gcc -S hello.c -o hello.s
์ด์ ๋ธ๋ฆฌ(hello.s → hello.o)
๊ทธ ๋ค์์ผ๋ก๋ ์ด์ ๋ธ๋ฆฌ ๊ณผ์ ์ด ์คํ๋๋๋ฐ ์ด์ ๋ธ๋ฆฌ์ด๋ฅผ ๋ชฉ์ ํ์ผ(object file)๋ก ๋ฐ๊พธ์ด ์ฃผ๋ ๊ณผ์ ์ด๋ค. ์ฌ๊ธฐ์๋ถํฐ๋ ํ ์คํธ๋ฅผ ์ฝ๋ cat๋ช ๋ น์ด๊ฐ ์๋ objdump ๋ช ๋ น์ด๋ฅผ ์ด์ฉํด์ผ ํ๋ค. -d ์ต์ ์ ์ด์ ๋ธ๋ฆฌ๋ก ์ญ๋ณํํด ๊ฐ์ ์ถ๋ ฅํ๋ผ๋ ๋ป์ด๋ค.
gcc -c hello.c -o hello.o
objdump -d hello.o
๋งํน
์์์ ๋ชฉ์ ํ์ผ์ ๋ง๋ค์์ผ๋ ๋ฐ๋ก ์คํ์ด ๊ฐ๋ฅํ ๊ฒ ๊ฐ์ง๋ง ์์ง ๋งํน ๊ณผ์ ์ ๊ฑฐ์ณ์ผ ํ๋ค. ์์์ ์ฐ๋ฆฌ๊ฐ ์์ฑํ hello.c์ฝ๋๋ฅผ ๋ค์ ๋ณด๋๋ก ํ์.
#include <stdio.h>
#define MAX 5
int main(){
printf("hello world\n"); //hello world๋ฅผ ์ถ๋ ฅ
int a = MAX;
return 0;
}
printfํจ์๋ standard C libraryํจ์์ด๋ค. ๊ทธ๋์ printf.o๋ผ๋ ํ์ผ์ ํจ์์ ๋ด์ฉ์ด ๋ช ์๋์ด์๋ค. ๋งํน๊ณผ์ ์ ์ด๋ฌํ ํ์ผ ๋ด์ ์ ์๋์ด์์ง ์์ ํจ์์ ๋ด์ฉ์ ์ฐ๊ฒฐํด์ฃผ๋ ์ญํ ์ ํ๋ค. ์ด์ ์ค์ ๋งํน ๊ณผ์ ์ ๊ฑฐ์น๊ธฐ ์ ๊ณผ ํ๋ฅผ ์ง์ ๋น๊ตํด๋ณด๋๋ก ํ์.
์ต๋ํ ๊ฐ๋จํ๊ฒ test.cํ์ผ์ ์๋์ ๊ฐ์ด ๊ตฌ์ฑํ๊ณ
#include <stdio.h>
int main(){
printf("hello world!");
return 0;
}
์๋ test.cํ์ผ์ ์ด์ ๋ธ๋ฆฌ ๊ณผ์ ๊น์ง๋ง ๊ฑฐ์น ๋ค(๋งํน ์ ) ์ฌ๋ณผ ํ ์ด๋ธ์ ์ดํด๋ณด์.
gcc -c test.c -o test.o
nm test.o // ์ฌ๋ณผ ํ
์ด๋ธ์ ๋ณด๋ ๋ช
๋ น์ด
์ ๊ธฐ์ T๋ ํ ์คํธํ์ผ์์ ํด๋น ์ฌ๋ณผ์ ์๋ฏธ๋ฅผ ์ฐพ์๋ค๋ ๋ป์ด๊ณ , U๋ undefined๋ฅผ ์๋ฏธํ๋ค. ์ด์ ๋งํน๊ณผ์ ๊น์ง ๊ฑฐ์น ๋ค ํด๋น ํ์ผ์ ์ฌ๋ณผ ํ ์ด๋ธ์ ์ดํด๋ณด์.
๊ธฐ๋ ๊ฒฐ๊ณผ๋ _printf์ U๊ฐ ์ฌ๋ผ์ง๊ฑฐ๋ ์์ ํด๋น ๋ช ๋ น์ด์ ์ฃผ์๊ฐ ๊ธฐ์ ๋๋๊ฒ์ธ๋ฐ ์ ๋์ง ์๋๋ค. ์ด ๋ถ๋ถ์ ์ถํ ์์ ํ๊ฒ ๋ค.
'Computer Science > ์ปดํจํฐ ๊ตฌ์กฐ๋ก ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
CH 1. Computer Architecture (0) | 2024.04.22 |
---|---|
์์ฐจํ๋ก vs ์กฐํฉํ๋ก(feat ํ๋ฆฝํ๋กญ) (0) | 2023.08.29 |