wmaxlees
wmaxlees

Reputation: 605

C Inline Asm Int 0x10

I'm attempting to write a function that prints strings the screen in C. It's for a boot loader so there are no external libraries or anything linked in. Here's my function:

void printString(const char* pStr) {
    while(*pStr) {
        __asm__ __volatile__ (
            "movb 0x0e, %%ah\n"
            "movb %[c], %%al\n"
            "int $0x10\n"
            : 
            : [c] "r" (*pStr)
            : "ax"
        );
        ++pStr;
    }
}

When I run this, I don't get any errors in my VM. It just sits there with the cursor in the upper left corner of the screen. Any thoughts? I can produce an objdump -d if anyone thinks it will be helpful.

Upvotes: 2

Views: 2259

Answers (2)

wmaxlees
wmaxlees

Reputation: 605

Okay after some helpful comments, I may just go with full assembly. Something like

Print:
            push    %ax
            movb    $0x0E, %ah      # Set interrupt code
            movb    $0x00, %bh      # Set page #
.loop:
            lodsb                   # Load next char
            test    %al, %al        # Check for \0
            je      .done
            int     $0x10           # Call interrupt
            jmp     .loop

.done:
            pop     %ax
            ret

That should be 16-bit real mode compatible and can be assembled with GAS, which, as I understand it, works better than GCC for compiling 16-bit programs.

Upvotes: 2

paulsm4
paulsm4

Reputation: 121599

I think you're missing the point. The problem isn't your assembly code; the problem is that "int 10" is a BIOS

If you've already booted to an OS (e.g. Windows or Linux), then your x86 CPU is running in "protected mode"; and you probably don't have access to int 10 from user space ... unless something like a Windows command prompt emulates it for you.

As far as Linux/assembly programming in general, I strongly recommend this (free, on-line, very good) book:

Programming from the Ground Up, Jonathan Bartlett

Thank you for clarifying that you're writing a "boot loader". Strong suggestion1: boot your custom code from a USB stick, or create a virtual DOS floppy to boot a DOS VM (either VMWare or VBox VMs, for example).

Here are some tutorials:

Upvotes: -2

Related Questions