Page 1 of 1

gcc 16bit

Posted: Fri Jun 08, 2007 5:46 am
by xyjamepa
Hi...
I'm using DJGPP to write my kernel,
does any one know how to create a 16 bit code with gcc,
there is wrong with http://www.delorie.com it doesn't open.

Re: gcc 16bit

Posted: Fri Jun 08, 2007 7:32 am
by AJ
abuashraf wrote: there is wrong with http://www.delorie.com it doesn't open.
Works ok for me. If you want to navigate directly, try http://www.delorie.com/djgpp/ .

Posted: Fri Jun 08, 2007 8:07 am
by JAAman
one note: while GCC can produce 16bit code, it is not true 16bit code, and may not work properly in RMode/VMode and certainly wont work on a 8086/8

all it does is prefix all 32bit references with an override (and removes prefixes from 16bit references), this makes it suitable for use in a 16bit PMode segment, but may or may not work in RMode (and definitely wont work if you must use different segments -- GCC assumes DS.base=SS.base=ES.base, and if you need more than one data segment, it may not work properly)

i dont know what you want to use it for, but this is something important to keep in mind

Posted: Fri Jun 08, 2007 8:48 am
by binutils
wayttd?
http://freshmeat.net/projects/bin86/
16-bit code in GAS: objdump --disassemble -mi8086

Posted: Fri Jun 08, 2007 9:28 am
by xyjamepa
Hi...
i dont know what you want to use it for
I need it for my virtual task,virtul mode doesn't accept 32bit,
and It gives gpf,so would some one please give me a simple example
on how to create 16bit code using GCC,or any link speaks about that.
Works ok for me. If you want to navigate directly, try http://www.delorie.com/djgpp/ .
It didn't work again...I don't know maybe the wrong comes form my server.

Thanx.

Posted: Fri Jun 08, 2007 12:15 pm
by earlz
try http://207.22.48.162 your DNS server might be stupid and not work on certain websites..I use to have that problem, you can fix it by setting your DNS server to 216.231.41.2
that is a very good free DNS server(it's what I use)


I saw a GCC based 16bit compiler..it didn't support segments iirc, but it didn't use 32bit opcodes..


edited...

Posted: Fri Jun 08, 2007 1:42 pm
by jnc100
I think, although I haven't tested this, if you add the line:

Code: Select all

asm(".code16gcc\n");
to the start of each C file you wish to be compiled as 16 bit code, then gas will generate proper 16-bit code (not 32-bit with the 66 prefix). It limits you to a single segment, however (ie. 64kB code, 64kB data) and you can't use anything that assumes 32-bit operation, e.g. long ints and the like.

edit: if anyone's interested, http://gujin.org has a boot loader written in 16-bit code using gcc.

edit2: and open watcom natively supports 16-bit, if you can tear yourself away from gcc...

Regards,
John.

Posted: Fri Jun 08, 2007 5:18 pm
by jnc100
A bit of an update.

Using the method I described above, it is possible to have gcc generate code that will execute in real mode (and presumably v8086), but only on a 386 or above, as it still insists on using the 32-bit registers, even for 2 byte wide variables. Be prepared to get lots of 66 and 67 prefixes.

In actuality it is gas which produces the 16-bit code - the asm instruction just inserts the .code16gcc line at the top of the intermediate assembly file.

In addition, as it is gcc, it is completely unaware of segments. Therefore, if you wish to access any memory outside the segment which ds/es are set to, you need to create your own farpeek/farpoke functions with inline asm (see [wiki]Inline_Assembly/Examples[/wiki] - I can't seem to link to this, its something to do with the slash in the title).

As a small test, I compiled the following, both with and without the .code16asm header. I attach the disassembly for reference. No optimisations were used in the compilation, otherwise this would have become really small :wink:

Code: Select all

asm(".code16gcc\n");

int add2(int a);

int main()
{
        int a = 31;

        int b = add2(a);

        return b;
}

int add2(int a)
{
        return a + 2;
}

If you use short ints instead of ints then the code produced still uses 32-bit registers with prefixes, but the values on the stack are 2-byte aligned.

The .code16gcc option can be changed to .code16, which is similar in its output except that it does not necessarily manipulate the stack pointer in the same way as gcc does making it more difficult to walk the stack from a 32-bit monitor.

The disassembly, produced by the NASM disassembler is attached.

Regards,
John.

Posted: Fri Jun 08, 2007 9:46 pm
by earlz
Really, if you could find a way past the one segment bit in g++ by making your own far_pointer class...

What about the gcc that minix uses? it would be 16bit wouldn't it?

Posted: Fri Jun 08, 2007 9:54 pm
by Brynet-Inc
hckr83 wrote:Really, if you could find a way past the one segment bit in g++ by making your own far_pointer class...

What about the gcc that minix uses? it would be 16bit wouldn't it?
Uhh, The early Minix releases used "ACK" as a C compiler, Which seems to have a new life here: http://tack.sourceforge.net/

Supports the 8086 and 80386.. Is even support for other systems as well.. :)