نمایش نتایج 1 تا 3 از 3

نام تاپیک: hello world kernel

  1. #1

    hello world kernel

    salam be hame dostan bebakhshid ke farsi neminevisam
    man daram say mikonam barname nevisi sath paiin va kernel ro yad begiram hala be ye moshkel khoram harchi ke search kardam va maghale khondam chizi motevajeh nashodam
    mamnon misham komakam konin
    man 4 ta file neveshtam

    boot.asm
    ;
    Declare constants used for creating a multiboot header.
    MBALIGN equ 1<<0 ; align loaded modules on page boundaries
    MEMINFO equ 1<<1 ; provide memory map
    FLAGS equ MBALIGN | MEMINFO ; this is the Multiboot 'flag' field
    MAGIC equ 0x1BADB002 ; 'magic number' lets bootloader find the header
    CHECKSUM equ -(MAGIC + FLAGS) ; checksum of above, to prove we are multiboot

    ; Declare a header as in the Multiboot Standard. We put this into a special
    ; section so we can force the header to be in the start of the final program.
    ; You don't need to understand all these details as it is just magic values that
    ; is documented in the multiboot standard. The bootloader will search for this
    ; magic sequence and recognize us as a multiboot kernel.
    section .multiboot
    align 4
    dd MAGIC
    dd FLAGS
    dd CHECKSUM

    ; Currently the stack pointer register (esp) points at anything and using it may
    ; cause massive harm. Instead, we'll provide our own stack. We will allocate
    ; room for a small temporary stack by creating a symbol at the bottom of it,
    ; then allocating 16384 bytes for it, and finally creating a symbol at the top.
    section .bootstrap_stack, nobits
    align 4
    stack_bottom:
    resb 16384
    stack_top:

    ; The linker script specifies _start as the entry point to the kernel and the
    ; bootloader will jump to this position once the kernel has been loaded. It
    ; doesn't make sense to return from this function as the bootloader is gone.
    section .text
    global _start
    _start:
    ; Welcome to kernel mode! We now have sufficient code for the bootloader to
    ; load and run our operating system. It doesn't do anything interesting yet.
    ; Perhaps we would like to call printf("Hello, World\n"). You should now
    ; realize one of the profound truths about kernel mode: There is nothing
    ; there unless you provide it yourself. There is no printf function. There
    ; is no <stdio.h> header. If you want a function, you will have to code it
    ; yourself. And that is one of the best things about kernel development:
    ; you get to make the entire system yourself. You have absolute and complete
    ; power over the machine, there are no security restrictions, no safe
    ; guards, no debugging mechanisms, there is nothing but what you build.

    ; By now, you are perhaps tired of assembly language. You realize some
    ; things simply cannot be done in C, such as making the multiboot header in
    ; the right section and setting up the stack. However, you would like to
    ; write the operating system in a higher level language, such as C or C++‎‎‎‎‎.
    ; To that end, the next task is preparing the processor for execution of
    ; such code. C doesn't expect much at this point and we only need to set up
    ; a stack. Note that the processor is not fully initialized yet and stuff
    ; such as floating point instructions are not available yet.

    ; To set up a stack, we simply set the esp register to point to the top of
    ; our stack (as it grows downwards).
    mov esp, stack_top

    ; We are now ready to actually execute C code. We cannot embed that in an
    ; assembly file, so we'll create a kernel.c file in a moment. In that file,
    ; we'll create a C entry point called kernel_main and call it here.
    extern kernel_main
    call kernel_main

    ; In case the function returns, we'll want to put the computer into an
    ; infinite loop. To do that, we use the clear interrupt ('cli') instruction
    ; to disable interrupts, the halt instruction ('hlt') to stop the CPU until
    ; the next interrupt arrives, and jumping to the halt instruction if it ever
    ; continues execution, just to be safe.
    cli
    .hang:
    hlt
    jmp .hang


    kernel.c


    #if !defined(__cplusplus)
    #include <stdbool.h> /* C doesn't have booleans by default. */
    #endif
    #include <stddef.h>
    #include <stdint.h>

    /* Hardware text mode color constants. */
    enum vga_color {
    COLOR_BLACK = 0,
    COLOR_BLUE = 1,
    COLOR_GREEN = 2,
    COLOR_CYAN = 3,
    COLOR_RED = 4,
    COLOR_MAGENTA = 5,
    COLOR_BROWN = 6,
    COLOR_LIGHT_GREY = 7,
    COLOR_DARK_GREY = 8,
    COLOR_LIGHT_BLUE = 9,
    COLOR_LIGHT_GREEN = 10,
    COLOR_LIGHT_CYAN = 11,
    COLOR_LIGHT_RED = 12,
    COLOR_LIGHT_MAGENTA = 13,
    COLOR_LIGHT_BROWN = 14,
    COLOR_WHITE = 15,
    };

    uint8_t make_color(enum vga_color fg, enum vga_color bg) {
    return fg | bg << 4;
    }

    uint16_t make_vgaentry(char c, uint8_t color) {
    uint16_t c16 = c;
    uint16_t color16 = color;
    return c16 | color16 << 8;
    }

    size_t strlen(const char* str) {
    size_t ret = 0;
    while ( str[ret] != 0 )
    ret++;
    return ret;
    }

    static const size_t VGA_WIDTH = 80;
    static const size_t VGA_HEIGHT = 25;

    size_t terminal_row;
    size_t terminal_column;
    uint8_t terminal_color;
    uint16_t* terminal_buffer;

    void terminal_initialize() {
    terminal_row = 0;
    terminal_column = 0;
    terminal_color = make_color(COLOR_LIGHT_GREY, COLOR_BLACK);
    terminal_buffer = (uint16_t*) 0xB8000;
    for (size_t y = 0; y < VGA_HEIGHT; y++) {
    for (size_t x = 0; x < VGA_WIDTH; x++) {
    const size_t index = y * VGA_WIDTH + x;
    terminal_buffer[index] = make_vgaentry(' ', terminal_color);
    }
    }
    }

    void terminal_setcolor(uint8_t color) {
    terminal_color = color;
    }

    void terminal_putentryat(char c, uint8_t color, size_t x, size_t y) {
    const size_t index = y * VGA_WIDTH + x;
    terminal_buffer[index] = make_vgaentry(c, color);
    }

    void terminal_putchar(char c) {
    terminal_putentryat(c, terminal_color, terminal_column, terminal_row);
    if (++terminal_column == VGA_WIDTH) {
    terminal_column = 0;
    if (++terminal_row == VGA_HEIGHT) {
    terminal_row = 0;
    }
    }
    }

    void terminal_writestring(const char* data) {
    size_t datalen = strlen(data);
    for (size_t i = 0; i < datalen; i++)
    terminal_putchar(data[i]);
    }

    #if defined(__cplusplus)
    extern "C" /* Use C linkage for kernel_main. */
    #endif
    void kernel_main() {
    /* Initialize terminal interface */
    terminal_initialize();

    /* Since there is no support for newlines in terminal_putchar
    * yet, '\n' will produce some VGA specific character instead.
    * This is normal.
    */
    terminal_writestring("Hello, kernel World!\n");
    }




    linker.ld

    /* The bootloader will look at this image and start execution at the symbol
    designated as the entry point. */
    ENTRY(_start)

    /* Tell where the various sections of the object files will be put in the final
    kernel image. */
    SECTIONS
    {
    /* Begin putting sections at 1 MiB, a conventional place for kernels to be
    loaded at by the bootloader. */
    . = 1M;

    /* First put the multiboot header, as it is required to be put very early
    early in the image or the bootloader won't recognize the file format.
    Next we'll put the .text section. */
    .text BLOCK(4K) : ALIGN(4K)
    {
    *(.multiboot)
    *(.text)
    }

    /* Read-only data. */
    .rodata BLOCK(4K) : ALIGN(4K)
    {
    *(.rodata)
    }

    /* Read-write data (initialized) */
    .data BLOCK(4K) : ALIGN(4K)
    {
    *(.data)
    }

    /* Read-write data (uninitialized) and stack */
    .bss BLOCK(4K) : ALIGN(4K)
    {
    *(COMMON)
    *(.bss)
    *(.bootstrap_stack)
    }

    /* The compiler may produce other sections, by default it will put them in
    a segment with the same name. Simply add stuff here as needed. */
    }



    grub.cfg


    menuentry "myos" {
    multiboot /boot/myos.bin
    }

    va dastorat zir ra estefade kardam

    nasm -felf32 boot.asm -o boot.o

    g++ -c kernel.cpp -o kernel.o -ffreestanding -fno-exceptions -fno-rtti

    gcc boot.o kernel.o -T linker.ld -o myos.bin -nostdlib -nodefaultlibs -lgcc

    mkdir -p isodir/boot/grub

    cp myos.bin isodir/boot/myos.bin

    cp grub.cfg isodir/boot/grub/grub.cfg

    grub-mkrescue -o myos.iso isodir

    qemu-system-i386 -cdrom myos.iso


    vaghti gemu ejra mishe be error zir mikhoram
    error: no multiboot header fund
    kesy mitone komakam kone ke moshkel chi hast?



  2. #2

    نقل قول: hello world kernel

    یه ساختاری به اسم مولتی بوت هست که باید ابتدای فایل هسته قرار بگیره. این ساختار یکسری از اطلاعات بوت رو نگهداری می کنه. از اونجا که می خوایی از
    گراب استفاده کنی می تونی از فایل لینک سیستم عامل آراکس استفاده کنی. (البته بعد از اعمال تغییرات) همیمنطور حتما فایل makefile رو هم نگاه بنداز. ترتیب برای این ساختار خیلی مهمه.

    کد:
    http://sourceforge.net/p/arax/code/HEAD/tree/

    فایل لینک:
    http://sourceforge.net/p/arax/code/HEAD/tree/link.ld

    فایل make:
    http://sourceforge.net/p/arax/code/HEAD/tree/Makefile

  3. #3

    نقل قول: hello world kernel

    اینجور که مشخصه شما سیستم عامل و اولین تجربتون رو با 32bit-Protected Mode داشتید. خیلی هم خوب! کدی مثل کد زیر هم میتونه کمک کنه توی نوشتن 16bit Real mode :


    MOV AH, 0x0e
    MOV AL, 'H'
    INT 0x10

    JMP $

    times 510-($-$$) db 0
    DW 0xaa55


    موفق باشید :)

تاپیک های مشابه

  1. سوال: مشکل این Hello world چیه؟
    نوشته شده توسط all_vb_net در بخش برنامه نویسی با زبان C و ++C
    پاسخ: 6
    آخرین پست: سه شنبه 28 اردیبهشت 1389, 13:30 عصر
  2. برنامه Hello World برای موبایل
    نوشته شده توسط mousamk در بخش Symbian OS
    پاسخ: 3
    آخرین پست: یک شنبه 15 بهمن 1385, 09:25 صبح
  3. یه برنامه کوچک برای آموزش - پروژه "Hello World"
    نوشته شده توسط Mohammad_programmer در بخش برنامه نویسی اسمبلی خانواده x86
    پاسخ: 4
    آخرین پست: سه شنبه 13 اردیبهشت 1384, 19:22 عصر

قوانین ایجاد تاپیک در تالار

  • شما نمی توانید تاپیک جدید ایجاد کنید
  • شما نمی توانید به تاپیک ها پاسخ دهید
  • شما نمی توانید ضمیمه ارسال کنید
  • شما نمی توانید پاسخ هایتان را ویرایش کنید
  •