Segmentation fault problem with C & Inline Assembly code

Programming, for all ages and all languages.
Post Reply
SmartBoy

Segmentation fault problem with C & Inline Assembly code

Post by SmartBoy »

Hi ...

i have this code :

void print()
{
char massege = 'H';
char t;
asm("movb $0x0E,%ah");
asm("movb %0,%%al"
: // No
: "r"(massege)
);
asm("int $0x10");
}

int main()
{
print();
}

it's compiled without any problem , but when i want to run the programme it's show for me segmentation fault problem , so what is the error in this code?

GCC -> Linux

sorry for my bad English .
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:Segmentation fault problem with C & Inline Assembly c

Post by distantvoices »

You are by any chance trying to put a character to screen with bios-interrupt 0x10?

This doesn't work in a protected mode environment as you can see: you get a seg-fault.

what you need to do is (if you want to do it this way under all circumstances) check how to issue vm86 calls with Linux. It isn't trivial but neither it is hard to achieve as far as I recall.

Stay safe & remember: google is your best friend. ;-) Try these lemmata: vm86 linux
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Segmentation fault problem with C & Inline Assembly c

Post by Pype.Clicker »

i suppose you don't ignore the presence of the standard library function "printf" and have a very good reason to attempt using BIOS calls to print on a text screen from an operating environment that has probably set up a graphic video mode and uses hardware acceleration to blit fonts on screen through the framebuffer, right ?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Segmentation fault problem with C & Inline Assembly c

Post by Solar »

@ SmartBoy:

Your print() function is written in a way you would do things if you were writing your own operating system kernel.

If you do, you need more to get it running - a bootloader, Assembler startup code, your binaries written to some boot media, and the computer booted off that media.

If that is what you want to do, visit the Quick Linkz thread (found in my signature) and the FAQ (found by clicking the banner on top of this page).

Taking kernel-space code constructs and trying to execute them as any other application (in user space) will fail.
Every good solution is obvious once you've found it.
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:Segmentation fault problem with C & Inline Assembly c

Post by distantvoices »

I'd avoid using int 0x10 out of speed reasons even inside a bootloader:

just put the characters directly to the memory area responsible for screen output: 0xb8000 (is that one correct? dunno exactly such a shame)
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
bluecode

Re:Segmentation fault problem with C & Inline Assembly c

Post by bluecode »

beyond infinity wrote:0xb8000 (is that one correct? dunno exactly such a shame)
That's correct. :D
Post Reply