OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Nov 24, 2020 9:23 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: for loops aren't working
PostPosted: Wed Oct 28, 2020 3:12 am 
Offline
User avatar

Joined: Tue Mar 31, 2020 6:28 pm
Posts: 11
https://github.com/ackOS-project/dev

A recently ported my OS to x86_64. I added serial COM port logging. The problem is this code works and prints "Hello world" to the terminal
Code:
    serial_putc(COM1, 'H');
    serial_putc(COM1, 'e');
    serial_putc(COM1, 'l');
    serial_putc(COM1, 'l');
    serial_putc(COM1, 'o');
    serial_putc(COM1, ' ');
    serial_putc(COM1, 'w');
    serial_putc(COM1, 'o');
    serial_putc(COM1, 'r');
    serial_putc(COM1, 'l');
    serial_putc(COM1, 'd');

but this doesn't:
Code:
serial_write(COM1, "Hello world!");

it compiles but doesn't print "Hello world!"

The source code for serial_write
Code:
void serial_write(int port, const char* data, size_t size = -1)
{
   if(size < 0)
   {
      size = strlen(data);
   }

   for(size_t i = 0; i < size; i++)
   {
      serial_putc(port, data[i]);
   }
}


Top
 Profile  
 
 Post subject: Re: for loops aren't working
PostPosted: Wed Oct 28, 2020 3:27 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 3991
Location: Chichester, UK
1. Just saying "it doesn't work" isn't very helpful. What happens; what output do you get?

2. Almost certainly the fault lies in your strlen() function, but you don't give us the code for it so who knows.

3. What happens if you do
Code:
serial_write(COM1, "Hello world!", 12);
Does that print correctly?


Top
 Profile  
 
 Post subject: Re: for loops aren't working
PostPosted: Wed Oct 28, 2020 3:31 am 
Offline
User avatar

Joined: Tue Mar 31, 2020 6:28 pm
Posts: 11
iansjack wrote:
1. Just saying "it doesn't work" isn't very helpful. What happens; what output do you get?

2. Almost certainly the fault lies in your strlen() function, but you don't give us the code for it so who knows.

3. What happens if you do
Code:
serial_write(COM1, "Hello world!", 12);
Does that print correctly?


it does not print anything at all even with serial_write(COM1, "Hello world!", 12).


Top
 Profile  
 
 Post subject: Re: for loops aren't working
PostPosted: Wed Oct 28, 2020 4:00 am 
Offline
Member
Member

Joined: Sun Apr 05, 2020 1:01 pm
Posts: 110
size_t is unsigned it cannot be less than 0 :D


Top
 Profile  
 
 Post subject: Re: for loops aren't working
PostPosted: Wed Oct 28, 2020 4:21 am 
Offline
Member
Member
User avatar

Joined: Fri Nov 22, 2019 5:46 am
Posts: 432
Maybe something weird happens at the function call, maybe because of the flags used for the compilation or linking (telling the way how the function call ABI is done). Just a rough guess, because your code looks correct, at least to me.

And is the function in the same object or source-code file as the code calling it?

EDIT: I found the link to your OS and looked at the linker script: You don't link the data!!!

Greetings
Peter

_________________
https://wiki.osdev.org/User:PeterX


Top
 Profile  
 
 Post subject: Re: for loops aren't working
PostPosted: Wed Oct 28, 2020 9:41 am 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 706
I'm getting major deja vu here. Problems with strings are typically caused by
  1. Not loading enough sectors, so the kernel image in memory is incomplete.
  2. Not linking in the .rodata section, so the string is not part of the kernel image.
Try these two hints first.

_________________
Thou hast outraged, not insulted me, sir; but for that I ask thee not to beware of Starbuck; thou wouldst but laugh; but let Ahab beware of Ahab; beware of thyself, old man.


Top
 Profile  
 
 Post subject: Re: for loops aren't working
PostPosted: Wed Oct 28, 2020 10:55 am 
Offline
Member
Member
User avatar

Joined: Fri Oct 27, 2006 9:42 am
Posts: 1662
Location: Athens, GA, USA
This is definitely a FAQ. I actually had to check the date to be sure that this was a new thread and not someone committing thread necromancy.

The Wiki does cover this, and even has a redirect of the phrase 'Strings do not work' which brings up the page on Bran's Tutorial's known bugs. It seems that it isn't the easiest answer for newcomers to find despite this. This may need to be addressed, as it will keep happening otherwise, though I am not certain how we could go about it. Any thoughts?

In this case, the focus on the for() loop rather than the string data seems to have been a stumbling block. XY problems happen, I guess.

_________________
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
μή εἶναι βασιλικήν ἀτραπόν ἐπί γεωμετρίαν
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.


Top
 Profile  
 
 Post subject: Re: for loops aren't working
PostPosted: Wed Oct 28, 2020 1:27 pm 
Online
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 2477
It's pretty interesting that you're able to get this far running 64-bit code in 32-bit mode.


Top
 Profile  
 
 Post subject: Re: for loops aren't working
PostPosted: Wed Oct 28, 2020 1:56 pm 
Offline
Member
Member
User avatar

Joined: Fri Nov 22, 2019 5:46 am
Posts: 432
Schol-R-LEA wrote:
This is definitely a FAQ. I actually had to check the date to be sure that this was a new thread and not someone committing thread necromancy.

The Wiki does cover this, and even has a redirect of the phrase 'Strings do not work' which brings up the page on Bran's Tutorial's known bugs. It seems that it isn't the easiest answer for newcomers to find despite this. This may need to be addressed, as it will keep happening otherwise, though I am not certain how we could go about it. Any thoughts?

In this case, the focus on the for() loop rather than the string data seems to have been a stumbling block. XY problems happen, I guess.

1.) Sorry that I didn't point the asking person to the FAQ. In the future I'll try to (mentally and reading) check first if a question can be answered by the FAQ or maybe the Wiki in general or old forum threads.

2.) And yes, it was an XY problem, a linking problem "in disguise" as a loop problem.

3.) Someone already made a redirection for the string-page to the page with the tutorial-error-page. That looks good.

4.) I think the FAQ should be linked in big, fat letters from the forum's top (like the Wiki in general is done). And I don't mean the forum FAQ but the Wiki FAQ!

Greetings
Peter

_________________
https://wiki.osdev.org/User:PeterX


Top
 Profile  
 
 Post subject: Re: for loops aren't working
PostPosted: Fri Oct 30, 2020 2:05 pm 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 589
PeterX wrote:
EDIT: I found the link to your OS and looked at the linker script: You don't link the data!!!
Unless the linker script explicitly DISCARDs the sections or he uses some tool like objcopy to remove sections then the `.rodata` section will be emitted but not necessarily in a place they might expect. In this case though the data is in the executable. The problem is as Octocontrabass points out - they are running 64-bit code in 32-bit protected mode and the instructions being run aren't producing the desired results for obvious reasons. Most people often see this issue when displaying to the console (often characters get displayed but not correctly) but this person is seeing it as a result of a function that displays a string to the serial port.

catOS needs to put the processor in 64-bit long mode for this code to work.


Top
 Profile  
 
 Post subject: Re: for loops aren't working
PostPosted: Fri Oct 30, 2020 3:08 pm 
Offline
Member
Member
User avatar

Joined: Fri Nov 22, 2019 5:46 am
Posts: 432
MichaelPetch wrote:
Unless the linker script explicitly DISCARDs the sections or he uses some tool like objcopy to remove sections then the `.rodata` section will be emitted but not necessarily in a place they might expect. In this case though the data is in the executable..

Thanks for the correction. Didn't know that.

Greetings
Peter

_________________
https://wiki.osdev.org/User:PeterX


Top
 Profile  
 
 Post subject: Re: for loops aren't working
PostPosted: Fri Oct 30, 2020 9:33 pm 
Offline
User avatar

Joined: Tue Mar 31, 2020 6:28 pm
Posts: 11
MichaelPetch wrote:
catOS needs to put the processor in 64-bit long mode for this code to work.


how do I setup long mode?


Top
 Profile  
 
 Post subject: Re: for loops aren't working
PostPosted: Fri Oct 30, 2020 10:10 pm 
Online
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 2477
There is a decent overview for setting up long mode on the wiki. (Don't forget to check the links at the bottom of the page.)

You should also check the Intel and AMD architecture manuals for details.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 11 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