Someone
Someone

Reputation: 126

NASM: invalid combination of opcode and operands in a %include file, reported line number is past the end?

I'm building a simple OS and am trying to just get some text on the screen, from the bootloader.

Whenever I compile it (nasm -fbin bootloader.asm), it gives this error:

print.asm:23: error: invalid combination of opcode and operands

I can't understand what's wrong, and print.asm doesn't even have that many lines, so it's not clear which line NASM doesn't like.

This is the bootloader.asm:

[org 0x7c00]

mov bp, 0x7c00
mov sp, bp

mov bx, TestString
call PrintString

jmp $

%include "print.asm"

times 510-($-$$) db 0

dw 0xaa55

Here is print.asm:

PrintString:
    push ax
    push bx

    mov ah, 0x0e
    .Loop:
    cmp [bx], byte 0
    je .Exit
        mov al, [bx]
        int 0x10
        int bx
        jmp .Loop
    .Exit:

    pop ax
    pop bx
    ret

TestString:
    db 'Test String',0

Upvotes: 2

Views: 406

Answers (1)

Peter Cordes
Peter Cordes

Reputation: 363980

nasm -fbin print.asm reveals the actual line number, 11.

int bx isn't an instruction. Typo for inc.

I think NASM's line number after a %include is probably overall line since the start of the main file, which is a bug in NASM 2.15.05 (at least). In this case your file can assemble separately to see what's wrong, but other cases would be less easy. The bug has already been fixed in NASM 2.16rc0, and may get backported to 2.15.xx. https://bugzilla.nasm.us/show_bug.cgi?id=3392731

Apparently this also affects debug info line-numbering. e.g. for setting breakpoints in GDB by source line number. It of course always works to b *0x1234 with a copy-pasted address from disas output, that doesn't care about debug info.


yasm -fbin bootloader.asm gets the line numbering correct: print.asm:11: ... with the same invalid combo message as NASM. That's from yasm 1.3.0. Unfortunately YASM isn't being developed anymore (?), and hasn't kept up with new instructions like AVX-512, but it's usable for stuff like bootloaders.

The bug is also not present in some older versions of NASM, for example 2.11.05 from 2014 reports print.asm:11. I don't have versions between that kicking around to bisect when this bug was introduced.

Upvotes: 3

Related Questions