Assembly function call from c -


i cannot combine kernel_entry.asm , main.c. main.c calls asm function sum. both nasm , gcc compiles respective files. however, linker gives error.

kernel_entry.asm:

[bits 32] [extern _start] [global _sum]  ....  _sum:     push    ebp                  mov     ebp, esp     mov     eax, [ebp+8]         mov     ecx, [ebp+12]        add     eax, ecx             pop     ebp                  ret  

main.c:

.... extern int sum();  void start() {     ....     int x = sum(4, 5);     .... } 

to compile source files, use following commands:

nasm kernel_entry.asm -f win32 -o kernel_entry.o gcc -ffreestanding -c main.c -o main.o .... ld -t nul -o kernel.tmp -ttext 0x1000 kernel_entry.o main.o mem.o port_in_out.o screen.o idt.o 

linker gives following error:main.o:main.c:(.text+0xa82): undifened reference 'sum'. tried couldn't find solution. when remove asm function call main.c, works.

the tl;dr version of answer mixing nasm's -f win32 generates object file not compatible gnu toolchain on windows - need use -f elf if want link using ld. described in nasm's documentation here under sections 7.5 , 7.9.

the hint me running nm kernel_entry.o generated:

00000000 .absolut 00000000 t .text 00000001 @feat.00          u _start          u _sum 

which shows sum undefined symbol. after compiling elf, got:

         u _start 00000000 t _sum 

indicating sum recognised symbol in text section.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -