some questions about storing our code in array main

Need help with C, C++, perl, python, etc?

some questions about storing our code in array main

Postby KeepMoving » 2016-08-10 11:06

Today I met a question:
Add some contents to array a,and you will make the following code displaying a character c in the middle of screen.
The code is :

Code: Select all
char a[200] = {......};
int main ()
{
         ((void (far *) ()) (long)a) ();
}


And I add the following hex numbers into array a,and compile the code above ,there is no error.An exe file created.I run the exe file,it displays an character c in the middle of screen.
these hex number is:

Code: Select all
0xBB,0x00,0xB8,0x8E,0xC3,0xBB,0x90,0x06,0x26,0xC6,0x07,0x63,0xBB,0x00,0xB8,0x8E,0xC3,0xBB,0x91,0x06,0x26,0xC6,0x07,0x02,0xC3


you can think of these hex number as some machine code ,thest machine code is relative to the following code in c:

Code: Select all
*(char far*) (0xb8000000 + 160 * 10 + 80) = 'c';
*(char far*) (0xb8000000 + 160 * 10 + 81) = 2;


The code above lets computer display a character c in the middle of screen in 8086 PC (I know it is too old).

But today someone told me another method to achieve this:

Code: Select all
char main[201] = {0xBB,0x00,0xB8,0x8E,0xC3,0xBB,0x90,0x06,0x26,0xC6,0x07,0x63,0xBB,0x00,0xB8,0x8E,0xC3,0xBB,0x91,0x06,0x26,0xC6,0x07,0x02,0xC3};



You can compile it using tcc.exe,and run the created exe files,it can also show a character c in the middle of screen.
But why this can effect??I don`t know clearly.please give me an answer,thank you. :D
KeepMoving
 
Posts: 7
Joined: 2016-08-10 10:39

Re: some questions about storing our code in array main

Postby arochester » 2016-08-10 11:10

You can compile it using tcc.exe,and run the created exe files

??? Debian Linux???
arochester
 
Posts: 829
Joined: 2010-12-07 19:55

Re: some questions about storing our code in array main

Postby KeepMoving » 2016-08-10 11:23

arochester wrote:
You can compile it using tcc.exe,and run the created exe files

??? Debian Linux???


I installed dosbox on debian linux.The dosbox can simulation a 8086PC environment on debian linux.So I can run tcc.exe on it.
KeepMoving
 
Posts: 7
Joined: 2016-08-10 10:39

Re: some questions about storing our code in array main

Postby tomazzi » 2016-08-10 13:08

KeepMoving wrote:you can think of these hex number as some machine code

Well, this *is* a machine code, namely an array of 8086 opcodes, which are forming a classic function.
This function is just setting the cursor in the middle of the screen (VGA mode) and prints "c".

Besides this simple example, such "tricks" are often used in interpreters and hw emulators.

Regards.
Odi profanum vulgus
tomazzi
 
Posts: 730
Joined: 2013-08-02 21:33

Re: some questions about storing our code in array main

Postby KeepMoving » 2016-08-11 02:00

tomazzi wrote:
KeepMoving wrote:you can think of these hex number as some machine code

Well, this *is* a machine code, namely an array of 8086 opcodes, which are forming a classic function.
This function is just setting the cursor in the middle of the screen (VGA mode) and prints "c".

Besides this simple example, such "tricks" are often used in interpreters and hw emulators.

Regards.



thank you.
KeepMoving
 
Posts: 7
Joined: 2016-08-10 10:39


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests

fashionable