Assembly Language & x86 Low-Level Systems Programming · 강의

하드웨어와 직접 상호 작용

커널 수준 코드에서 입출력 포트와 메모리 매핑 입출력을 사용하여 하드웨어에 직접 접근하는 기법을 살펴봅니다.

레슨 3/411개 단계

하드웨어와 직접 상호 작용은(는) CoddyKit의 무료 Assembly Language & x86 Low-Level Systems Programming 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Assembly Language & x86 Low-Level Systems Programming 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Assembly Language & x86 Low-Level Systems Programming 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Why Talk Directly to Hardware?

In this lesson, we'll dive into how a CPU directly communicates with hardware devices. While your operating system usually handles this, kernel-level code and device drivers need to talk directly to components like network cards, graphics processors, or storage controllers.

This direct interaction is a powerful, low-level capability that forms the backbone of how your computer functions.

Two Ways to Talk to Hardware

The x86 architecture provides two main methods for the CPU to communicate with peripheral devices:

  • I/O Ports: A dedicated, separate address space.
  • Memory-Mapped I/O (MMIO): Device registers appear as locations within the CPU's main memory address space.

Both allow the CPU to read from and write to device registers, but they use different mechanisms and instructions.

Understanding I/O Ports

I/O ports are a special 64KB address space, completely separate from the main memory addresses. Devices map their internal registers to specific port addresses.

Think of them like a set of mailboxes, each with a unique number, where the CPU and devices can exchange small pieces of data. These are often used by older or simpler devices, or for basic control functions.

Reading from I/O Ports: The IN Instruction

To read data from an I/O port, x86 assembly uses the IN instruction. This instruction takes the port address (usually in the DX register) and transfers data into an accumulator register (AL, AX, or EAX).

  • IN AL, DX: Reads 1 byte from port DX into AL.
  • IN AX, DX: Reads 2 bytes from port DX into AX.
  • IN EAX, DX: Reads 4 bytes from port DX into EAX.
; Read a byte from I/O port 0x60 (e.g., keyboard data)
MOV DX, 0x60    ; Load port address into DX
IN AL, DX       ; Read 1 byte from port 0x60 into AL
; AL now holds the data from port 0x60

Writing to I/O Ports: The OUT Instruction

To write data to an I/O port, we use the OUT instruction. It sends data from an accumulator register to the specified port address (again, typically in DX).

  • OUT DX, AL: Writes 1 byte from AL to port DX.
  • OUT DX, AX: Writes 2 bytes from AX to port DX.
  • OUT DX, EAX: Writes 4 bytes from EAX to port DX.
; Write a byte 0xFA to I/O port 0x64 (e.g., keyboard command)
MOV DX, 0x64    ; Load port address into DX
MOV AL, 0xFA    ; Load data to write into AL
OUT DX, AL      ; Write 0xFA to port 0x64

A Glimpse at I/O Port Interaction

Here's a conceptual example of how IN and OUT might be used together to interact with a simple device, like a UART (Universal Asynchronous Receiver/Transmitter) for serial communication. Remember, these operations require kernel privileges!

; Conceptual: Check UART status, then send a character
MOV DX, 0x3F8 + 5 ; Port address for UART Line Status Register (LSR)
.wait_tx_ready:
  IN AL, DX         ; Read LSR
  TEST AL, 0x20     ; Check Transmit Empty (bit 5)
  JZ  .wait_tx_ready; Loop if not ready

MOV DX, 0x3F8     ; Port address for UART Data Register
MOV AL, 'K'       ; Data to send ('K')
OUT DX, AL        ; Write 'K' to the UART

Memory-Mapped I/O (MMIO)

Memory-Mapped I/O (MMIO) is a more modern and common way for the CPU to interact with devices. Instead of a separate I/O port space, device registers are mapped directly into the CPU's physical memory address space.

This means the CPU can access device registers using the same load and store instructions (like MOV) it uses for regular RAM, making it often faster and more flexible for complex devices like GPUs and network cards.

MMIO: Using MOV for Hardware Control

Since MMIO locations appear as regular memory addresses, you don't need special IN/OUT instructions. You simply use standard memory access instructions like MOV to read from or write to these addresses.

The operating system kernel is responsible for setting up these memory mappings so that driver code can access them.

; Conceptual: Accessing a device register via MMIO
; Assume MMIO_BASE_ADDR is a virtual address mapped to a physical device register

; Read a 32-bit value from a device register at MMIO_BASE_ADDR + 0x10
MOV EAX, [MMIO_BASE_ADDR + 0x10]

; Modify the value (e.g., increment it)
ADD EAX, 1

; Write the modified value back to the device register
MOV [MMIO_BASE_ADDR + 0x10], EAX

MMIO vs. I/O Ports: A Comparison

Let's summarize the key differences:

  • I/O Ports: Separate address space, uses IN/OUT instructions, often for simpler or legacy devices (e.g., PIC, PIT).
  • MMIO: Part of the main memory address space, uses standard MOV instructions, preferred for modern, high-speed, and complex devices (e.g., GPUs, NICs).

MMIO generally offers better performance and easier programming due to using the CPU's optimized memory access mechanisms.

Direct Hardware Access: Kernel's Domain

It's crucial to understand that direct hardware access, whether via I/O ports or MMIO, is a highly privileged operation. User-mode programs are prevented from performing these actions directly for security and system stability.

The operating system kernel acts as the gatekeeper, providing controlled interfaces (like system calls or device drivers) for user applications to interact with hardware safely.

Lesson Summary: Interacting with Hardware

You've learned about the two primary ways to directly interact with hardware in x86 assembly from a kernel perspective:

  • I/O Ports: A separate address space accessed with IN and OUT instructions.
  • Memory-Mapped I/O (MMIO): Device registers mapped into main memory, accessed with standard MOV instructions.

Remember that these powerful techniques are reserved for kernel-level code and device drivers to maintain system integrity and security.

무료로 시작

AI 튜터와 함께 Assembly을(를) 배우세요 — 무료

브라우저에서 실제 코드를 작성하고 실행하며, 24/7 AI 튜터로부터 즉각적인 도움을 받고, 웹이나 앱에서 중단한 부분부터 계속 학습하세요.

코스
12
레슨
48

자주 묻는 질문

“하드웨어와 직접 상호 작용” 강의는 무료인가요?

네 — “하드웨어와 직접 상호 작용” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Assembly Language & x86 Low-Level Systems Programming 강의 전체를 잠금 해제할 수 있습니다. Assembly Language & x86 Low-Level Systems Programming 강의에는 총 4개의 강의가 포함되어 있습니다.

“하드웨어와 직접 상호 작용”에서 뭘 배우나요?

커널 수준 코드에서 입출력 포트와 메모리 매핑 입출력을 사용하여 하드웨어에 직접 접근하는 기법을 살펴봅니다. 브라우저에서 직접 실행하는 실습 코드로 Assembly Language & x86 Low-Level Systems Programming을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Assembly Language & x86 Low-Level Systems Programming을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Assembly Language & x86 Low-Level Systems Programming은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.

“하드웨어와 직접 상호 작용” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Assembly Language & x86 Low-Level Systems Programming 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Assembly Language & x86 Low-Level Systems Programming 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 커널 공간 소개
  2. 간단한 장치 드라이버 작성
  3. 하드웨어와 직접 상호 작용
  4. 커널 공간의 동기화와 동시성
← Assembly Language & x86 Low-Level Systems Programming(으)로 돌아가기