help with c code

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
thomas

help with c code

Post by thomas »

ive commentated this code so people can read it and hopefully fix the problems gcc says go to 82.36.200.57/shot.jpg for a screenshot of all the problems

main.c

Code: Select all

#include <system.h>

   int gdt_install();
   int idt_install();
   int isrs_install();
   int irq_install();
   int init_video();
   float timer_install();
   float keyboard_install();

/* copys 'count' bytes'bytes of data from 'src' to
   *  'dest', finally return 'dest' */
void *memcpy(void *dest, const void *src, size_t count)
{
    const char *sp = (const char *)src;
    char *dp = (char *)dest;
    for(; count != 0; count--) *dp++ = *sp++;
    return dest;
}

/*  sets 'count' bytes in 'dest' to 'val'.
    *  Again, return 'dest' */
void *memset(void *dest, char val, size_t count)
{
    char *temp = (char *)dest;
    for( ; count != 0; count--) *temp++ = val;
    return dest;
}
/* Same as above, but this time, we're working with a 16-bit
    *  'val' and dest pointer. Your code can be an exact copy of
    *  the above, provided that your local variables if any, are
    *  unsigned short */
unsigned short *memsetw(unsigned short *dest, unsigned short val, size_t count)
{
    unsigned short *temp = (unsigned short *)dest;
    for( ; count != 0; count--) *temp++ = val;
    return dest;
}
 /* This loops through character array 'str', returning how
    *  many characters it needs to check before it finds a 0.
    *  In simple words, it returns the length in bytes of a string */
size_t strlen(const char *str)
{
    size_t retval;
    for(retval = 0; *str != '\0'; str++) retval++;
    return retval;
}
//reads I/O ports
unsigned char inportb (unsigned short _port)
{
    unsigned char rv;
    __asm__ __volatile__ ("inb %1, %0" : "=a" (rv) : "dN" (_port));
    return rv;
}
//writes I/O ports
void outportb (unsigned short _port, unsigned char _data)
{
    __asm__ __volatile__ ("outb %1, %0" : : "dN" (_port), "a" (_data));
}
//simple main function
void main()
{
    int i;

    gdt_install();
    idt_install();
    isrs_install();
    irq_install();
    init_video();
    timer_install();
    keyboard_install();

    __asm__ __volatile__ ("sti");

    puts("test\n");

//    i = 10 / 0;
//    putch(i);

    for (;;);
}

system.h

Code: Select all

#ifndef __SYSTEM_H
#define __SYSTEM_H

/* MAIN.C */
extern unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count);
extern unsigned char *memset(unsigned char *dest, unsigned char val, int count);
extern unsigned short *memsetw(unsigned short *dest, unsigned short val, int count);
extern int strlen(const char *str);
extern unsigned char inportb (unsigned short _port);
extern void outportb (unsigned short _port, unsigned char _data);

#endif

Curufir

Re:help with c code

Post by Curufir »

Learn C before writing your OS. Struggling with the language at the same time you're learning OS concepts is not fun.

Code: Select all

  int gdt_install();
  int idt_install();
  int isrs_install();
  int irq_install();
  int init_video();
  float timer_install();
  float keyboard_install();
You declare these function prototypes, then later try and use these functions, without ever writing the functions.

Code: Select all

extern unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count);
extern unsigned char *memset(unsigned char *dest, unsigned char val, int count);
extern unsigned short *memsetw(unsigned short *dest, unsigned short val, int count);
extern int strlen(const char *str);
These function prototypes do NOT match the functions in main.c.

Code: Select all

size_t retval;
size_t is NOT an internal compiler type. It's defined in stddef.h. Fix this by having 'typedef unsigned long size_t' somewhere before you use size_t.

***

Got bored at this point so I didn't bother checking functionality.
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:help with c code

Post by distantvoices »

Hm. this ip address - is that your own server/pc?

You *do* know, that some black hats might be lured into playing some tricks on you?

BTW: i don't want to know what you've done to your previous thread so that one can't open it anymore.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
thomas

Re:help with c code

Post by thomas »

thanks i know c++ but C is quite different
Poseidon

Re:help with c code

Post by Poseidon »

beyond infinity wrote: Hm. this ip address - is that your own server/pc?

You *do* know, that some black hats might be lured into playing some tricks on you?

BTW: i don't want to know what you've done to your previous thread so that one can't open it anymore.
when they want to hack on ip, why should they use that one.. it's easy to get the ip from a domain..

EDIT: they will get it anyway.. in every post right at the bottom is the origin IP.. ;D
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:help with c code

Post by Solar »

Erm... looking at that piece of code, I'd daresay you don't "know" C++ either.

I second Curufir: Learn the language first before tackling OS development.
Every good solution is obvious once you've found it.
Post Reply