0

I have a problem with my bootloader, when I call a function to write a screen-> printString, the function don't launch. I don't know why. The code is:

/*generate 16-bit code*/
__asm__(".code16gcc\n");
/*jump boot code entry*/
__asm__("jmpl $0x0000, $main\n");

void printString(char *);

/* user defined function to print series of characters terminated by null character */
void main() {
     /* calling the printString function passing string as an argument */
     printString("Hello, World");
     while(1);
} 
void printString(char *string) {
     __asm__ __volatile__("movb $'H' , %al\n");
     __asm__ __volatile__("movb $0x0e, %ah\n");
     __asm__ __volatile__("int  $0x10\n");
     while(*string) {
          __asm__ __volatile__ (
               "int $0x10" : : "a"(0x0e00 | *string)
          );
          string++;
     }
}

The linkable file, ld is:

ENTRY(main);
SECTIONS
{
    . = 0x7C00;
    .text : AT(0x7C00)
    {
        *(.text);
    }
    .sig : AT(0x7DFE)
    {
        SHORT(0xaa55);
    }
} 

The makefile is:

bash -c "gcc -c -g -Os -march=i686 -m16 -ffreestanding -Wall -Werror ../project/kernel/bootloader/boot.c -o boot.o"
bash -c "ld -static -melf_i386 -Ttest.ld -nostdlib --nmagic -o boot.elf boot.o"
bash -c "objcopy -O binary boot.elf boot.bin"

I wonder that the function launch

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    _"the function don't launch"_. How did you conclude that? Have you stepped through the bootsector in a debugger? – Michael Feb 12 '21 at 19:58
  • Add `-fno-pic` to `gcc` command line. – Jester Feb 12 '21 at 20:25
  • `__asm__ __volatile__("movb $'H' , %al\n");` is an obvious bug; you write AL without telling the compiler about it, and expect AL to have the same value in a later asm statement instead of putting all the instructions in one asm statement. You got it right for the loop body, using `asm volatile("int $0x10" : : "a"(0x0e00 | *string))` a couple lines later. Probably this will happen to work, but abusing inline asm this way is not a good start. Don't use GNU C Basic Asm inside a non-naked function. – Peter Cordes Feb 12 '21 at 21:15
  • 1
    You might want to see this answer https://stackoverflow.com/a/57932741/3857942 – Michael Petch Feb 12 '21 at 22:06

0 Answers0