Author Topic: NASM x86 Win32 Bootloader Bochs Emulator  (Read 19265 times)

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
NASM x86 Win32 Bootloader Bochs Emulator
« on: December 12, 2013, 03:38:00 PM »
Hello!

Some time ago, when i was sitting on 32Bit machine,
i created a Win32 Bootloader that is emulated using Bochs Emulator.

; *************************************************
; *************************************************
; This is: NASM x86 Win32 Bootloader
; Author: J.K. Encryptor256
; Date: Tuesday, October 1, 2013
; *************************************************
; Using: Netwide assembler and Bochs emulator
; Nasm: Generates .obj file
; Bochs: Emulates generated file as a hard disk
; *************************************************
; Bochs emulator:
; In order to emulate bootloader as a hard disk, we have to make
; this NASM .obj result file a bit larger, so, bochs could read it,
; ... more info a the end of file.
; *************************************************
; Compile:
; Nasm: "nasm -fbin bootloader.asm"
; *************************************************
; TASK:
; 1. Create bootloader
; 2. Compile with NASM
; 3. Configure Bochs (visual demo, find on youtube, link below)
; 4. Emulate with Bochs (visual demo, find on youtube, link below)
; *************************************************
; *************************************************

Source code:
Code: [Select]
; START:

; Tell compiler to generate 16 bit code
bits 16

; Bootloader specification states, that Bootloader program will be loaded
; at segment 0x0000 and address 0x07C0.
; We can use "org" directive to set program origin.
; Fixed address is of 0x0000 and 0x07C0 is 0x7C00.

;org 0x7c00 ; But i don't like it, so i will setup segments manually.

; A. Jump to the code --------------------------------------

        jmp bootloader

; B.Define data, procedures here ---------------------------
       
        message db 13,10," * [ !!! NASM: Hello World :NASM !!! ]  *",13,10,0

        printMessage:
                ; -----------------
                push ax
                ; -----------------
                .loop:
                        ; -----------
                        lodsb
                        or al,al
                        jz .quitLoop
                        mov ah,0x0e
                        int 0x10
                        jmp .loop
                ; -----------------
                .quitLoop:
                ; -----------------
                pop ax
                ; -----------------
                ret


; Here, with label "bootloader" starts our code: --------------

bootloader:

        ; CLI - Interrupts:
        ; disable interrupts when setting up segments,
        ; because, changes in segment registers,
        ; could fire some interrut and ruin something for us.

        cli

        ; Setup segments, just here where we are loaded at.
        mov ax,0x07c0
        mov ds,ax
        mov gs,ax
        mov fs,ax
        mov es,ax

        ; Setup stack segment.
        ; Bios memory map description on the internet states, that:
        ; Address:
        ; from: 0x00007E00,
        ; to: 0x0007FFFF
        ; is (480.5 KiB, RAM (guaranteed free for use), Conventional memory)
        ; So let's setup our stack segment there.

        ; Setup stack segment address
        mov ax,0x07e0
        mov ss,ax
        mov bp,ax

        ; Setup stack segment pointer (roof) of 256 bytes
        mov sp,0xff
       
        ; STI - Interrupts:
        ; enable interrupts, so, software and hardware can call them.

        sti

        ; Display message on the screen
        mov si,message
        call printMessage

        ; Display message on the screen
        mov si,message
        call printMessage

        ; Display message on the screen
        mov si,message
        call printMessage

        ; Now stop cpu, so we could see the result

        ; Clear interrupts and halt cpu
        cli
        hlt

        ; Finish Bootloader: -------------------------------------------------------------

        ; Bootloader, must be, total in sizeof 512 bytes.
        ; The last two bytes are constant numbers.
       
        ; Fill rest of file with zeroes, how many zeroes needed?
        ; 510 - (Current location) - (Start of file) define bytes with value of zero
        times 510 - ($-$$) db 0

        ; Here comes last two signature bytes,
        ; so, cpu can recoginze, that this is a valid bootloader
        DB 0x55
        DB 0xAA

        ; END, but for Bochs emulator, this file will be recognized as a hard disk,
        ; so, let's make it a bit bigger, just to fool Bochs

        times 1024*1024 db 0

Attachment:
Attachment includes source code file, plus Bochs config file.
Config file might be required to be reconfigured again.

Video:
Watch full source code and runtime demo on youtube: NASM Bootloader x86 The Netwide Assembler Bochs Emulator

I think that's it, that i wanted to share,
bye,
Encryptor256!!!
Encryptor256's Investigation \ Research Department.

Offline Hafiz Naser Aslam

  • Jr. Member
  • *
  • Posts: 2
Re: NASM x86 Win32 Bootloader Bochs Emulator
« Reply #1 on: April 10, 2015, 07:01:46 PM »
E:\NASM>nasm -f obj project.asm

E:\NASM>LINK project.obj io.obj

Microsoft (R) Segmented Executable Linker  Version 5.60.339 Dec  5 1994
Copyright (C) Microsoft Corp 1984-1993.  All rights reserved.

Run File [project.exe]:
List File [nul.map]:
Libraries [.lib]:
Definitions File [nul.def]:
project.obj(project.asm) : error L1070: text : segment size exceeds 64K
io.obj(io.asm) : fatal error L1123: text : segment defined both 16- and 32-bit

E:\NASM>project.exe
'project.exe' is not recognized as an internal or external command,
operable program or batch file.




these are errors when i run the exe file plese tell me how to solve these issues

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: NASM x86 Win32 Bootloader Bochs Emulator
« Reply #2 on: April 10, 2015, 07:46:24 PM »
Hi Hafiz Naser Aslam,

Thanks for joining us!

What is "project.asm"? What is "io.asm"? I'm confused.

Best,
Frank