0
//a pointer to the first block of screen
int far *SCR = (int far *) 0xB8000000; 

(SCR +  6 * 80 + 31) += "HELLO HELLO HELLO";

In the code above, I defined SCR which is a pointer to the first block of the screen in text mode. It works correctly and I can write on the screen in text mode as below:

#define WHITE 3840
*(SCR +  6 * 80 + 31) = 'h' + WHITE;
*(SCR +  6 * 80 + 32) = 'e' + WHITE;
*(SCR +  6 * 80 + 33) = 'l' + WHITE;
*(SCR +  6 * 80 + 34) = 'l' + WHITE;
*(SCR +  6 * 80 + 35) = 'o' + WHITE;

but when I want to write something long in the screen and when I attempt to write as below I see an error of Lvalue required from the last line in the code written below:

#define WHITE 3840
for(int i = 31; i < 36; i++)
   *(SCR + 6 * 80 + i) = WHITE;
(SCR + 6 * 80 + 31) += "hello";

Why I cannot have access to the pointer such as above and how I can fix my problem in writing long words and sentences with the pointer SCR I defined?

4
  • 1
    += does not automatically loop over the elements of an array. Commented Dec 6, 2013 at 9:49
  • @Barmar let me see if it can be fixed without the +=. but then how I can add pen color to that if I am supposed not to use +=? Commented Dec 6, 2013 at 9:53
  • @Barmar no it still errors as Lvalue required Commented Dec 6, 2013 at 9:55
  • OT: As writing chars I'd declare SCR this way: char far * SCR = (char far *) 0xB8000000; Commented Dec 6, 2013 at 10:21

2 Answers 2

2

You can't do something like:

(SCR + 6 * 80 + 31) += "hello";

because "hello" is a pointer that points to an array of char that's been initialized with the characters 'h', 'e', 'l', etc. And you can't add pointers to pointers (nor would it make sense for your problem even if you were allowed to).

If you want to output a string to a location pointed to by SCR plus some offset, write a function to make it less painful. That's what functions are for:

void write_to_screen( int row, int col, char const* s, int attr)
{
    int offs = row * 80 + col;

    for (;*s != 0; ++s) {
        SCR[offs++] = attr | *s;
    }
}

// call it like so:
write_to_screen( 6, 30, "hello", WHITE);

or something like that (you may need to adjust the details of where & how attributes and characters get written, since I don't recall the details of writing to the screen buffer in real mode).

Sign up to request clarification or add additional context in comments.

3 Comments

if we initialize SCR[offs++] with attr and then with *s the so the attr value will be lost. cannot we use `+=' at any case?
what is this SCR[offs++] = attr | *s;? I don't understand | between them? would you explain?
@MelikaBarzegaranHosseini: it is a bitwise or operator. The value of WHITE is 0x0f00 in hex, if s is pointing to the letter 'h' (which has a hex value of 0x68) the result of attr | *s would be 0x0f68.
0
#define WHITE 3840
char *hello = "hello";
for (int i = 0; hello[i]; i++) {
    *(SCR + 6 * 80 + 31 + i) = WHITE + hello[i];
}

3 Comments

why for (int i = 0; hello[i]; i++) ? I don't understand the second part of the for loop.
and I have lots of strings to write. Isn't there a shorter way so that I wouldn't be made to write one for loop for each string?
Put it in a function, and pass the string as an argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.