Compile an asm bootloader with external c code

后端 未结 3 1231
情歌与酒
情歌与酒 2020-12-30 15:48

I write a boot loader in asm and want to add some compiled C code in my project.

I created a test function here:

test.c

__as         


        
3条回答
  •  借酒劲吻你
    2020-12-30 16:22

    First i want to express how to link C compiled code with assembled file.

    I put together some Q/A in SO and reach to this.

    C code:

    func.c

    //__asm__(".code16gcc\n");when we use eax, 32 bit reg we cant use this as truncate 
    //problem
    
    #include 
    int  x = 0;
    
    
    
    int madd(int a, int b)
    {
        return a + b;
    }
    
    
    
    void mexit(){
          __asm__ __volatile__("mov $0, %ebx\n");
        __asm__ __volatile__("mov $1, %eax \n");
        __asm__ __volatile__("int $0x80\n");
    }
    
    
    char* tmp;
    
    ///how to direct use of arguments in asm command
    void print_str(int a,  char* s){
    
          x = a;
            __asm__("mov x, %edx\n");//  ;third argument: message length
    
            tmp = s;
            __asm__("mov tmp, %ecx\n");//  ;second argument: pointer to message to write
    
            __asm__("mov $1, %ebx\n");//first argument: file handle (stdout)
        __asm__("mov $4, %eax\n");//system call number (sys_write)
    
    
       __asm__ __volatile__("int $0x80\n");//call kernel
    
    }
    
    
    void mtest(){
        printf("%s\n", "Hi");
        //putchar('a');//why not work
    }
    
    
    ///gcc -c func.c -o func
    

    Assembly code:

    hello.asm

    extern  mtest
    extern  printf
    extern  putchar
    extern  print_str
    extern  mexit
    extern  madd
    
    section .text                   ;section declaration
    
                                    ;we must export the entry point to the ELF linker or
        global  _start              ;loader. They conventionally recognize _start as their
                                          ;entry point. Use ld -e foo to override the default.
    
    _start:
                                    ;write our string to stdout         
    
        push msg
            push len
            call print_str;
    
    
            call  mtest                                 ;print "Hi"; call printf inside a void function
    
    
    
                                                                    ; use add inside func.c
    
            push 5
            push 10                                                         
            call madd;              
                                                                    ;direct call of  printf()
            push  eax
        push  format
        call  printf;                           ;printf(format, eax)
    
    
    
    
            call mexit;                                     ;exit to OS
    
    
    section .data                   ;section declaration
    format db "%d", 10, 0
    msg db      "Hello, world!",0xa ;our dear string
    len equ     $ - msg             ;length of our dear string
    
    
    
    ; nasm -f elf32 hello.asm -o hello
    
    ;Link two files 
    ;ld hello func -o hl -lc -I /lib/ld-linux.so.2
    ; ./hl run code
    
    ;chain to assemble, compile, Run
    ;;   gcc -c func.c -o func && nasm -f elf32 hello.asm -o hello && ld hello func -o hl -lc -I /lib/ld-linux.so.2 && echo &&./hl
    

    Chain commands for assemble, compile and Run

    gcc -c func.c -o func && nasm -f elf32 hello.asm -o hello && ld hello func -o hl -lc -I /lib/ld-linux.so.2 && echo && ./hl

    Edit[toDO]

    • Write boot loader code instead of this version
    • Some explanation on how ld, gcc, nasm works.

提交回复
热议问题