Page 1 of 1

Just can't get what's wrong..

Posted: Mon Jun 06, 2005 11:00 pm
by dXtr
hi. I'm trying out some things with DJGPP and NASM.
For the moment I'm experementing with compiling things into com files.
I've succeded compiling this into a com file:

Code: Select all

start.asm:
[BITS 32]

[global start]
[extern _main]

start:
	call	_main
	ret

main.c
void main(void)
{
	__asm__	(
	"mov	$'e', %%al\n\t"
	"mov	$0x0E, %%ah\n\t"
	"xor	%%bl, %%bl\n\t"
	"int	$0x10\n\t"
	:
	:
	);
}
using this linkscript:

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
SECTIONS
{
	.text 0x100 :
	{
		code = .; _code = .; __code = .;
		*(.text)
	}

	.data :
	{
		data = .; _data = .; __data = .;
		*(.data)
	}

	.bss :
	{
		bss = .; _bss = .; __bss = .;
		*(.bss)
	}

	end = .; _end = .; __end = .;
}
but now I wanted to try something a little more "advanced" with functions:

Code: Select all

still same start.asm

main.c:
void putch(char c)
{
	__asm__	(
	"mov	%0, %%al\n\t"
	"mov	$0x0E, %%ah\n\t"
	"xor	%%bl, %%bl\n\t"
	"int	$0x10\n\t"
	:
	: "r" (c)
	);
}

void main(void)
{
	putch('H');
}
this dosen't work and I can't figure out why.. especially after seeing the the disassembly (made some changes so I can run it in nasm):

Code: Select all

[bits 32]
[org 0x100]

start:	call	_main
	ret


; void putch(char c)
_putch:	push	ebp
	mov	ebp, esp

	; Print a character
	mov	al, [ebp+0x8]
	mov	al, al
	mov	ah, 0x0E
	xor	bl, bl
	int	0x10

	pop	ebp
	ret


; void main(void)
_main:	push	ebp

	; putch('H');
	; Print a character
	mov	al, 0x48	; H
	mov	ebp, esp
	mov	ah, 0x0E
	xor	bl, bl
	int	0x10

	pop	ebp
	ret
is there something I'm missing?
(sorry for the long message ;) )

edit:
I noticed if I remove the

Code: Select all

	pop	ebp
from the putch function in the asm file. the H letter gets printed out but not else =/

Re: Just can't get what's wrong..

Posted: Tue Jun 07, 2005 11:00 pm
by digo_rp
go to www.osdever.net/downlods and get kernel3.zip it is in gcc and nasm, then you need bootloader that load pmode image... try bootf02.zip, it start pm do paging and jump to pmode image.

Re: Just can't get what's wrong..

Posted: Tue Jun 07, 2005 11:00 pm
by dXtr
using hiew (a hex/disasm like program) I think I found the problem...

output from hiew:

Code: Select all

00000000: E81100	call	000000014   -------- (1)
00000003: 0000		add	[bx][si],al
00000005: C3		retn
00000006: 55		push	bp
00000007: 89E5		mov	bp,sp
00000009: 8A4508	mov	al,[di][00008]
0000000C: 88C0		mov	al,al
0000000E: B40E		mov	ah,00E ;"?"
00000010: 30DB		xor	bl,bl
00000012: CD10		int	010
00000014: 5D		pop	bp
00000015: C3		retn
00000016: 55		push	bp
00000017: B048		mov	al,048 ;"H"
00000019: 89E5		mov	bp,sp
0000001B: B40E		mov	ah,00E ;"?"
0000001D: 30DB		xor	bl,bl
0000001F: CD10		int	010
00000021: 5D		pop	bp
00000022: C3		retn
it treats the 32bit adress as a 16bit so the program calls 0x14 instead of 0x16 as it should... or something like that ;)
I tried chaenging [bits 32] to [bits 16] and like magic I got a 'H' printed out. yay! :D

but why can't I use [bits 32] ?

Re: Just can't get what's wrong..

Posted: Tue Jun 07, 2005 11:00 pm
by dXtr
digo_rp wrote:go to www.osdever.net/downlods and get kernel3.zip it is in gcc and nasm, then you need bootloader that load pmode image... try bootf02.zip, it start pm do paging and jump to pmode image.
so I have to do it this way to test it? I can't do as I've done now creating a com file and running it under windows? =/

Re: Just can't get what's wrong..

Posted: Tue Jun 07, 2005 11:00 pm
by digo_rp
I?m just start to learn pmode too, as I discover gcc is 32bits pmode image... you need some program that start pmode and load that image created by you...

don?t know! this is the bestway I found, I think alexei founze has a loader.exe that can load your image created . I?ll take a loot for you and send it the link to you

Re: Just can't get what's wrong..

Posted: Tue Jun 07, 2005 11:00 pm
by dXtr
thanks.
but it's not that important it was mostly so I could try out my own libc that I'm writting and also to try if it was possibly to compile a file into a com file. now I'm sitting and doing some experementing with mixing asm and c instead. :)

edit:
now for an other question :D

lets say I do this in C:
print("Hello");

print is an asm func. thats takes one argument pushed to it:

Code: Select all

_print:
	push	ebp
	mov	ebp, esp

	mov	eax, [ebp+8]

	pop	ebp
	ret
the adress to the string is now in eax, but how do I move this adress to ds:si instead?


Never mind think.. I got it to work now :)

Re: Just can't get what's wrong..

Posted: Tue Jun 07, 2005 11:00 pm
by rexlunae
dXtr wrote:but why can't I use [bits 32] ?
The problem is a misunderstanding of the meaning of the bits 32 and bits 16 directive. You are correct that in a DOS *.com file, you must use bits 16, at least to start. This directive does not control how the program is loaded. It is informational for Nasm, so that it knows which mode your code is running in. If you tell Nasm bits 16, it assumes you are running in a 16-bit segment, which is the only way that DOS runs *.com files. However, if you use 32-bit instructions while in "bits 16" mode (such as using 32-bit registers), Nasm will automatically insert overrides so that the 32-bit instructions work.

DOS *.com files are basically raw binaries. They are loaded as images at an offset (0x100 maybe...don't remember for sure) within a 16-bit segment. There is no header stored on disk, so there is no way for DOS to allow *.com files to be loaded any other way.

Re: Just can't get what's wrong..

Posted: Wed Jun 08, 2005 11:00 pm
by dXtr
ok then I understand it better :)

Re: Just can't get what's wrong..

Posted: Wed Jun 08, 2005 11:00 pm
by digo_rp
if you are passing the address of "hello" you have to take care the "hello" adress size example may be word or dword in some cases the ebp in asm should be mov eax, [ebp +6] and not +8