0

I recently was trying to make an OS with assembly. I compiled C into NASM assembly, and did the normal things to make the OS run on a "virtual machine" (QEMU) Here is the code for the assembly file:

; Disassembly of file: main.o
; Sun May  8 12:39:04 2022
; Type: ELF64
; Syntax: NASM
; Instruction set: 8086, x64

global main

extern __printf_chk                                     ; near


SECTION .text                            ; section number 1, code


SECTION .data                          ; section number 2, data


SECTION .bss                           ; section number 3, bss


SECTION .rodata.str1.1                 ; section number 4, const

.LC0:                                                   ; byte
        db 48H, 65H, 6CH, 6CH, 6FH, 2CH, 20H, 57H       ; 0000 _ Hello, W
        db 6FH, 72H, 6CH, 64H, 21H, 00H                 ; 0008 _ orld!.


SECTION .text.startup                   ; section number 5, code

main:   ; Function begin
        endbr64                                         ; 0000 _ F3: 0F 1E. FA
        sub     rsp, 8                                  ; 0004 _ 48: 83. EC, 08
        lea     rsi, [rel .LC0]                         ; 0008 _ 48: 8D. 35, 00000000(rel)
        mov     edi, 1                                  ; 000F _ BF, 00000001
        xor     eax, eax                                ; 0014 _ 31. C0
        call    __printf_chk                            ; 0016 _ E8, 00000000(PLT r)
        xor     eax, eax                                ; 001B _ 31. C0
        add     rsp, 8                                  ; 001D _ 48: 83. C4, 08
        ret                                             ; 0021 _ C3
; main End of function


SECTION .note.gnu.property             ; section number 6, const

        db 04H, 00H, 00H, 00H, 10H, 00H, 00H, 00H       ; 0000 _ ........
        db 05H, 00H, 00H, 00H, 47H, 4EH, 55H, 00H       ; 0008 _ ....GNU.
        db 02H, 00H, 00H, 0C0H, 04H, 00H, 00H, 00H      ; 0010 _ ........
        db 03H, 00H, 00H, 00H, 00H, 00H, 00H, 00H       ; 0018 _ ........

I used this command to convert the ASM file to BIN:

nasm -f elf64 myfirst.bin main2.asm

I used this one to convert the BIN file to an FLP file which can be ran by QEMU:

dd status=noxfer conv=notrunc if=myfirst.bin of=myfirst.flp

I then ran QEMU with this command:

qemu-system-i386 -fda myfirst.flp

And that was when it failed... Qemu failed me

Please help!!

1 Answer 1

1

qemu-system-i386 -fda myfirst.flp tells Qemu that the file ("myfirst.flp") is a floppy disk image, so Qemu loads the first sector (first 512 bytes of your file) at 0x7C00 and jumps to it in real mode.

None of your code is compatible with real mode (it's 64-bit for a start) so it won't work.

Your choices are:

a) rewrite everything and build it as "16-bit real mode code for BIOS"

b) write a "loader stub" (as 16-bit real mode code for BIOS) that switches to 64-bit before passing control to your existing code

c) Find a boot loader that does the same as "loader stub" for you (e.g. GRUB, maybe)

d) Switch to UEFI (and replaced the firmware with 64-bit UEFI firmware, and format the disk image with partitions and a UEFI system partition containing a FAT file system, and find a way to convert ELF to PE32+ file format).

Sign up to request clarification or add additional context in comments.

5 Comments

Is there any way to make it compatible then?
@RZ4: No easy way - updated the answer to include possibilities.
@RZ4: Your code uses __printf_chk with an x86-64 System V calling convention. You'll have a much easier time running it under an OS (e.g. Linux or MacOS) than on bare metal, or even under firmware like UEFI.
@Brendan I am trying option a. But I only know how to write in 64-bit code are there any other ways to run my code without running it on an underlying OS?
@RZ4: No other ways that I know of (that weren't mentioned). If you learnt how to write 64-bit code, you can learn to write 16-bit code. It's mostly the same (mostly same instructions, just with 16-bit or 32-bit sizes); where the harder part is dealing with segments (which you might not have to worry about much if it's a small boot loader, if you set it up as "CS = DS = ES = SS" where possible).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.