RZ4
RZ4

Reputation: 31

Cannot run assembly on QEMU

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!!

Upvotes: 0

Views: 451

Answers (1)

Brendan
Brendan

Reputation: 37214

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).

Upvotes: 1

Related Questions