본문 바로가기
Computer Architecture/컴퓨터구조[01]

[컴퓨터구조] 2. Instructions: Language of the Computer (3)

by leziwn.cs 2023. 9. 25.
Data Directives (명령어)

Data Directives (명령어)

 


Q. malloc (memory allocation)?

malloc은 C 및 C++ 프로그래밍 언어에서 메모리를 동적으로 할당하는 함수입니다. "malloc"은 "memory allocation"의 줄임말로, 프로그램 실행 중에 필요한 메모리 공간을 할당하는 데 사용됩니다. 메모리 할당은 데이터 구조를 생성하거나 가변 크기의 데이터를 저장하기 위해 매우 유용합니다.
malloc 함수는 특정 크기의 메모리 블록을 할당하고 그 블록의 시작 주소를 반환합니다. 이 할당된 메모리 블록은 프로그램이 사용되는 동안 유지되며, 사용이 끝나면 해제해야 합니다. 메모리 누수를 방지하기 위해 free 함수를 사용하여 할당된 메모리를 해제할 수 있습니다.
 
malloc 함수의 기본 원형은 다음과 같습니다:

void* malloc(size_t size);

여기서 size는 할당하려는 바이트 수를 나타내며, 함수는 할당된 메모리 블록의 시작 주소를 void 포인터로 반환합니다.
 
예를 들어, 다음은 정수 10개를 저장할 수 있는 메모리 블록을 할당하고 해당 주소를 포인터에 저장하는 방법입니다:

int* myArray = (int*)malloc(10 * sizeof(int));

 
메모리를 할당한 후에는 필요한 작업을 수행한 후에 free 함수를 사용하여 해당 메모리를 해제해야 합니다. 메모리를 해제하지 않으면 메모리 누수가 발생할 수 있으며, 이는 프로그램의 성능에 영향을 미칠 수 있습니다.


 

Example 3 - Hello World

Example 3 - Hello World

 

They are the same

They are the same

 


Register Operands

▶ Arithmetic instruction: Use register operands.
▶ MIPS has a 32 x 32-bit register file.

  • Register number: 0 ~ 31
  • word: 4-byte (32-bit)

▶ Assembler names:

  • $t0, $t1, ..., $t9: temporary values
  • $s0, $s1, ..., $s7: saved values

 

Register Operand - Example
f = (g + h) - (i + j);

// f, ..., j in $s0, ..., $s4

▶ Compiled MIPS code:

  • add $t0, $s1, $s2
  • add $t1, $s3, $s4
  • add $s0, $t0, $t1

 

Memory Operands

▶ To apply arithmetic operands:

  • Load: memory --> register
  • Store: register --> memory

▶ Memory is byte addressed.
▶ Words are aligned in memory.

  • Address in memory: multiple of 4 (1 word = 4 bytes)

▶ MIPS is Big Endian
: MSB(Most Significant Byte) at least address of a word.
 

Big Endian vs. Little Endian
  • Big Endian: MSB --> least address in memory.
  • Little Endian: MSB --> largest address in memory.

Big Endian vs. Little Endian

 

Memory Operand - Example 1
g = h + A[8];

// g in $s1
// h in $s2
// base address of A in $s3

▶ Compiled MIPS code:

  • lw $t0, 32($s3)
  • add $s1, $s2, $t0

 

Memory Operand - Exampe 2
A[12] = h + A[8]

// h in $s2
// base address of A in $s3

▶ Compiled MIPS code:

  • lw $t0, 32($s3)
  • add $t0, $s2, $t0
  • sw $t0, 48($s3)

 


Registers vs. Memory
  • Registers are faster to access than memory.
  • Operating on memory data requires loads and stores.
  • Compiler must use registers for variables as much as possible.

 

Immediate Operands
  • Constant data specified in an instruction.
  • No subtract immediate instruction.
addi  $s3, $s3, 4   // $s3 = $s3 + 4

 

The Constant Zero
  • MIPS register 0($zero) is the constant 0.

--> Useful for common operations. 

// Move between registers

add $t2, $s1, $zero   // $t2 = $s1 + 0

 


Unsigned Binary Integers

Unsigned Binary Integers

 

2s-Complement Signed Integers

2s-Complement Signed Integers

▶ Bit 31 is sign bit

  • 1: -
  • 0: +

Some specific numbers

 

Signed Negation (2의 보수법 계산 방법)
  1. 숫자 다 뒤집기
  2. +1
  3. 부호 붙이기

 

Sign Extension
  • Unsigned: 앞에 0을 붙인다.
  • Signed: 앞에 sign bit을 붙인다.

 


SBY 3-2