can't print text to string in c

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
poseidon

can't print text to string in c

Post by poseidon »

i tried in my small kernel to print some text to the screen. this didn't work, so i put this in my kernel loader:
movl $0x07690748,0xb8000

this did work, it wrote hi in the top-left part of my screen. I tried the same in c, at the very beginning of my main function:
*((int*)0xb8000)=0x07690748;

but this didn't work.
What's wrong, and how can i fix it?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:can't print text to string in c

Post by Pype.Clicker »

might it be that your data segment's base is not 0 ?
poseidon

Re:can't print text to string in c

Post by poseidon »

i'm just a beginner at kernel programming so i don't know exactly what you mean by that, but i tried objdump, and this is my output:

Code: Select all

Sections:
Idx Name      Size      Vma      LMA      file off   Algn
  0 .text      000001a0   00100000   00100000   00001000   2**2
         CONTENTS, ALLOC, LOAD, CODE
  1 .data      00000000   00101000   00101000   00002000   2**2
         CONTENTS, ALLOC, LOAD, DATA
  2. bss      00004000   00101000   00101000   00002000   2**2
poseidon

Re:can't print text to string in c

Post by poseidon »

i disassembled my kernel with objdump-elf, and this is a piece of the output:

Code: Select all

0010000c <_loader>:
  10000c:   bc 00 10 10 00    movl   $0x101000,%esp
  100011:   50                pushl  %eax
  100012:   53                pushl  %ebx
  [b]100013:   e8 30 00 00 00    call   100048 <__main+0x18>[/b]
  100018:   c7 05 00 80 0b    movl   $0x7690748,0xb8000
  10001d:   00 48 07 69 07 
  100022:   f4                hlt    
  100023:   90                nop    
  100024:   90                nop    
  100025:   90                nop    
  100026:   90                nop    
  100027:   90                nop    
  100028:   90                nop    
  100029:   90                nop    
  10002a:   90                nop    
  10002b:   90                nop    
  10002c:   90                nop    
  10002d:   90                nop    
  10002e:   90                nop    
  10002f:   90                nop    

00100030 <__main>:
  100030:   55                pushl  %ebp
  100031:   89 e5             movl   %esp,%ebp
  100033:   8d b6 00 00 00    leal   0x0(%esi),%esi
  100038:   00 
  100039:   8d bc 27 00 00    leal   0x0(%edi,1),%edi
  10003e:   00 00 
  100040:   eb fe             jmp    100040 <__main+0x10>
  100042:   90                nop    
  100043:   90                nop    
  100044:   90                nop    
  100045:   90                nop    
  100046:   90                nop    
  100047:   90                nop    
  [b]100048:   90                nop    [/b]
  100049:   90                nop    
  10004a:   90                nop    
  10004b:   90                nop    
  10004c:   90                nop    
  10004d:   90                nop    
  10004e:   90                nop    
  10004f:   90                nop    

it looks like it jumps over the main function. this is my _loader:

Code: Select all

.global _loader           # making entry point visible to linker
.extern __main

# setting up the Multiboot header - see GRUB docs for details
.set ALIGN,    1<<0             # align loaded modules on page boundaries
.set MEMINFO,  1<<1             # provide memory map
.set FLAGS,    ALIGN | MEMINFO  # this is the Multiboot 'flag' field
.set MAGIC,    0x1BADB002       # 'magic number' lets bootloader find the header
.set CHECKSUM, -(MAGIC + FLAGS) # checksum required

.align 4
.long MAGIC
.long FLAGS
.long CHECKSUM

# reserve initial kernel stack space
.set STACKSIZE, 0x4000          # that's 16k.
.comm stack, STACKSIZE, 32      # reserve 16k stack on a quadword boundary

_loader:       mov   $(stack + STACKSIZE), %esp # set up the stack
               pushl  %eax                       # Multiboot magic number
               pushl  %ebx                       # Multiboot data structure
               call  __main         # call kernel proper
         movl $0x07690748,0xb8000
               hlt                          # halt machine should kernel return
User avatar
Candy
Member
Member
Posts: 3883
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:can't print text to string in c

Post by Candy »

I think you made an error while linking. The 000000030 is too obviously an indirection compared to the base of the data segment in my view for it to be an accident. It now jumps over the main and never returns, thus not calling your move-function ever.


How do you link? Do you have a linker script or something that explains it?

You don't use cli/hlt but plain hlt. This only halts until the next interrupt which will usually not take longer than 1/18th second. Did you disable them before this code?
poseidon

Re:can't print text to string in c

Post by poseidon »

i've added now cli before hlt, and this is my linker script:

Code: Select all

ENTRY (_loader)
OUTPUT_FORMAT (coff-i386)

SECTIONS
{
    . = 0x00100000;

    .text :
    {
        *(.text)
        *(.rodata)
    }

    .data ALIGN (0x1000) :
    {
        *(.data)
    }

    .bss :
    {
        _sbss = .;
        *(COMMON)
        *(.bss)
        _ebss = .;
    }
}
btw coff is converted to elf with objcopy, so that's no problem.

Has anyone an idea? Thanks in advance..
dh

Re:can't print text to string in c

Post by dh »

BareBone + Mod

Code: Select all

ENTRY (_loader)

SECTIONS
{
    . = 0x00100000;

    .text :
    {
        *(.text)
        *(.rodata)
    }

    .data ALIGN (0x1000) :
    {
        *(.data)
    }

    .bss :
    {
        _sbss = .;
        *(COMMON)
        *(.bss)
        _ebss = .;
    }
}
just tack on your format line for nice results!!
AR

Re:can't print text to string in c

Post by AR »

You don't use cli/hlt but plain hlt. This only halts until the next interrupt which will usually not take longer than 1/18th second. Did you disable them before this code?
Interrupts should technically already be disabled, There is a multiboot header in the code and the multiboot specification says IF must be cleared by the bootloader. So it isn't technically neccessary to clear IF but it is a good idea to note that for later.

As to the incorrect call, it has to be something wrong with the linking, there may be a bug in the version of LD you're using but thats very unlikely (in terms of completely bogus pointers). If you're using MinGW32/Cygwin though that may be your problem, I read in another thread that MinGW32 generates bad ELFs (The linker generates progressively worse pointers to functions in the call instruction) you may want to change compiler or just leave it as a COFF and use the 'a.out kludge' to load it correctly (Since GRUB can't read COFF).

EDIT: Here's the thread http://www.mega-tokyo.com/forum/index.p ... 5#msg57745
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:can't print text to string in c

Post by Pype.Clicker »

or you may check out the FAQ and see how you can build a cross-compiler that will nicely support ELF format.

Imho, you also may want to check the instruction stream in your intermediate COFF file ... it may have something to do with your objcopy (some missing parameter, etc) which could doom all the linker's effort to produce a nice file ...
Post Reply