-1

I am developing a operating system, following this tutorial, and I'm on part 7 (chapter 7), and he shows how to print a character to the screen, but I want to print multiple characters but it just overwrites the previous character. Here is my code

extern "C" void main() {
    // printf("Hello, World!");
    *(char *)0xb8000 = 'H';
    *(char *)0xb8000 = 'e';
    *(char *)0xb8000 = 'l';
    *(char *)0xb8000 = 'l';
    *(char *)0xb8000 = 'o';
    *(char *)0xb8000 = ',';
    *(char *)0xb8000 = ' ';
    *(char *)0xb8000 = 'W';
    *(char *)0xb8000 = 'o';
    *(char *)0xb8000 = 'r';
    *(char *)0xb8000 = 'l';
    *(char *)0xb8000 = 'd';
    *(char *)0xb8000 = '!';
    return;
}
  • 1
    Try writing successive characters to successive memory locations. – Pete Becker Dec 12 '21 at 21:16
  • 1
    All the addresses are the same.. hmm. (Also, there is no assembly here, nor does C++ define assembly.) – user2864740 Dec 12 '21 at 21:16
  • Will incrementing the address every time work? I think that might cause memory corruption and possibly overflows. – A.K _ThePortal Dec 12 '21 at 21:17
  • Also, @user2864740, the OS I am working with IS made in assembly – A.K _ThePortal Dec 12 '21 at 21:17
  • “..memory corruption..”, possibly. However the current *replace the same character in memory* clearly is **not** a usable solution for the task. – user2864740 Dec 12 '21 at 21:18
  • (The OS being written in assembly is irrelevant to C++.) – user2864740 Dec 12 '21 at 21:18
  • And here you’d want to look at *video memory* (which is how even a single character is displayed). https://stackoverflow.com/questions/32972051/in-c-how-do-i-write-to-a-particular-memory-location-e-g-video-memory-b800-in – user2864740 Dec 12 '21 at 21:21
  • What exactly is _video memory_? – A.K _ThePortal Dec 12 '21 at 21:22
  • Gameshow host: Sorry, we were looking for “What is 0xb8000?” (The answer is “video memory”, however that has been arrange to “display a character”. The should be explained in the resource showing usage of such.) – user2864740 Dec 12 '21 at 21:23
  • Also I'm using protected mode since real mode C++ is a pain – A.K _ThePortal Dec 12 '21 at 21:25
  • Various resources explaining some usages of said memory region (depends on hardware, etc.) https://stackoverflow.com/questions/33681795/how-to-write-to-screen-with-video-memory-address-0xb8000-from-real-mode , https://wiki.osdev.org/Printing_To_Screen , http://www.osdever.net/tutorials/view/the-world-of-protected-mode – user2864740 Dec 12 '21 at 21:27
  • I'm trying it, OK? – A.K _ThePortal Dec 12 '21 at 21:27
  • that address is valid only in plain mode with IBM PC/XT compatible hardware.. I have no idea what even that is working with, looks like primitive version of DOS.. OpenDOS? – Swift - Friday Pie Dec 12 '21 at 21:28
  • I'm working with QEMU – A.K _ThePortal Dec 12 '21 at 21:29
  • @user2864740 Outputs [this garbage](https://qardruss.api-minecraft.net/sq35XyF8) – A.K _ThePortal Dec 12 '21 at 21:30
  • That’s what you told it to output. Hint: every other byte is being used for color information. Now please go **read** some resources on how to access *this particular* memory layout correctly. Reiteration of a provided a link: https://wiki.osdev.org/Printing_To_Screen to the format it appears to be using.. – user2864740 Dec 12 '21 at 21:33
  • Oh I tried the OSDev link, @user2864740, and their print function worked! – A.K _ThePortal Dec 12 '21 at 21:34
  • 3
    what architecture? as I said trying t write into 0xb8000 is IBM PC/XT thing (essentially the very first x86 PC that ever existed, 42 years ago). No color, single byte per character representation of screen. The correct mode should be set beforehand (as you're not setting it in your program). – Swift - Friday Pie Dec 12 '21 at 21:34

2 Answers2

1

Is it possible to write multiple characters in C++ Assembly with no stdlib?

There is no standard way to write one or more characters in C++ other than using the standard library.

The system (operating system / CPU architecture) may have ways, but that depends on which OS / CPU you are using. See their documentation.

There is no one assembly language; each CPU architecture has their own language, and compilers have their own syntax.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

@user2864740 sent me a link that helped me fix it, as it turns out, every 2nd character written to 0xB8000 is a color.
So if you wanted to print Hello!, I'm pretty sure you would do

*(char*)0xB8000 = "H";
*(char*)0xB8001 = 15;
*(char*)0xB8002 = "e";
*(char*)0xB8003 = 15;
*(char*)0xB8004 = "l";
*(char*)0xB8005 = 15;
*(char*)0xB8006 = "l";
*(char*)0xB8007 = 15;
*(char*)0xB8008 = "o";
*(char*)0xB8008 = 15;
*(char*)0xB8009 = "!";
*(char*)0xB800a = 15;

(untested)

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 13 '21 at 01:41