OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Aug 20, 2019 2:30 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Strange C Behaviour
PostPosted: Fri Apr 26, 2019 9:46 am 
Offline

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


Last edited by Optimizer on Fri Apr 26, 2019 11:04 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Strange C Behaviour
PostPosted: Fri Apr 26, 2019 10:06 am 
Offline

Joined: Fri Apr 26, 2019 10:03 am
Posts: 1
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.

Top
 Profile  
 
 Post subject: Re: Strange C Behaviour
PostPosted: Fri Apr 26, 2019 10:11 am 
Offline

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


Top
 Profile  
 
 Post subject: Re: Strange C Behaviour
PostPosted: Fri Apr 26, 2019 10:39 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 3444
Location: Chichester, UK
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.


Top
 Profile  
 
 Post subject: Re: Strange C Behaviour
PostPosted: Fri Apr 26, 2019 10:51 am 
Offline
Member
Member
User avatar

Joined: Wed Dec 12, 2018 12:16 pm
Posts: 119
Location: Chile
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.


Top
 Profile  
 
 Post subject: Re: Strange C Behaviour
PostPosted: Fri Apr 26, 2019 10:52 am 
Offline
Member
Member
User avatar

Joined: Wed Dec 12, 2018 12:16 pm
Posts: 119
Location: Chile
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.


Top
 Profile  
 
 Post subject: Re: Strange C Behaviour
PostPosted: Fri Apr 26, 2019 11:00 am 
Offline

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


Top
 Profile  
 
 Post subject: Re: Strange C Behaviour
PostPosted: Fri Apr 26, 2019 11:01 am 
Offline

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


Top
 Profile  
 
 Post subject: Re: Strange C Behaviour
PostPosted: Fri Apr 26, 2019 12:12 pm 
Offline

Joined: Sun Nov 23, 2008 5:56 am
Posts: 24
Location: Russia, Saint-Petersburg
Are you sure you are loading enough sectors from the disk?


Top
 Profile  
 
 Post subject: Re: Strange C Behaviour
PostPosted: Fri Apr 26, 2019 12:34 pm 
Offline
Member
Member
User avatar

Joined: Wed Dec 12, 2018 12:16 pm
Posts: 119
Location: Chile
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


Top
 Profile  
 
 Post subject: Re: Strange C Behaviour
PostPosted: Fri Apr 26, 2019 12:36 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 226
Are you linking in a section called ".rodata"? It belongs just past the .text section. It's where all the strings are stored.


Top
 Profile  
 
 Post subject: Re: Strange C Behaviour
PostPosted: Fri Apr 26, 2019 12:44 pm 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 466
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.


Top
 Profile  
 
 Post subject: Re: Strange C Behaviour
PostPosted: Fri Apr 26, 2019 1:46 pm 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 466
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.


Top
 Profile  
 
 Post subject: Re: Strange C Behaviour
PostPosted: Fri Apr 26, 2019 1:54 pm 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 466
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.


Top
 Profile  
 
 Post subject: Re: Strange C Behaviour
PostPosted: Fri Apr 26, 2019 10:09 pm 
Offline

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: linguofreak, quadrant and 8 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