Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

some questions about storing our code in array main

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
KeepMoving
Posts: 7
Joined: 2016-08-10 10:39

some questions about storing our code in array main

#1 Post by KeepMoving »

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

arochester
Emeritus
Emeritus
Posts: 2435
Joined: 2010-12-07 19:55
Has thanked: 14 times
Been thanked: 54 times

Re: some questions about storing our code in array main

#2 Post by arochester »

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

KeepMoving
Posts: 7
Joined: 2016-08-10 10:39

Re: some questions about storing our code in array main

#3 Post by KeepMoving »

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.

tomazzi
Posts: 730
Joined: 2013-08-02 21:33

Re: some questions about storing our code in array main

#4 Post by tomazzi »

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

KeepMoving
Posts: 7
Joined: 2016-08-10 10:39

Re: some questions about storing our code in array main

#5 Post by KeepMoving »

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.

Post Reply