OSDev.org
https://forum.osdev.org/

Strange C Behaviour
https://forum.osdev.org/viewtopic.php?f=1&t=33665
Page 1 of 2

Author:  Optimizer [ Fri Apr 26, 2019 9:46 am ]
Post subject:  Strange C Behaviour

When I write
Code:
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:
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

Author:  Dingusnin [ Fri Apr 26, 2019 10:06 am ]
Post subject:  Re: Strange C Behaviour

Is this in protected mode? If so, could you share your gdt. -snip dumb misreading-

Author:  Optimizer [ Fri Apr 26, 2019 10:11 am ]
Post subject:  Re: Strange C Behaviour

Yes this is protected mode.

Here's my gdt.
Code:
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.

Author:  iansjack [ Fri Apr 26, 2019 10:39 am ]
Post subject:  Re: Strange C Behaviour

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.

Author:  deleted8917 [ Fri Apr 26, 2019 10:51 am ]
Post subject:  Re: Strange C Behaviour

Optimizer wrote:
When I write
Code:
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:
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.

Author:  deleted8917 [ Fri Apr 26, 2019 10:52 am ]
Post subject:  Re: Strange C Behaviour

Optimizer wrote:
When I write
Code:
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:
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.

Author:  Optimizer [ Fri Apr 26, 2019 11:00 am ]
Post subject:  Re: Strange C Behaviour

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

Author:  Optimizer [ Fri Apr 26, 2019 11:01 am ]
Post subject:  Re: Strange C Behaviour

hextakatt wrote:
Optimizer wrote:
When I write
Code:
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:
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

Author:  quirck [ Fri Apr 26, 2019 12:12 pm ]
Post subject:  Re: Strange C Behaviour

Are you sure you are loading enough sectors from the disk?

Author:  deleted8917 [ Fri Apr 26, 2019 12:34 pm ]
Post subject:  Re: Strange C Behaviour

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

Author:  nullplan [ Fri Apr 26, 2019 12:36 pm ]
Post subject:  Re: Strange C Behaviour

Are you linking in a section called ".rodata"? It belongs just past the .text section. It's where all the strings are stored.

Author:  MichaelPetch [ Fri Apr 26, 2019 12:44 pm ]
Post subject:  Re: Strange C Behaviour

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.

Author:  MichaelPetch [ Fri Apr 26, 2019 1:46 pm ]
Post subject:  Re: Strange C Behaviour

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.

Author:  MichaelPetch [ Fri Apr 26, 2019 1:54 pm ]
Post subject:  Re: Strange C Behaviour

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:
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.

Author:  Optimizer [ Fri Apr 26, 2019 10:09 pm ]
Post subject:  Re: Strange C Behaviour

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

Page 1 of 2 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/