Author Topic: Error message on windows: fatal: unable to open output file (?)  (Read 16175 times)

Offline gomi

  • Jr. Member
  • *
  • Posts: 9
Error message on windows: fatal: unable to open output file (?)
« on: November 20, 2020, 04:47:09 PM »
Hello, I am interested in assembly programs. I want to make simple boot.asm file and run it in qemu. I made asm file run nasm in order to compile to bin on win10x64 with command " nasm boot.asm -f bin -o boot.bin " Note that my nasm was came from installer so it is located in " C:\Program Files\NASM> " so I put my boot.asm inside it too. However, I got fatal: " unable to open output file boot.bin "

Yes I know that I would do it on linux but somehow I have to do this on windows10x64.

Thank you very much for any help.

Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 221
  • Country: gb
    • GPUZoo
Re: Error message on windows: fatal: unable to open output file (?)
« Reply #1 on: November 20, 2020, 06:06:52 PM »
I'm not sure, but it could be because you are trying to output it under "Program Files". Windows can be quite strict about where you can write files, and Program Files is possibly a protected operating system directory.
My graphics card database: www.gpuzoo.com

Offline gomi

  • Jr. Member
  • *
  • Posts: 9
Re: Error message on windows: fatal: unable to open output file (?)
« Reply #2 on: November 20, 2020, 06:50:55 PM »
Thank you, I changed my path to desktop and run command line as Admin. Now I got another issue  ;D ===> "boot.asm:18: error: parser: instruction expected"

Here is my code which is an example code I got:

Code: [Select]
;org 0x7C00                      ; BIOS loads our programm at this address
;bits 16                         ; We're working at 16-bit mode here

start:
cli                     ; Disable the interrupts
mov si, msg             ; SI now points to our message
mov ah, 0x0E            ; Indicate BIOS we're going to print chars
.loop lodsb                   ; Loads SI into AL and increments SI [next char]
or al, al               ; Checks if the end of the string
jz halt                 ; Jump to halt if the end
int 0x10                ; Otherwise, call interrupt for printing the char
jmp .loop               ; Next iteration of the loop

halt: hlt                     ; CPU command to halt the execution
msg: db "Hello, World!", 0   ; Our actual message to print

;; Magic numbers
times 510 – ($ – $$) db 0
dw 0xAA55

Offline gomi

  • Jr. Member
  • *
  • Posts: 9
Re: Error message on windows: fatal: unable to open output file (?)
« Reply #3 on: November 20, 2020, 07:19:15 PM »
Alright I found an answer for myself, compiler needs a compatible code for itself, I searched and found the compatible code

Source: https://medium.com/@g33konaut/writing-an-x86-hello-world-boot-loader-with-assembly-3e4c5bdd96cf

nasm compatible code:

Code: [Select]
[bits 16] ; use 16 bits
[org 0x7c00] ; sets the start address
init:
  mov si, msg ; loads the address of "msg" into SI register
  mov ah, 0x0e ; sets AH to 0xe (function teletype)
print_char:
  lodsb ; loads the current byte from SI into AL and increments the address in SI
  cmp al, 0 ; compares AL to zero
  je done ; if AL == 0, jump to "done"
  int 0x10 ; print to screen using function 0xe of interrupt 0x10
  jmp print_char ; repeat with next byte
done:
  hlt ; stop execution
msg: db "Hello world!", 0 ; we need to explicitely put the zero byte here
times 510-($-$$) db 0 ; fill the output file with zeroes until 510 bytes are full
dw 0xaa55 ; magic number that tells the BIOS this is bootable

I got my boot.bin without error now.

Thanks.

Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 221
  • Country: gb
    • GPUZoo
Re: Error message on windows: fatal: unable to open output file (?)
« Reply #4 on: November 20, 2020, 07:44:54 PM »
If/when you want to expand on your bootsector, you might find my sample boot sector useful. It can be found at https://forum.nasm.us/index.php?topic=2265.0 and is FAT12 only (should be simpler to write one that can read your OS loader off other file systems). It can read whatever loader you code into it, and if your loader sets up 32 or 64 bit modes it can be any size. It can even cope with fragmented code :)
My graphics card database: www.gpuzoo.com

Offline gomi

  • Jr. Member
  • *
  • Posts: 9
Re: Error message on windows: fatal: unable to open output file (?)
« Reply #5 on: November 22, 2020, 08:48:32 AM »
If/when you want to expand on your bootsector, you might find my sample boot sector useful. It can be found at https://forum.nasm.us/index.php?topic=2265.0 and is FAT12 only (should be simpler to write one that can read your OS loader off other file systems). It can read whatever loader you code into it, and if your loader sets up 32 or 64 bit modes it can be any size. It can even cope with fragmented code :)

Thanks, I will check that. Do you have idea about interacting "C" and asm ? Here I have a topic: https://forum.nasm.us/index.php?topic=2698.0   since this is first time I will do it, I based on a totorial page. My program works but I cant see "C" print in the end.

Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 221
  • Country: gb
    • GPUZoo
Re: Error message on windows: fatal: unable to open output file (?)
« Reply #6 on: November 22, 2020, 08:50:50 AM »
I've never combined C and assembler, but I am sure someone will come along who can help. I imagine it's quite easy once you know how :)
My graphics card database: www.gpuzoo.com

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 368
  • Country: br
Re: Error message on windows: fatal: unable to open output file (?)
« Reply #7 on: November 22, 2020, 04:16:50 PM »
Alright I found an answer for myself, compiler needs a compatible code for itself, I searched and found the compatible code
Nope... the error is, instead of '-' (0x2d in UTF-8) at line 18, you are using '–' (0xe2 0x80 0x93, in UTF-8). That's the problem copying and pasting code from sites...

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 368
  • Country: br
Re: Error message on windows: fatal: unable to open output file (?)
« Reply #8 on: November 22, 2020, 04:37:54 PM »
Thanks, I will check that. Do you have idea about interacting "C" and asm ? Here I have a topic: https://forum.nasm.us/index.php?topic=2698.0   since this is first time I will do it, I based on a totorial page. My program works but I cant see "C" print in the end.
There are 2 kinds of programs writen in C: "hosted" and "freestanding". Hosted programs are those which uses libraries such as libc and "c runtime initialization" codes linked to your final binary. Freestanding are those which don't use ANY libraries, except your own, and have no initialization code linker together your code to make it work.

When developing a boot sector isn't interesting to use hosted code (using functions as printf(), scanf() etc) because you don't have an operating system providing system calls. But you CAN write functions in C usefun to your assembly code. Just have to respect C's "calling convention" and check if your compiler can create real mode compatible code. This is the case of GCC (probably you can do this with Visual Studio's C++ compiler, but is much harder!).

Example: Consider you fave a simple C function which multiply an integer value by 3:
Code: [Select]
int f( int x ) { return 3 * x; }
If you compile this and take a look at the assembly code, you'll see:
Code: [Select]
; Compiled with 'gcc -m16 -masm=intel -S -O2 test.c`
f:
  mov eax,[esp+4]
  lea eax,[eax+eax*2]
  ret
Notice the C's function f() uses the stack to get its arguments. This can be avoided using function attributes, like this one:
Code: [Select]
__attribute__((regparm(1))) int f( int x ) { return 3 * x; }This will create a function like this:
Code: [Select]
; Compiled with 'gcc -m16 -masm=intel -S -O2 test.c`
f:
  lea eax,[eax+eax*2]
  ret
Where f() expects its argument to be passed in EAX.
You can link this kind of C function with your NASM program using the 'extern' keyword:
Code: [Select]
; my nasm code
bits 16
...
extern f

start:
  ...
  mov eax,2
  call f
  ; Here EAX will be 6.
  ...
The problem now is, when you compile your assembly file with -f bin, you cannot LINK anything (bin format lacks the necessary info abour externs and globals)... You'll need to create a code with sections, compile it to some object format (elf or pe, dependind on the operating system) and create a linker script to transform your objects in a binary code suitable for a boot sector. Essentially:
Code: [Select]
$ nasm boot.asm -o boot.o
$ gcc -O2 -m16 -c -o func.o func.c
$ ld -Tmyscript.ld func-o boot.o -o boot
All the magic of transforming code with 'sections' to binary must be inside this 'myscript.ld' linker script. Which is something like this:
Code: [Select]
OUTPUT(binary)

SECTIONS {
  .text: *(.text)
  .rodata: *(.rodata)
  .data: *(.data)
  . = 510;
  BYTE(0x55), BYTE(0xAA);
}
Or something like this (search about ld linker scripts)...
« Last Edit: November 22, 2020, 04:43:43 PM by fredericopissarra »

Offline gomi

  • Jr. Member
  • *
  • Posts: 9
Re: Error message on windows: fatal: unable to open output file (?)
« Reply #9 on: November 22, 2020, 10:04:25 PM »
@fredericopissarra well, looks like I do not know enough about this.

Quote
There are 2 kinds of programs writen in C: "hosted" and "freestanding". Hosted programs are those which uses libraries such as libc and "c runtime initialization" codes linked to your final binary. Freestanding are those which don't use ANY libraries, except your own, and have no initialization code linker together your code to make it work.

I did not put "libc" to proper location and also I did not add #include "../libc/string.h" in loader.c during compile. When I compiled in that path there was no libc. Is that the reason "C" message did not print ?