OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 3:05 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Using assembly with C and stack
PostPosted: Tue Aug 17, 2021 4:23 pm 
Offline

Joined: Tue Aug 30, 2016 9:34 am
Posts: 21
Hi,

I have the following c code (run in protected mode, all segments well defined):
Code:
extern void _printcool();
extern void _wait();

int kmain()
{
_printcool();
while(1==1){}
return 0;
}



The second assembly code defines both functions:

Code:
[bits 32]
global _printcool
global _wait

_printcool:
mov byte [0xB8000], 'R'
mov byte [0xB8001], 0x52
ret

_wait:
jmp _wait


I build them using :
nasm -f elf asmfile -o asmfile.o
gcc -ffreestanding -m32 -fno-pic -fno-pie -c kernel.c -o kernel.o
ld --oformat binary -Ttext 0x1000 -melf_i386 asmfile.o kernel.o -o kernel.bin

Then I run my bootloader with this kernel, like before, but it triple fault every time.

The ret is responsible of the triple fault, but I don't understand what should I do, I think the problem comes from the stack, I dont know how to clean it before using ret instruction.

Thank you !


Top
 Profile  
 
 Post subject: Re: Using assembly with C and stack
PostPosted: Tue Aug 17, 2021 6:34 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
leosa99 wrote:
gcc -ffreestanding -m32 -fno-pic -fno-pie -c kernel.c -o kernel.o

You should be using a cross-compiler.

leosa99 wrote:
The ret is responsible of the triple fault, but I don't understand what should I do, I think the problem comes from the stack, I dont know how to clean it before using ret instruction.

How do you know the RET is responsible for the triple fault?


Top
 Profile  
 
 Post subject: Re: Using assembly with C and stack
PostPosted: Wed Aug 18, 2021 3:36 am 
Offline

Joined: Tue Aug 30, 2016 9:34 am
Posts: 21
I am not using a cross compiler for school reasons, I know the ret is responsible because if I replace it by a jump $, there is no triple fault.


Top
 Profile  
 
 Post subject: Re: Using assembly with C and stack
PostPosted: Wed Aug 18, 2021 5:24 am 
Offline

Joined: Tue Aug 30, 2016 9:34 am
Posts: 21
After using a cross-compiler (target elf i686), the same commands with nasm, gcc and ld. It still produces a triple fault on the ret instruction.


Top
 Profile  
 
 Post subject: Re: Using assembly with C and stack
PostPosted: Wed Aug 18, 2021 6:18 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
Single-step your code under a debugger to find out where it is going wrong and what the surrounding code looks like. That should give you a good idea of what the problem is.

My guess would be an invalid stack. There's not much else to go wrong in that code snippet.

As a matter of interest, what value do you set your stack pointer to?


Top
 Profile  
 
 Post subject: Re: Using assembly with C and stack
PostPosted: Wed Aug 18, 2021 11:34 am 
Offline
User avatar

Joined: Tue Aug 10, 2021 4:22 pm
Posts: 13
I think you need to respect the calling convention for your compiler.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 64 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group