Page 1 of 1

help with c code

Posted: Sun Feb 27, 2005 6:20 am
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


Re:help with c code

Posted: Sun Feb 27, 2005 7:23 am
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.

Re:help with c code

Posted: Sun Feb 27, 2005 7:31 am
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.

Re:help with c code

Posted: Sun Feb 27, 2005 8:11 am
by thomas
thanks i know c++ but C is quite different

Re:help with c code

Posted: Sun Feb 27, 2005 9:26 am
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

Re:help with c code

Posted: Mon Feb 28, 2005 2:13 am
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.