Using assembly with C and stack

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
leosa99
Posts: 21
Joined: Tue Aug 30, 2016 9:34 am
Libera.chat IRC: leosa99

Using assembly with C and stack

Post by leosa99 »

Hi,

I have the following c code (run in protected mode, all segments well defined):

Code: Select all

extern void _printcool();
extern void _wait();

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

The second assembly code defines both functions:

Code: Select all

[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 !
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Using assembly with C and stack

Post by Octocontrabass »

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?
leosa99
Posts: 21
Joined: Tue Aug 30, 2016 9:34 am
Libera.chat IRC: leosa99

Re: Using assembly with C and stack

Post by leosa99 »

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.
leosa99
Posts: 21
Joined: Tue Aug 30, 2016 9:34 am
Libera.chat IRC: leosa99

Re: Using assembly with C and stack

Post by leosa99 »

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.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Using assembly with C and stack

Post by iansjack »

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?
User avatar
Maddie
Posts: 13
Joined: Tue Aug 10, 2021 4:22 pm
Libera.chat IRC: maddie

Re: Using assembly with C and stack

Post by Maddie »

I think you need to respect the calling convention for your compiler.
Post Reply