Strange C Behaviour

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.
Optimizer
Posts: 11
Joined: Thu Apr 25, 2019 2:02 pm

Strange C Behaviour

Post by Optimizer »

When I write

Code: Select all

void main()
{
 print("");
}
void print(char* message)
{
 char c = 'X';
 char* vga = (char*)0xb8000;
 vga[offset] = c;
 vga[offset+1] = 0x0f;
}
code works fine.

but when I do this

Code: Select all

void main()
{
 print("X");
}

void print(char* message)
{
 char c = message[0];
 char* vga = (char*)0xb8000;
 vga[offset] = c;
 vga[offset+1] = 0x0f;
}
Instead of printing 'X' it prints ' '(blank space/ deletes character that's already there)

Here's my full code:

https://github.com/Optimizer0/IAOS
Last edited by Optimizer on Fri Apr 26, 2019 11:04 am, edited 1 time in total.
Dingusnin
Posts: 1
Joined: Fri Apr 26, 2019 10:03 am

Re: Strange C Behaviour

Post by Dingusnin »

Is this in protected mode? If so, could you share your gdt. -snip dumb misreading-
Last edited by Dingusnin on Fri Apr 26, 2019 10:30 am, edited 1 time in total.
Optimizer
Posts: 11
Joined: Thu Apr 25, 2019 2:02 pm

Re: Strange C Behaviour

Post by Optimizer »

Yes this is protected mode.

Here's my gdt.

Code: Select all

gdt_start:
    ; the GDT starts with a null 8-byte
    dd 0x0 ; 4 byte
    dd 0x0 ; 4 byte

; GDT for code segment. base = 0x00000000, length = 0xfffff
gdt_code: 
    dw 0xffff    ; segment length, bits 0-15
    dw 0x0       ; segment base, bits 0-15
    db 0x0       ; segment base, bits 16-23
    db 10011010b ; flags (8 bits)
    db 11001111b ; flags (4 bits) + segment length, bits 16-19
    db 0x0       ; segment base, bits 24-31

; GDT for data segment. base and length identical to code segment
gdt_data:
    dw 0xffff
    dw 0x0
    db 0x0
    db 10010010b
    db 11001111b
    db 0x0

gdt_end:

; GDT descriptor
gdt_descriptor:
    dw gdt_end - gdt_start - 1 ; size (16 bit), always one less of its true size
    dd gdt_start ; address (32 bit)

CODE_SEG equ gdt_code - gdt_start
DATA_SEG equ gdt_data - gdt_start
yeah but I can't use char to send strings can I?

Thank you.
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Strange C Behaviour

Post by iansjack »

I'd offer you the same advice as before, but you'd only ignore it so there's no point.

You're not showing us enough of your code for a sensible answer - link to your repository.
deleted8917
Member
Member
Posts: 119
Joined: Wed Dec 12, 2018 12:16 pm

Re: Strange C Behaviour

Post by deleted8917 »

Optimizer wrote:When I write

Code: Select all

void main()
{
 print("");
}
void print(char* message)
{
 char c = 'X';
 char* vga = (char*)0xb8000;
 vga[offset] = c;
 vga[offset+1] = 0x0f;
}
code works fine.

but when I do this

Code: Select all

void main()
{
 print("X");
}

void print(char* message)
{
 char c = message[0];
 char* vga = (char*)0xb8000;
 vga[offset] = c;
 vga[offset+1] = 0x0f;
}
Instead of printing 'X' it prints ' '(blank space/ deletes character that's already there)
Maybe because you're passing an string instead of a character... (change char* message to char message, and instead of "X" use 'X') ;) I don't know, we don't have enough info.
deleted8917
Member
Member
Posts: 119
Joined: Wed Dec 12, 2018 12:16 pm

Re: Strange C Behaviour

Post by deleted8917 »

Optimizer wrote:When I write

Code: Select all

void main()
{
 print("");
}
void print(char* message)
{
 char c = 'X';
 char* vga = (char*)0xb8000;
 vga[offset] = c;
 vga[offset+1] = 0x0f;
}
code works fine.

but when I do this

Code: Select all

void main()
{
 print("X");
}

void print(char* message)
{
 char c = message[0];
 char* vga = (char*)0xb8000;
 vga[offset] = c;
 vga[offset+1] = 0x0f;
}
Instead of printing 'X' it prints ' '(blank space/ deletes character that's already there)
Maybe because you're passing an string instead of a character... (change char* message to char message, and instead of "X" use 'X') ;) I don't know, we don't have enough info.
Optimizer
Posts: 11
Joined: Thu Apr 25, 2019 2:02 pm

Re: Strange C Behaviour

Post by Optimizer »

iansjack wrote:I'd offer you the same advice as before, but you'd only ignore it so there's no point.

You're not showing us enough of your code for a sensible answer - link to your repository.
Sir I didn't ignore your last message. I didn't debug right now because I couldn't get the gdb and qemu working. I didn't worry about this much cause I'm soon switching to linux.

I'm a beginner to this and by your stars I know you are far more experienced than me and I know what you say is important.

Here's my repo.
https://github.com/Optimizer0/IAOS
Optimizer
Posts: 11
Joined: Thu Apr 25, 2019 2:02 pm

Re: Strange C Behaviour

Post by Optimizer »

hextakatt wrote:
Optimizer wrote:When I write

Code: Select all

void main()
{
 print("");
}
void print(char* message)
{
 char c = 'X';
 char* vga = (char*)0xb8000;
 vga[offset] = c;
 vga[offset+1] = 0x0f;
}
code works fine.

but when I do this

Code: Select all

void main()
{
 print("X");
}

void print(char* message)
{
 char c = message[0];
 char* vga = (char*)0xb8000;
 vga[offset] = c;
 vga[offset+1] = 0x0f;
}
Instead of printing 'X' it prints ' '(blank space/ deletes character that's already there)
Maybe because you're passing an string instead of a character... (change char* message to char message, and instead of "X" use 'X') ;) I don't know, we don't have enough info.
It will work fine but I want pass strings
quirck
Member
Member
Posts: 42
Joined: Sun Nov 23, 2008 5:56 am
Location: Russia, Saint-Petersburg

Re: Strange C Behaviour

Post by quirck »

Are you sure you are loading enough sectors from the disk?
deleted8917
Member
Member
Posts: 119
Joined: Wed Dec 12, 2018 12:16 pm

Re: Strange C Behaviour

Post by deleted8917 »

Optimizer wrote:
iansjack wrote:I'd offer you the same advice as before, but you'd only ignore it so there's no point.

You're not showing us enough of your code for a sensible answer - link to your repository.
Sir I didn't ignore your last message. I didn't debug right now because I couldn't get the gdb and qemu working. I didn't worry about this much cause I'm soon switching to linux.

I'm a beginner to this and by your stars I know you are far more experienced than me and I know what you say is important.

Here's my repo.
https://github.com/Optimizer0/IAOS
1- Open qemu with the -s and -S flag
2- Open GDB and type target remote localhost:1234
nullplan
Member
Member
Posts: 1801
Joined: Wed Aug 30, 2017 8:24 am

Re: Strange C Behaviour

Post by nullplan »

Are you linking in a section called ".rodata"? It belongs just past the .text section. It's where all the strings are stored.
Carpe diem!
MichaelPetch
Member
Member
Posts: 798
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Strange C Behaviour

Post by MichaelPetch »

This is only a possible guess:

You aren't use a cross compiler. If the OS you are building on is 64-bit then it is likely generating 64-bit code. GCC will need the -m32 option and LD will need the option -melf_i386 added to them to generate and link 32-bit code. This is a guess based on similar problems with video output working inconsistently as you describe are often related to 64-bit code running in 32-bit protected mode. It won't work as expected. If this is your problem then I rote a Stackoverflow answer about something similar: https://stackoverflow.com/questions/398 ... y/39815993
If building 32bit kernels I recommend using an i686 (or i386) cross compiler. OSDev Wiki has information on doing that: https://wiki.osdev.org/GCC_Cross-Compiler
I see you are using Windows. If you are using a 64-bit toolchain you might have to use -mi386pe with LD.

I won't delete this but it appears this likely isn't your problem.
MichaelPetch
Member
Member
Posts: 798
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Strange C Behaviour

Post by MichaelPetch »

nullplan wrote:Are you linking in a section called ".rodata"? It belongs just past the .text section. It's where all the strings are stored.
Since he seems to be using a Windows GCC compiler(and not a cross compiler) he might need to use *(.rdata*)in the liner script.
MichaelPetch
Member
Member
Posts: 798
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Strange C Behaviour

Post by MichaelPetch »

I learned you were using MingGW 6.3.0 and Nullplan'ss general observation about the read only data section appears correct, except with MinGW the read only data sections are .rdata*. Try modifying your linker script to look like:

Code: Select all

SECTIONS
{
  . = 0x10000;
  .text : { *(.text*) }
  .rdata : { *(.rdata*) }
  .data : { *(.data) }
  .bss : { *(.bss) }
}
I also amended it to be *(.text*) as it is possible for GCC to create a number of .text sections that begin with .text.

It really is a symptom though that you are only reading 2 sectors in boot.asm. The default alignment with the MinGW linker is 4kb for sections that don't explicitly appear in the linker script. Your read only data happens to sit outside the sectors of the disk you did read so the memory where your string would have been was probably filled with zeros. Setting AL to 17 for int13h/ah=2 would be a start but hard coding the size is a hack.
Optimizer
Posts: 11
Joined: Thu Apr 25, 2019 2:02 pm

Re: Strange C Behaviour

Post by Optimizer »

hextakatt wrote:
Optimizer wrote:
iansjack wrote:I'd offer you the same advice as before, but you'd only ignore it so there's no point.

You're not showing us enough of your code for a sensible answer - link to your repository.
Sir I didn't ignore your last message. I didn't debug right now because I couldn't get the gdb and qemu working. I didn't worry about this much cause I'm soon switching to linux.

I'm a beginner to this and by your stars I know you are far more experienced than me and I know what you say is important.

Here's my repo.
https://github.com/Optimizer0/IAOS
1- Open qemu with the -s and -S flag
2- Open GDB and type target remote localhost:1234
I tried it but when I type commands like "continue" gdb says invalid remote reply
Post Reply