สวัสดีครับ ผมลองเขียนระบบปฎิบัติการดู ใช้ภาษา C++ และ asm ครับ ผมเขียนแล้วรวมไฟล์เป็น .iso แล้วไปรันใน VMware แล้วรันไม่ผ่านครับ นี้เป็นโค้ดไฟล์ครับ

--kernel.cpp--

#include

int main() { std::cout << "Hello, world!" << std::endl; return 0; }

--Link.ld--

OUTPUT_FORMAT("binary") ENTRY(start) SECTIONS { .text 0x100000 : { code = .; _code = .; __code = .; *(.text) . = ALIGN(4096); }

.data : { __CTOR_LIST__ = .; LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2) *(.ctors) LONG(0) __CTOR_END__ = .; __DTOR_LIST__ = .; LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2) *(.dtors) LONG(0) __DTOR_END__ = .;

data = .; _data = .; __data = .; *(.data) . = ALIGN(4096); }

.bss : { bss = .; _bss = .; __bss = .; *(.bss) . = ALIGN(4096); }

end = .; _end = .; __end = .; } INPUT(kernel.o)

-- Loader.asm --

[BITS 32] ; protected mode

[global start] [extern _main] ; this is our C++ code [extern __main] ; this is our C support code [extern __atexit]

start: call __main call _main ; call our C++ code call __atexit

cli ; interrupts will disturb the halt hlt ; halt the CPU

ผมใช้ nasm ในการคอมไพล์ asm ส่วน C++ ใช้ Dev-C++ 5.5.3 ตัวคอมไพล์ TDM-GCC 4.7.1 32-bit ครับ (คอมที่เขียนใช้ 64 bit) ผมพลาดตรงไหนครับ ขอบคุณครับ

Hiring! บริษัทที่น่าสนใจ

Carmen Software company cover
Carmen Software
Hotel Financial Solutions
Next Innovation (Thailand) Co., Ltd. company cover
Next Innovation (Thailand) Co., Ltd.
We are web design with consulting & engineering services driven the future stronger and flexibility.
KKP Dime company cover
KKP Dime
KKP Dime บริษัทในเครือเกียรตินาคินภัทร
Kiatnakin Phatra Financial Group company cover
Kiatnakin Phatra Financial Group
Financial Service
Fastwork Technologies company cover
Fastwork Technologies
Fastwork.co เว็บไซต์ที่รวบรวม ฟรีแลนซ์ มืออาชีพจากหลากหลายสายงานไว้ในที่เดียวกัน
Thoughtworks Thailand company cover
Thoughtworks Thailand
Thoughtworks เป็นบริษัทที่ปรึกษาด้านเทคโนโยลีระดับโลกที่คว้า Great Place to Work 3 ปีซ้อน
Iron Software company cover
Iron Software
Iron Software is an American company providing a suite of .NET libraries by engineer for engineers.
CLEVERSE company cover
CLEVERSE
Cleverse is a Venture Builder. Our team builds several tech companies.
Nipa Cloud company cover
Nipa Cloud
#1 OpenStack cloud provider in Thailand with our own data center and software platform.
Bangmod Enterprise company cover
Bangmod Enterprise
The leader in Cloud Server and Hosting in Thailand.
CIMB THAI Bank company cover
CIMB THAI Bank
MOVING FORWARD WITH YOU - CIMB is the leading ASEAN Bank
Bangkok Bank company cover
Bangkok Bank
Bangkok Bank is one of Southeast Asia's largest regional banks, a market leader in business banking
MuvMi (Urban Mobility Tech Co.,Ltd.) company cover
MuvMi (Urban Mobility Tech Co.,Ltd.)
Shape the future of urban mobility towards affordable, clean, and safe solutions
T.N. Digital Solution Co., Ltd. company cover
T.N. Digital Solution Co., Ltd.
TNDS has been involving in every first move of banking’s major digital transformation.
KBTG - KASIKORN Business-Technology Group company cover
KBTG - KASIKORN Business-Technology Group
KBTG - "The Technology Company for Digital Business Innovation"
Siam Commercial Bank Public Company Limited company cover
Siam Commercial Bank Public Company Limited
"Let's start a brighter career future together"
Icon Framework co.,Ltd. company cover
Icon Framework co.,Ltd.
Global Standard Platform for Real Estate แพลตฟอร์มสำหรับธุรกิจอสังหาริมทรัพย์ครบวงจร มาตรฐานระดับโลก
REFINITIV company cover
REFINITIV
The Financial and Risk business of Thomson Reuters is now Refinitiv
H LAB company cover
H LAB
Re-engineering healthcare systems through intelligent platforms and system design.
The Gang Technology Co., Ltd. company cover
The Gang Technology Co., Ltd.
We're a Digital Agency that helps our customers transform their business into digital with ease.
LTMH company cover
LTMH
LTMH มุ่งเน้นการพัฒนาผลิตภัณฑ์ที่สามารถช่วยพันธมิตรของเราให้บรรลุเป้าหมาย
Seven Peaks company cover
Seven Peaks
We Drive Digital Transformation
Wisesight (Thailand) Co., Ltd. company cover
Wisesight (Thailand) Co., Ltd.
The Best Choice For Handling Social Media · High Expertise in Social Data · Most Advanced and Secure
MOLOG Tech company cover
MOLOG Tech
We are Modern Logistic Platform, Specialize in WMS, OMS and TMS.
Data Wow Co.,Ltd company cover
Data Wow Co.,Ltd
We enable our clients to realize increased productivity by solving their most complex issues by Data
LINE Company Thailand company cover
LINE Company Thailand
LINE, the world's hottest mobile messaging platform, offers free text and voice messaging + Call
LINE MAN Wongnai company cover
LINE MAN Wongnai
Join our journey to becoming No.1 food platform in Thailand

ผมไม่เคยเขียนระบบปฎิบัติการนะ เลยไม่แน่ใจเท่าไร

x86 CPU เมื่อบูทเป็น 16bit real mode เสมอนิครับ ต้องค่อยๆ escalate เป็น 64-bit long mode โดยขึ้นมาเป็น protected mode แล้วถึงเป็น long mode แถมยังมีรายละเอียดยิบย่อยอีกเต็มไปหมด (เช่น A20 line ฯลฯ) แล้วถ้าผมจำไม่ผิด bios มันจะเรียกตำแหน่งที่ 0000:FFFF (ถ้าเป็นเขียนแบบ flat model 32บิท ก็ 0x0000FFFF นั่นแหละ) ขึ้นมารันทีแรก ต้องเอาโค้ดไปไว้ตรงนั้น

ส่วนการใช้ cout ซึ่งเป็น standard c++ library นั้นใช้ไม่ได้แน่นอน เพราะระบปฏิบัติการ (ทีคุณต้องเขียนนี่แหละ) จะต้องรองรับ system call ต่างๆ ที่ libc (glibc, msvc runtime, etc) จะมาเรียกใช้อีกทอดหนึ่ง

ส่วนวิธีแก้นี่ผมไม่ทราบฮะ

ผมว่า Code ที่เขียนมาไม่น่าจะเอามาจาก OSDev เพราะเขาไม่น่าจะสอนเขียนแบบนั้น

อันดับแรกต้องเขียน Boot loader ก่อนคับ ใช้อันที่อยู่ในหมวด Example bootsector ของหน้านี้ก็ได้ http://wiki.osdev.org/Entering_Long_Mode_Directly

ส่วนรายละเอียดที่ต้องรู้ในช่วงนี้ ลองดูใน http://wiki.osdev.org/Boot_Sequence http://wiki.osdev.org/Real_Mode http://wiki.osdev.org/Protected_Mode http://wiki.osdev.org/X86-64

พอเข้าใจ Boot loader แบบเบสิคแล้ว ลองนำมันไปไว้ใน VBR แทน MBR ดูคับ รายละเอียดลองดูในนี้ http://en.m.wikipedia.org/wiki/Volume_boot_record

เอ่อ... ยังไม่เห็น ข้อมูลที่จะลง boot sector (MBR) เลยครับ แล้วมันจะ boot ได้ไง
ผมเคยเขียนตอนเรียน แต่เป็น asm แล้วยัดลง boot sector ของ fdd ก่อน ถึงจะทำงานได้

http://en.wikipedia.org/wiki/Master_Boot_Record