gcc 16bit

Programming, for all ages and all languages.
Post Reply
User avatar
xyjamepa
Member
Member
Posts: 397
Joined: Fri Sep 29, 2006 8:59 am

gcc 16bit

Post 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.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: gcc 16bit

Post 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/ .
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Post 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
User avatar
binutils
Member
Member
Posts: 214
Joined: Thu Apr 05, 2007 6:07 am

Post by binutils »

wayttd?
http://freshmeat.net/projects/bin86/
16-bit code in GAS: objdump --disassemble -mi8086
User avatar
xyjamepa
Member
Member
Posts: 397
Joined: Fri Sep 29, 2006 8:59 am

Post 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.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post 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...
Last edited by earlz on Fri Jun 08, 2007 1:54 pm, edited 1 time in total.
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post 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.
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post 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.
Attachments
test16.16.txt
compiled with .code16gcc
(1.05 KiB) Downloaded 288 times
test16.16-nogcc.txt
compiled with .code16
(1.04 KiB) Downloaded 235 times
test16-short.16.txt
short ints instead of ints, compiled with .code16gcc
(1.26 KiB) Downloaded 345 times
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post 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?
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post 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.. :)
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
Post Reply