I have tried to compile C code to run in my x86 protected-mode environment.
Because I am using Ubuntu 14 64bit I use the -m32 to compile for a 32bit target.
The c-code:
Code: Select all
int my_function()
{
return 0xbaba;
}
gcc -ffreestanding -c kernel.c -o kernel.o -m32
ld kernel.o --oformat binary -Ttext 0x0 -o kernel.bin -melf_i386
The output of 'objdump -d kernel.o' is:
Code: Select all
kernel.o: file format elf32-i386
Disassembly of section .text:
00000000 <my_function>:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: b8 ba ba 00 00 mov $0xbaba,%eax
8: 5d pop %ebp
9: c3 ret
Code: Select all
00000000 55 push bp
00000001 89E5 mov bp,sp
00000003 B8BABA mov ax,0xbaba
00000006 0000 add [bx+si],al
00000008 5D pop bp
00000009 C3 ret
0000000A 0000 add [bx+si],al
0000000C 1400 adc al,0x0
0000000E 0000 add [bx+si],al
00000010 0000 add [bx+si],al
00000012 0000 add [bx+si],al
00000014 017A52 add [bp+si+0x52],di
00000017 0001 add [bx+di],al
00000019 7C08 jl 0x23
0000001B 011B add [bp+di],bx
0000001D 0C04 or al,0x4
0000001F 0488 add al,0x88
00000021 0100 add [bx+si],ax
00000023 001C add [si],bl
00000025 0000 add [bx+si],al
00000027 001C add [si],bl
00000029 0000 add [bx+si],al
0000002B 00D4 add ah,dl
0000002D FF db 0xff
0000002E FF db 0xff
0000002F FF0A dec word [bp+si]
00000031 0000 add [bx+si],al
00000033 0000 add [bx+si],al
00000035 41 inc cx
00000036 0E push cs
00000037 08850242 or [di+0x4202],al
0000003B 0D0546 or ax,0x4605
0000003E C50C lds cx,[si]
00000040 0404 add al,0x4
00000042 0000 add [bx+si],al
So i guess the problem is, that I use ld wrong. But what do I have to do to get a correct output?