Page 1 of 2

Strange C Behaviour

Posted: Fri Apr 26, 2019 9:46 am
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

Re: Strange C Behaviour

Posted: Fri Apr 26, 2019 10:06 am
by Dingusnin
Is this in protected mode? If so, could you share your gdt. -snip dumb misreading-

Re: Strange C Behaviour

Posted: Fri Apr 26, 2019 10:11 am
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.

Re: Strange C Behaviour

Posted: Fri Apr 26, 2019 10:39 am
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.

Re: Strange C Behaviour

Posted: Fri Apr 26, 2019 10:51 am
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.

Re: Strange C Behaviour

Posted: Fri Apr 26, 2019 10:52 am
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.

Re: Strange C Behaviour

Posted: Fri Apr 26, 2019 11:00 am
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

Re: Strange C Behaviour

Posted: Fri Apr 26, 2019 11:01 am
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

Re: Strange C Behaviour

Posted: Fri Apr 26, 2019 12:12 pm
by quirck
Are you sure you are loading enough sectors from the disk?

Re: Strange C Behaviour

Posted: Fri Apr 26, 2019 12:34 pm
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

Re: Strange C Behaviour

Posted: Fri Apr 26, 2019 12:36 pm
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.

Re: Strange C Behaviour

Posted: Fri Apr 26, 2019 12:44 pm
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.

Re: Strange C Behaviour

Posted: Fri Apr 26, 2019 1:46 pm
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.

Re: Strange C Behaviour

Posted: Fri Apr 26, 2019 1:54 pm
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.

Re: Strange C Behaviour

Posted: Fri Apr 26, 2019 10:09 pm
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