[OS] 06. Boot Sector Segmentation
서론
이번 강의에서는 가장 기본적인 Boot Sector에서 Segmentation을 고려하는 코드를 만들어 보겠습니다. 해당 Github 강의는 다음과 같습니다.
https://github.com/cfenollosa/os-tutorial/tree/master/06-bootsector-segmentation
cfenollosa/os-tutorial
How to create an OS from scratch. Contribute to cfenollosa/os-tutorial development by creating an account on GitHub.
github.com
이론
Segmentation
Segmentation은 03. Boot Sector Memory에서 설명했던 offset을 설정하는 개념입니다. Segmentation은 모든 데이터에 offset을 설정하며 Segment를 저장하는 레지스터로는 cs, ds, ss, es가 있습니다. 더 자세한 정보는 https://wiki.osdev.org/Segmentation 을 참조해보세요.
Segment을 계산하는 과정은 약간 특이합니다. Segment가 0x4d, 메모리 주소가 0x20이면, Segment을 반영한 메모리 주소는 (0x4d*0x10)+0x20=0x4f0 으로 계산합니다. 즉, Segment에 0x10을 곱하여 사용합니다. 따라서 0x7c00을 offset으로 사용하기 위해서는 0x7c0을 segment로 사용해야 합니다.
Segment를 표현하는 방법은 [es:변수 주소]입니다. es레지스터에 저장된 offset으로 변수의 실제 주소를 계산할 수 있습니다.
코드
이번 코드는 간단해 주석으로 설명을 대체하겠습니다.
mov ah, 0x0e
; 기본적으로 0x10곱하므로
; offset으로 0x7c00아닌 0x7c0 설정
mov bx, 0x7c0
mov ds, bx
mov al, [the_secret]
int 0x10
mov bx, 0x7c0
mov es, bx
; segment:메모리 주소
mov al, [es:the_secret]
int 0x10
jmp $
the_secret:
db 'X'
times 510-($-$$) db 0
dw 0xaa55
실행 결과
위 코드를 실행 bin파일로 컴파일하고, qemu로 실행시키면 다음과 같은 화면이 나옵니다.