๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Programming/C

11. ํฌ์ธํ„ฐ

by ๊ธฐ๋”ฉ 2023. 10. 19.
728x90

๐Ÿ’ก endian := byte order ๋ฉด์ ‘ ๋•Œ ์ž์ฃผ ๋ฌผ์–ด๋ด„
     (MSB์ชฝ) 0x12345678 (LSB)
      little endian in x86 → 78 56 34 12

      big endian in M68K → 12 34 56 78

      ํ†ต์‹ ์—์„œ๋Š” ๋ฌธ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ์–ด TCP/IP์—์„œ๋Š” ๋ฐ”๊ฟˆ

 

&a : a ๋ณ€์ˆ˜์˜ ์ฃผ์†Œ So, &๋ณ€์ˆ˜

int* pointer; ํฌ์ธํ„ฐ๋ณ€์ˆ˜ ์„ ์–ธ
   //ํฌ์ธํ„ฐ๋Š” ์ฒซ ์ฃผ์†Œ๋งŒ ์ฝ์œผ๋ฏ€๋กœ ์–ผ๋งˆ๋‚˜ ์ฝ์–ด์˜ฌ์ง€ ํƒ€์ž… ์ ์–ด์ค˜์•ผ ํ•จ

*pointer : (๊ฐ„์ ‘ ์ฐธ์กฐ ์—ฐ์‚ฐ์ž) ํฌ์ธํ„ฐ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’
              So, *์ฃผ์†Œ

 

โœ… ๋ฉ”๋ชจ๋ฆฌ ๋‹จ์œ„ : byte
       ์ฃผ์†Œ ํ•˜๋‚˜ : 1byte = 8bit → 00 ~ ์ตœ๋Œ€ FF = 1111 1111
       32bit cpu์˜ ์ตœ๋Œ€ ๋ฉ”๋ชจ๋ฆฌ ์šฉ๋Ÿ‰ 4GB → 32bit ์ฃผ์†Œ : 0xFFFF FFFF
       4bit = 1 nibble

 

#include <stdio.h>

int main(void)
{
	int a; // ์œˆ๋„์šฐ OS๊ฐ€ ์ฃผ์†Œ(ํฌ์ธํ„ฐ๊ฐ’)์„ ํ• ๋‹นํ•ด์คŒ
	int* pointer;
	int b;
	int* pointer_b;

	a = 0x12345678;
	pointer = &a;
	b = 3;
	pointer_b = &b;

	//d๋Š” 10์ง„์ˆ˜๋กœ. x๋กœ ํ•˜๋ฉด ๊ทธ๋Œ€๋กœ ๋‚˜์˜ด
	printf("%d, %p, %d, %p\\n", a, &a, *pointer, pointer);
	// 305419896, 0000007C165AF7A4, 305419896, 0000007C165AF7A4
	printf("%d, %p, %d, %p", b, &b, *pointer_b, pointer_b);
	// 3, 0000007C165AF7E4, 3, 0000007C165AF7E4

printf("ํฌ์ธํ„ฐ ๋ณ€์ˆ˜์˜ ํฌ๊ธฐ: %zd ๋ฐ”์ดํŠธ\\n", sizeof(pointer));
	// 8๋ฐ”์ดํŠธ ;64๋น„ํŠธ ์ฃผ์†Œ์ฒด๊ณ„

	return 0;
}
  • ํฌ์ธํ„ฐ ์“ฐ๋ฉด ๋ฉ”๋ชจ๋ฆฌ(๋ณต์‚ฌ) ๋œ ์”€ ← ์‹œ์ž‘ ์ฃผ์†Œ๋งŒ ์“ฐ๋‹ˆ๊นŒ
  • ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น์€ OS๊ฐ€ → ์‹คํ–‰๋งˆ๋‹ค ๋ฐ”๋€œ

๐Ÿ’ก PC์—์„œ pointer = 100; ํฌ์ธํ„ฐ์— ์ง์ ‘ ํ• ๋‹นํ•˜๋ฉด ์›๋žœ OS๊ฐ€ ํ•ด์•ผ ํ•˜๋Š” ์ผ์ด๋ฏ€๋กœ ์—๋Ÿฌ ๋‚จ.

     //๋นŒ๋“œ๋Š” ๋˜์ง€๋งŒ, ์‚ฌ์šฉํ•˜๋ฉด ์—๋Ÿฌ(์—‘์„ธ์Šค ๊ฑฐ๋ถ€)

     ๋ฐ˜๋ฉด, ์•„๋‘์ด๋…ธ ๊ฐ™์€ ์ž„๋ฒ ๋””๋“œ๋Š” ๋ณธ์ธ์ด ๊ฑด๋“œ๋ ค์•ผ ํ•ด์„œ ์˜คํžˆ๋ ค ์‚ฌ์šฉ

 

ํฌ์ธํ„ฐ ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™” = NULL

#include <stdio.h>
//void๋ฉด ๋ณ€์ˆ˜ ๋ถ€๋ฅผ ๋•Œ๋งˆ๋‹ค ๋ณ€์ˆ˜ ํƒ€์ž… ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Œ

int main(void)
{
	int i = 3000;
	int* p = NULL; //์•„๋ฌด๊ฒƒ๋„ ์•ˆ ๊ฐ€๋ฅดํ‚ด. 0์˜๋ฏธ

	p = &i;

	printf("i = %d\\n", i); //3000
	printf("&i = %p\\n\\n", &i); //000000EED0F0F944

	printf("p = %p\\n", p); //000000EED0F0F944
	printf("p = %d\\n", *p); //3000

	return 0;
}

 

ํฌ์ธํ„ฐ ์ด์šฉํ•ด์„œ ์›๋ž˜ ๊ฐ’ ๋ฐ”๊พธ๊ธฐ

#include <stdio.h>

int main(void)
{
	int i = 10;
	int* p;

	p = &i;
	printf("i = %d\\n", i);

	*p = 20; //ํฌ์ธํ„ฐ p์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’i์— 20๋Œ€์ž…
	printf("i = %d\\n", i);

	return 0;
}

  • ์ดˆ๊ธฐํ™” ์•ˆ ๋œ ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ ๋จ.int *p; *p = 100; ํ•˜๋ฉด ์•ˆ ๋จ. int x; *p = &x; ํ•„์š”
  • i.e. ์–ด๋–ค ๊ฐ’์˜ ์ฃผ์†Œ์ธ์ง€ ๋ง ์•ˆ ํ•˜๊ณ 
  • ํฌ์ธํ„ฐ ํƒ€์ž…, ๋ณ€์ˆ˜ ํƒ€์ž… ์ผ์น˜ํ•ด์•ผ ํ•จ
  • byte access๊ฐ€ ๋‹ค๋ฆ„

 

ํฌ์ธํ„ฐ์™€ ๋ฐฐ์—ด

๋ฐฐ์—ด์€ & ํ‘œ์‹œ ์•ˆ ํ•ด๋„ ๋จ

์ฃผ์†Œ a a+1 a+2 a+3  int๋‹ˆ๊นŒ 4byte์”ฉ ์ฆ๊ฐ€
&a[0] &a[1] &a[2] &a[3]  
๋ณ€์ˆ˜์ด๋ฆ„ a[0] a[1] a[2] a[3]  
๋ณ€์ˆ˜๊ฐ’ 10 20 30 40  
*a *(a+1) *(a+2) *(a+3) a๋Š” ์ฃผ์†Œ๋‹ˆ๊นŒ 4byte์”ฉ ์ฆ๊ฐ€

ํฌ์ธํ„ฐ ํ˜•๋ณ€ํ™˜

#include <stdio.h>

int main(void)
{
	int data = 0x0A0B0C0D;
	char* pc;
	int* bb;

	pc = (char *)&data; //๊ฐ’ ํ•˜๋‚˜ ๊ฐ€์ ธ์˜ค๊ณ  1byte์”ฉ ์ด๋™
	bb = (int*)&data; //4๊ฐœ ๊ฐ€์ ธ์˜ค๊ณ  4byte์”ฉ ์ด๋™ 

	for (int i = 0; i < 4; i++) {
		printf("*(pc + %d) = %X\\n", i, *(pc + i));
	}
	for (int i = 0; i < 4; i++) {
		printf("*(bb + %d) = %X\\n", i, *(bb + i));
	}
	return 0;
}

ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ์ธ์ˆ˜ ์ „๋‹ฌ๋ฐฉ๋ฒ•

  1. ๊ฐ’์— ์˜ํ•œ ํ˜ธ์ถœ call by value
  • ํ•จ์ˆ˜๋กœ ๋ณต์‚ฌ๋ณธ์ด ์ „๋‹ฌ
  • ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ๋ฒ•
  • ๊ณ„์‚ฐ์ด๋‚˜ ๋™์ž‘์„ ์œ„ํ•ด ๊ฐ’๋งŒ ํ•„์š”ํ•  ๊ฒฝ์šฐ

   2. ์ฐธ์กฐ์— ์˜ํ•œ ํ˜ธ์ถœ call by reference

  • ํ•จ์ˆ˜๋กœ ์›๋ณธ์ด ์ „๋‹ฌ
  • C์—์„  ํฌ์ธํ„ฐ ์ด์šฉํ•˜์—ฌ ํ‰๋‚ด ๋‚ผ ์ˆ˜ o
  • ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํšจ์œจ์ ์ž„
  • func(int *px, *py){} ํ˜•ํƒœ
  • ํ•จ์ˆ˜ ์™ธ๋ถ€์—์„œ ์„ ์–ธ๋œ ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ๋•Œ
// ์ฐธ์กฐ์— ์˜ํ•œ ํ˜ธ์ถœ
#include <stdio.h>

void sum(int x, int y, int* value)
{
	*value = x + y;
} // ๋ฐ˜ํ™˜ ์—†์ด ๊ฒฐ๊ณผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ, return์„ ์จ์„œ ํ•˜๋‚˜๊ฐ€ ์•„๋‹Œ ์—ฌ๋Ÿฌ ๊ฒฐ๊ณผ ๊ฐ€๋Šฅ

int main(void)
{
	int a, b, value;

	a = 10;
	b = 20;
	value = 0;

	sum(a, b, &value); // a,b๋Š” ๋ณ€ํ•  ํ•„์š” ์—†์œผ๋‹ˆ ๊ทธ๋Œ€๋กœ
	// value๋Š” ์˜ํ–ฅ์„ ๋ฐ›์•„์•ผ ํ•˜๋‹ˆ ์ฃผ์†Œ๋ฅผ ์ค˜์„œ ๊ฐ’ ๋ฐ”๊พธ๊ณ  ์ฃผ์†Œ ๊ฐ€์ ธ์˜ค๊ธฐ
	printf("%d + %d = %d\\n", a, b, value);

	return 0;
}

scanf() ํ•จ์ˆ˜ : ์‚ฌ์šฉ์ž์—๊ฒŒ ์ฃผ์†Œ๋ฐ›์•„์„œ ๋ณ€์ˆ˜์— ๊ฐ’ ์ €์žฅ

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{
	int i;
	printf("์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์‹œ์˜ค: "); //์ฐฝ์— ์ถœ๋ ฅ
	scanf("%d", &i); //์ฐฝ์— ์ž…๋ ฅ
	printf("%d", i); //์ฐฝ์— ์ถœ๋ ฅ
	return 0;
}
//์˜ˆ์ œ
#include <stdio.h>

int main(void)
{
	int a[] = { 10, 20, 30, 40, 50 };
	printf("a = %u\\n", a);
	printf("a + 1 = %u\\n", a + 1);
	printf("*a= %d\\n", *a);
	printf("*(a+1) = 5%d\\n", *(a + 1));

	return 0;
}
//ํฌ์ธํ„ฐ๋ฅผ ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์‚ฌ์šฉ
#include <stdio.h>

int main(void)
{
	int a[] = { 10, 20, 30, 40, 50 };
	int* p;

	p = a;
	printf("a[0]=%d a[1]=%d a[2]=%d \\n", a[0], a[1], a[2]);
	printf("p[0]=%d p[1]=%d p[2]=%d \\n\\n", p[0], p[1], p[2]);

	p[0] = 60;
	p[1] = 70;
	p[2] = 80;

	printf("a[0]=%d a[1]=%d a[2]=%d \\n", a[0], a[1], a[2]);
	printf("p[0]=%d p[1]=%d p[2]=%d \\n", p[0], p[1], p[2]);
	
	return 0;
}
// ํฌ์ธํ„ฐ์™€ ํ•จ์ˆ˜์˜ ๊ด€๊ณ„
#include <stdio.h>

void sub(int b[], int n); //์ฐธ์กฐ ํ˜ธ์ถœ, ๊ฐ’ ํ˜ธ์ถœ

int main(void)
{
	int a[3] = { 1,2,3 };

	printf("%d %d %d\\n", a[0], a[1], a[2]);
	sub(a, 3);
	printf("%d %d %d\\n", a[0], a[1], a[2]);

	return 0;
}

void sub(int b[], int n)
{
	b[0] = 4;
	b[1] = 5;
	b[2] = 6;
}
/*
#include <stdio.h>
#define SIZE 5

void print_image(int image[SIZE][SIZE])
{
	for (int r = 0; r < SIZE; r++) {
		for (int c = 0; c < SIZE; c++) {
			printf("%03d ", image[r][c]);
		}
		printf("\\n");
	}
	printf("\\n");
}

void brighten_image(int image[SIZE][SIZE])
{
	int* p = &image[0][0];
	for (int r = 0; r < SIZE; r++) {
		for (int c = 0; c < SIZE; c++) {
			//image[r][c] += 10; //์ธ๋ฑ์Šค ํ‘œ๊ธฐ๋ฒ•
			*p += 10;
			p++;
		}
	}
}

int main(void)
{
	int image[SIZE][SIZE] = {
		{10,20,30,40,50},
		{10,20,30,40,50},
		{10,20,30,40,50},
		{10,20,30,40,50},
		{10,20,30,40,50} };

	print_image(image);
	brighten_image(image);
	print_image(image);

	return 0;
}
*/

ํฌ์ธํ„ฐ ์‚ฌ์šฉ์˜ ์žฅ์ 

  • ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ, ์ด์ง„ ํŠธ๋ฆฌ ๋“ฑ ํ–ฅ์ƒ๋œ ์ž๋ฃŒ ๊ตฌ์กฐo
  • ์ฐธ์กฐ์— ์˜ํ•œ ํ˜ธ์ถœ ํฌ์ธํ„ฐ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ, ํ•จ์ˆ˜ ์™ธ๋ถ€๋ณ€์ˆ˜ ๊ฐ’ ๋ณ€๊ฒฝo
  • ๋™์  ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น ๋™์  ๋ฉ”๋ชจ๋ฆฌ( in 11์žฅ) int *b, *c; b = malloc(40); 40๋ฐ”์ดํŠธ ํ• ๋‹น // ๋™์  ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น์„ ์œ„ํ•ด ๋‚˜๋ˆ ๋‘  for (int i = 0; i < 10; i++) { *(b+i) = i;} free(b); b๊ฐ€ ์‚ฌ๋ผ์ง //๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ

c = malloc(40);

free(c);

 ๐Ÿ’ก ์Šคํƒ์€ ์ •์ ํ• ๋‹น → ์ง€์—ญ๋ณ€์ˆ˜, ํ•จ์ˆ˜๋ณ€์ˆ˜ ๋“ฑ ์ €์žฅ
     ํž™์€ ๋™์ ํ• ๋‹น ๊ด€๋ฆฌ ์˜์—ญ → ex_ malloc, calloc, realloc
      free() ์•ˆ ํ•˜๋ฉด ๋ˆ„์ˆ˜. ๊ณ„์† ๋‚จ์•„์„œ ์—๋Ÿฌ

 

ํฌ์ธํ„ฐ์™€ ๋ ˆ์ง€์Šคํ„ฐ

์–ด์…ˆ๋ธ”๋ฆฌ์–ด mov r0, #20, ; //20์— r0๋ฅผ ์ €์žฅ

์ปดํŒŒ์ผ cond 0 0 1 opcode S Rn Rd rot_imm imm8

  • 32๋น„ํŠธ Cpu๊ฐ€ ๊ฐ€์ง€๋Š” ์ฃผ์†Œ์˜ ๋ฒ”์œ„๋„ 32๋น„ํŠธ๋กœ ๊ตฌ์„ฑ
    ๐Ÿ‘Œ GPIO : ์ž…์ถœ๋ ฅ์ œ์–ดํ•€
         UART : ์‹œ๋ฆฌ์–ผ ํ†ต์‹ ํ•˜๋Š” ํ•˜๋“œ์›จ์–ด ์žฅ์น˜ → ๋น„ํŠธ ๋‹จ์œ„ ๋ฐ์ดํ„ฐ ์ „์†ก,์ˆ˜์‹ 

          → ๋ช…๋ น์–ด, ์–ด๋“œ๋ ˆ์Šค, ์–ด๋“œ๋ ˆ์‹ฑ๋„ ๋ณดํ†ต 32bit
              0x00000000 ~ 0xFFFFFFFF (์ด 4GB)
              ์œ„ ๋ฒ”์œ„์—์„œ RAM, ROM๊ณผ ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ์™€ GPIO, UART ๊ฐ™์€ ์žฅ์น˜ ์ œ์–ด ์œ„ํ•ด ๋ ˆ์ง€์Šคํ„ฐ์˜ ์ฃผ์†Œ๋“ค์ด ํฌํ•จ๋จ

  • Memory MAP : 32๋น„ํŠธ CPU์˜ ๋ฉ”๋ชจ๋ฆฌ, ๋ ˆ์ง€์Šคํ„ฐ ์ฃผ์†Œ๊ฐ’ ๋ณด์—ฌ์ฃผ๋Š” ์ง€๋„
    → GPIO, UART, usb, ํƒ€์ด๋จธ … ๋“ฑ ์ œ์–ด?

ํฌ์ธํ„ฐ๋กœ ๋ฉ”๋ชจ๋ฆฌ์™€ ๋ ˆ์ง€์Šคํ„ฐ ๊ฑด๋“œ๋ฆฌ๊ธฐ

  • RAM ์ฃผ์†Œ 0x00000000 0x12345678๋ผ๋Š” 4byte๋ฅผ ์“ฐ๊ณ  ์‹ถ์Œ
    ((unsigned int) 0x100000000) = 0x12345678; //4byte๋‹ˆ๊นŒ int
  • ์–ด๋–ค ์ฃผ์†Œ์— ํŠน์ •ํ•œ ์‚ฌ์ด์ฆˆ๋งŒํผ ๊ฐ’์„ ์“ฐ๊ณ  ์‹ถ์Œ
    *(( ์‚ฌ์ด์ฆˆ์— ํ•ด๋‹นํ•˜๋Š” ๋ณ€์ˆ˜ํ˜• *) ์ฃผ์†Œ) = ๊ฐ’;

10๊ฐœ์˜ ๋ชจ๋“  ๋ ˆ์ง€์Šคํ„ฐ ๋ชจ์•„์„œ ํ•œ ๋ฒˆ์— ๊ด€๋ฆฌํ•˜๋ฉด ๋” ํŽธ๋ฆฌ

So, ์‹ค์ œ ์ฝ”๋“œ์—์„  “๊ตฌ์กฐ์ฒด” ์‚ฌ์šฉ

#define PERIPH_BASE           0x40000000UL
#define AHB1PERIPH_BASE       (PERIPH_BASE + 0x00020000UL)
#define GPIOA_BASE            (AHB1PERIPH_BASE + 0x0000UL)
#define GPIOA               ((GPIO_TypeDef *) GPIOA_BASE) // ๊ฒฐ๊ตญ 0x40020000

GPIOA -> ODR = 0xFF;

typedef struct
{
__IO uint32_t MODER;    /*!< GPIO port mode register,               Address offset: 0x00      */
__IO uint32_t OTYPER;   /*!< GPIO port output type register,        Address offset: 0x04      */
__IO uint32_t OSPEEDR;  /*!< GPIO port output speed register,       Address offset: 0x08      */
__IO uint32_t PUPDR;    /*!< GPIO port pull-up/pull-down register,  Address offset: 0x0C      */
__IO uint32_t IDR;      /*!< GPIO port input data register,         Address offset: 0x10      */
__IO uint32_t ODR;      /*!< GPIO port output data register,        Address offset: 0x14      */
__IO uint32_t BSRR;     /*!< GPIO port bit set/reset register,      Address offset: 0x18      */
__IO uint32_t LCKR;     /*!< GPIO port configuration lock register, Address offset: 0x1C      */
__IO uint32_t AFR[2];   /*!< GPIO alternate function registers,     Address offset: 0x20-0x24 */
} GPIO_TypeDef;

void HAL_GPIO_Init(GPIO_TypeDef  *GPIOx, GPIO_InitTypeDef *GPIO_Init);

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 

๐Ÿ‘Œ __IO : volatile unit32 : unsigned int 32๋น„ํŠธ

 

728x90

'Programming > C' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

15. ์ŠคํŠธ๋ฆผ๊ณผ ํŒŒ์ผ ์ž…์ถœ๋ ฅ  (1) 2023.10.19
13. ๊ตฌ์กฐ์ฒด  (0) 2023.10.19
12. ๋ฌธ์ž์—ด  (0) 2023.10.19
10. ๋ฐฐ์—ด  (0) 2023.10.19
9. ํ•จ์ˆ˜์™€ ๋ณ€์ˆ˜  (0) 2023.10.19