-1

I've recently found myself following this tutorial: https://johv.dk/blog/bare-metal-assembly-tutorial

I've followed all steps, and my file boots fine using QEMU, but when I try to run it on my laptop (ASUS TUF Gaming F15 with Core i5-11400H, 16GB, RTX 3050TI) after selecting the USB stick I'm using in the boot menu the screen goes black and fans slowly ramp up to 100%, with no text anywhere.

Here's the code:

format pe64 efi
entry main

section '.text' executable readable

main:
  mov rcx, [rdx + 64]
  mov rax, [rcx + 8]
  mov rdx, string
  sub rsp, 32
  call rax
  add rsp,32
  jmp $

section '.data' readable writable

string du 'Hello, World!', 0xD, 0xA, 0

Edit

I think I've found what the problem is: both sections are compiled to the size of 512 bytes, while from what I know 512 bytes is the maximum size of the boot file supported by UEFI. So now my question is: how do I limit the compiled section size to be less than 256 bytes?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • 1
    Welcome to Stack Overflow. Please read the [About](http://stackoverflow.com/tour) page soon and also visit the links describing [How to Ask a Question](http://stackoverflow.com/questions/how-to-ask) and [How to create a Minimal Complete Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Providing the necessary details, including your MCRE, compiler warnings and associated errors, and sample data if any, will allow everyone here to help you with your question. (which is likely the cause of the downvotes -- of which I am NOT one) – David C. Rankin Aug 26 '23 at 01:42
  • @DavidC.Rankin That's the thing, there are no comiler warnings or errors, just the standard `2 passes, 1536 bytes.` output. I will post the code shortly. – DrogaMleczna Aug 26 '23 at 17:58
  • Should the code be position-independent? `lea rdx, [rel string]` or whatever the FASM syntax is for a RIP-relative addressing mode. (See [How to load address of function or label into register](https://stackoverflow.com/q/57212012) for the machine-code differences between that and `mov reg, imm64`, and syntax for some other assemblers; FASM syntax is usually the same as NASM.) – Peter Cordes Aug 26 '23 at 20:11
  • @PeterCordes I changed it to `lea rdx, [string]` which I think is the correct RIP-realtive addressing, and the problem persists: while it works fine in QEMU on real hardware all I get is a black screen – DrogaMleczna Aug 27 '23 at 08:55
  • Yeah, `objdump -drwC -Mintel` confirms it disassembles to `[rip+0xff1]`. Oh well, was worth a try. – Peter Cordes Aug 27 '23 at 11:50
  • @DrogaMleczna Replying to your recent edit: Why don't you see what happens if you remove `section '.data' readable writable` so as to get a single 512-byte section? The `.text` section is readable and can contain your constant data too. – Sep Roland Aug 27 '23 at 21:17
  • @SepRoland The output file is 1024 bytes, but it still doesn't work. On a side note, I tried changing `jmp $` to `hlt` and `ret`, but it still doesn't work – DrogaMleczna Aug 27 '23 at 22:35
  • I wonder if there are UEFI BIOSes that are expecting a `.reloc` table (even if it has nothing in it). What happens if you try this on real hardware: https://pastebin.com/J4NeMTZ0 . This should force the PE32+ file to have a HAS_RELOC flag which I think some BIOSes might be expecting as well. This is just a mostly blind guess as the code itself seems fine (correct parameters being passed to ConOut->OutputString and correctly called) – Michael Petch Aug 28 '23 at 00:57
  • @MichaelPetch I tried your code, but it still doesn't work. Also, sorry for the late reply. – DrogaMleczna Aug 29 '23 at 00:01
  • One last experiment. What if you take my code and make one alteration. Replace `format pe64 efi` with `format pe64 efi dll` ? – Michael Petch Aug 29 '23 at 00:10
  • @MichaelPetch Still doesn't work – DrogaMleczna Aug 29 '23 at 07:35

0 Answers0