PDA

View Full Version : whats the command in C++...


Bmxpunk86pl
01-30-02, 08:50 PM
I was wondering if there is a statement in C++ that would let u open a cd rom drive?

_Will_
01-31-02, 12:08 AM
http://he.net/~marcj/cdrom.html

Hope that helps.

Vovan
02-08-02, 06:03 AM
phui!!!!!
It is Microstop puzzle-ruzzle library code reference!
A friend of mine will use inline assembler, and not this
"
initializeDoorLock(&dl, lockState);
initializeIoctlOutputHeader(&i, (void far*)&dl,
sizeof(struct DoorLock), subunit);
callDevice(drive, (void far*)&i);
status = getStatus(&i.base);
"
or this
"
union REGS r;
struct SREGS s;

r.x.ax = 0x1503;
r.x.bx = FP_OFF(buffer);
r.x.cx = drive;
s.es = FP_SEG(buffer);
int86x(0x2F, &r, &r, &s);
".

Buy some PC-archetikture books where are all CDROM BIOS functions can be found and learn assembler, if you don´t want a headache!:D

Bmxpunk86pl
02-08-02, 01:40 PM
isnt C++ a assembly lanugage?

XWRed1
02-08-02, 06:42 PM
Buy some PC-archetikture books where are all CDROM BIOS functions can be found and learn assembler, if you don´t want a headache!

Ugh... talking to the bios to eject the cd? That sounds dirty and messy. Probably better to go through the OS, thats what its there for.


isnt C++ a assembly lanugage?

No.

Bmxpunk86pl
02-08-02, 06:49 PM
whats an assembly language? whats an example of one (the name, like perl, C++)?

XWRed1
02-08-02, 07:40 PM
i haven't heard anyone call assembly anything other than assembly.

As far as I know, assembly is the same type of thing, except that it has potentially wildly different dialects for each cpu architecture.

I.e., x86 assembly is pretty much the same on everything from a 286 to a Pentium4, excepting the extra instructions like SSE and whatnot.

But that dialect of assembly is different than what is used on PowerPCs, or Sparcs, or Mips, or Arms, etc.

At any rate, you can be sure that assembly is alot more low level than C/C++ are.

Bmxpunk86pl
02-08-02, 07:56 PM
ok so i could combine assembly with C++?

Where are some good guides on the net that teach assembly?

Bmxpunk86pl
02-09-02, 12:09 PM
bump

Visidex
02-09-02, 01:08 PM
you can inline assembler in C/C++ code. Here's (http://courses.ece.uiuc.edu/ece291/books/artofasm/artofasm.html) an online tut that happened to be in my bookmarks.

XWRed1
02-09-02, 02:48 PM
The methods of doing inline asm vary from compiler to compiler, don't they?

I.e., gcc has a diff syntax than vc++ which has a diff syntax then codewarrior, etc.

ButcherUK
02-10-02, 01:51 AM
int86 functions don't work in any modern OS so you want to go through the OS.
Code for windows:

HANDLE hCDDrive = CreateFile("\\\\.\\G:", GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hCDDrive != INVALID_HANDLE_VALUE)
{
DWORD BytesReturned;
if (Opening)
DeviceIoControl(hCDDrive, IOCTL_DISK_EJECT_MEDIA, 0, 0, 0, 0, &BytesReturned, 0);
else
DeviceIoControl(hCDDrive, IOCTL_DISK_LOAD_MEDIA, 0, 0, 0, 0, &BytesReturned, 0);
CloseHandle(hCDDrive);
}

Vovan
02-12-02, 09:16 AM
Assembly is a Standart, mostly known on Intel platforms, that allows low archetecture calls. In fact, nothing regards you from writing your own OS with asm.

------------
int86 functions don't work in any modern OS
-------------

In fact THEY MUST WORK, while every OS MUST HAVE A PROCCESSOR (CPU).
They can be not supported through the compiler manufacturer.

--------------
The methods of doing inline asm vary from compiler to compiler, don't they?
----------------

No they MUST NOT. Every respective compiler-builder must follow (when we talking about C or ASM) ANSI C convention. I now for sure that (_asm {} ), and (asm{} ) are reserved ANSI standart keywords, they are not functions and are replaced during the compilation with the assembler-generated code.


P.S:
I´ve bought a book at net, now waiting. As soon as it comes I will help U!:beer:

XWRed1
02-12-02, 11:33 AM
Assembly is a Standart, mostly known on Intel platforms, that allows low archetecture calls. In fact, nothing regards you from writing your own OS with asm.

So are you saying that if I were to write some tight and quick asm code for x86, I could take it over to something like, say, PowerPC or MIPS and it'd work fine, with no changes?

------------
int86 functions don't work in any modern OS
-------------
In fact THEY MUST WORK, while every OS MUST HAVE A PROCCESSOR (CPU).
They can be not supported through the compiler manufacturer.

I think he means that modern OSes won't let unprivileged userspace programs do int86 functions. Not to mention we might not even be talking about something running on x86.



--------------
The methods of doing inline asm vary from compiler to compiler, don't they?
----------------
No they MUST NOT. Every respective compiler-builder must follow (when we talking about C or ASM) ANSI C convention. I now for sure that (_asm {} ), and (asm{} ) are reserved ANSI standart keywords, they are not functions and are replaced during the compilation with the assembler-generated code.

Well, thats nice to know, even though it seems some of the older compilers don't follow ansi.

Here's an example of asm in the Linux kernel:

kdba_setjmp(kdb_jmp_buf *jb)
{
#if defined(CONFIG_FRAME_POINTER)
__asm__ ("movl 8(%esp), %eax\n\t"
"movl %ebx, 0(%eax)\n\t"
"movl %esi, 4(%eax)\n\t"
"movl %edi, 8(%eax)\n\t"
"movl (%esp), %ecx\n\t"
"movl %ecx, 12(%eax)\n\t"
"leal 8(%esp), %ecx\n\t"
"movl %ecx, 16(%eax)\n\t"
"movl 4(%esp), %ecx\n\t"
"movl %ecx, 20(%eax)\n\t");
#else /* CONFIG_FRAME_POINTER */
__asm__ ("movl 4(%esp), %eax\n\t"
"movl %ebx, 0(%eax)\n\t"
"movl %esi, 4(%eax)\n\t"
"movl %edi, 8(%eax)\n\t"
"movl %ebp, 12(%eax)\n\t"
"leal 4(%esp), %ecx\n\t"
"movl %ecx, 16(%eax)\n\t"
"movl 0(%esp), %ecx\n\t"
"movl %ecx, 20(%eax)\n\t");
#endif /* CONFIG_FRAME_POINTER */
KDB_STATE_SET(LONGJMP);
return 0;
}


Looks like its using __asm__()

Bmxpunk86pl
02-12-02, 01:38 PM
ok thanks i have one more question. Can i use assebly coding in C++? for example say if i wanted to write a program in C++ that would do something with MMX.

XWRed1
02-12-02, 04:05 PM
Yes, thats what we have been discussing.

If you were doing it in gcc (and I guess this sticks to other compilers too), you'd just put your asm code in the __asm__(), as shown above.

Barium56
02-12-02, 11:31 PM
Originally posted by ButcherUK


HANDLE hCDDrive = CreateFile("\\\\.\\G:", GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hCDDrive != INVALID_HANDLE_VALUE)
{
DWORD BytesReturned;
if (Opening)
DeviceIoControl(hCDDrive, IOCTL_DISK_EJECT_MEDIA, 0, 0, 0, 0, &BytesReturned, 0);
else
DeviceIoControl(hCDDrive, IOCTL_DISK_LOAD_MEDIA, 0, 0, 0, 0, &BytesReturned, 0);
CloseHandle(hCDDrive);
}


What header files do you need for that? My compiler can't find IOCTL_DISK_EJECT_MEDIA or IOCTL_DISK_LOAD_MEDIA....

Bmxpunk86pl
02-13-02, 07:00 PM
Ok thanks a lot everyone for ur help.

Vovan
02-14-02, 09:05 AM
Originally posted by XWRed1


So are you saying that if I were to write some tight and quick asm code for x86, I could take it over to something like, say, PowerPC or MIPS and it'd work fine, with no changes?


I don´t say so. To do this you should look in the CPU command reference and find out the synonyme. I mean, with replacing the unsupported code. Compiler plays here no role. You can write comands direct to data and then execute this data.


Well, thats nice to know, even though it seems some of the older compilers don't follow ansi.

Here's an example of asm in the Linux kernel:

kdba_setjmp(kdb_jmp_buf *jb)
{
#if defined(CONFIG_FRAME_POINTER)
__asm__ ("movl 8(%esp), %eax\n\t"
"movl %ebx, 0(%eax)\n\t"
"movl %esi, 4(%eax)\n\t"
"movl %edi, 8(%eax)\n\t"
"movl (%esp), %ecx\n\t"
"movl %ecx, 12(%eax)\n\t"
"leal 8(%esp), %ecx\n\t"
"movl %ecx, 16(%eax)\n\t"
"movl 4(%esp), %ecx\n\t"
"movl %ecx, 20(%eax)\n\t");
#else /* CONFIG_FRAME_POINTER */
__asm__ ("movl 4(%esp), %eax\n\t"
"movl %ebx, 0(%eax)\n\t"
"movl %esi, 4(%eax)\n\t"
"movl %edi, 8(%eax)\n\t"
"movl %ebp, 12(%eax)\n\t"
"leal 4(%esp), %ecx\n\t"
"movl %ecx, 16(%eax)\n\t"
"movl 0(%esp), %ecx\n\t"
"movl %ecx, 20(%eax)\n\t");
#endif /* CONFIG_FRAME_POINTER */
KDB_STATE_SET(LONGJMP);
return 0;
}


Looks like its using __asm__() [/B]

Thankx for info, but linux is a P-Code OS. If you Look carefully, you´ll notice that asm code is passed like a parameter to something (i think it is a programm), that !dynamically interpretates! (@run-time) this code. This data is stored in the ready executable not as ´BE 04 A4 AF 09 ´ and so on , but like ´"movl 4(%esp), %eax\n\t movl %ebx, 0(%eax)\n\t"´.
It is not a asm keyword, it is a ´data-sending´ funktion.
Such a reference mean that as Windows uses Visual BASIC, Linux in that funktion uses Visual Assembler (scripting).


This means that anybody can dig in your code , modify it , cancel, stop, abort and do other things!!! This is good for antivirus, but not good for CD writing prog!!!

-----------------------------------------------------------------------
I think he means that modern OSes won't let unprivileged userspace programs do int86 functions. Not to mention we might not even be talking about something running on x86.
------------------------------------------------------------------------

There are a lot of commands that they support! Especially outp(ort) and inp(ort), intXX!
Only dumb OSes prevent it!



But thanks for Info!:beer:

XWRed1
02-14-02, 04:53 PM
What the hell, I write a long post, but vbulletin won't take it!! I will come back later and try again.

Visidex
02-14-02, 10:27 PM
-----------------------------------------------------------------------
I think he means that modern OSes won't let unprivileged userspace programs do int86 functions. Not to mention we might not even be talking about something running on x86.
------------------------------------------------------------------------

There are a lot of commands that they support! Especially outp(ort) and inp(ort), intXX!
Only dumb OSes prevent it!

I think what they're trying to say is that OSes don't allow the unprivileged userspace progs direct access to the hardware. But I'm not sure what your saying there,Vovan, and I'm trying to interprete an interpretation.

Vovan
02-18-02, 08:59 AM
Understood!:beer:

Bmxpunk86pl
02-18-02, 03:14 PM
#include <iostream.h>
#include <stdio.h>
int main ()
{
__ASM___ {
begin

i := 10;
write('Enter a value for j:');
readln(j);
i := i*j + j*j;
writeln('The result is ',i);

end.
}
return 0;
}

i get a lot of errors when i do that. I know its not gonna do anything but it should at least compile right? Am i missing something in the #include files?

XWRed1
02-18-02, 07:14 PM
I think the problem is that you aren't putting valid assembler into the __asm{} block, but I'm not an expert.

Vovan
02-19-02, 09:10 AM
Originally posted by Bmxpunk86pl
#include <iostream.h>
#include <stdio.h>
int main ()
{
__ASM___ {
begin

i := 10;
write('Enter a value for j:');
readln(j);
i := i*j + j*j;
writeln('The result is ',i);

end.
}
return 0;
}

i get a lot of errors when i do that. I know its not gonna do anything but it should at least compile right? Am i missing something in the #include files?

!!!!!!!!!!!!!!!!
It is NOT PASCAL!!!

the right version should be so:
#include <iostream.h>
#include <stdio.h>
void main()
{
unsigned short j=0; // 0-255
unsigned short i=10; // 0-255
cout<<"Please enter the value";
cin>>j;
cout<<endl;

_asm {
mov AX,i
mul AX,AX,j ;i*j - mov AA,AA,AA - because it is 80186 instruction.
mov CX,AX ; result in the pocket

mov AX,j
mul AX,AX,2 ; j*j with optimization
add AX,CX ; (i*j)+(j*j)
mov i,AX ; i=...
} ;

cout<<i;

}

icculus
02-22-02, 04:24 PM
I actually created a login so I could respond to this.

Originally posted by Vovan
[B]
I don´t say so. To do this you should look in the CPU command reference and find out the synonyme. I mean, with replacing the unsupported code. Compiler plays here no role. You can write comands direct to data and then execute this data.


Xwred1 was referring to the use of inline assembler, which has a different syntax for almost every compiler, even when writing for the same processor.


Thankx for info, but linux is a P-Code OS. If you Look carefully, you´ll notice that asm code is passed like a parameter to something (i think it is a programm), that !dynamically interpretates! (@run-time) this code. This data is stored in the ready executable not as ´BE 04 A4 AF 09 ´ and so on , but like ´"movl 4(%esp), %eax\n\t movl %ebx, 0(%eax)\n\t"´.
It is not a asm keyword, it is a ´data-sending´ funktion.


Uhh...no. Assembly under Linux (using NASM or GAS or whatever to compile it) assembles down to machine code.

GCC's inline syntax requires you to put your assembly code into a string, which it then passes unmolested to the assembler. It's not an ideal solution, but don't let the presence of string literals throw you off.


Such a reference mean that as Windows uses Visual BASIC, Linux in that funktion uses Visual Assembler (scripting).


No, and that's not what P-Code is, either. P-Code is when something compiles down to a bytecode that isn't mean to be run on the processor without interpretation. You are suggesting that the assembly instructions are parsed at runtime as strings, like Perl would be.

Earlier versions of Visual Basic compiled to P-code, newer ones don't (but maybe still have the option to do so?).


This means that anybody can dig in your code , modify it , cancel, stop, abort and do other things!!! This is good for antivirus, but not good for CD writing prog!!!


If you are referring to open source, the philosophy suggests that letting people "dig" is actually constructive and proactive.

But the interpretation line of thought is incorrect.


There are a lot of commands that they support! Especially outp(ort) and inp(ort), intXX!
Only dumb OSes prevent it!


Modern operating systems block most of the IO ports without superuser access, and all of the software interrupts. Using the BIOS is not just non-portable, it's possible it won't work at all.


--ryan.

vogon_jeltz
02-22-02, 04:33 PM
Originally posted by icculus
Earlier versions of Visual Basic compiled to P-code, newer ones don't (but maybe still have the option to do so?).

Hmm... last time I checked (using VB 6.0 about 2 years ago) I couldn't find an option to do that. Not sure, tho.

Originally posted by Vovan
Thankx for info, but linux is a P-Code OS. If you Look carefully, you´ll notice that asm code is passed like a parameter to something (i think it is a programm), that !dynamically interpretates! (@run-time) this code. This data is stored in the ready executable not as ´BE 04 A4 AF 09 ´ and so on , but like ´"movl 4(%esp), %eax\n\t movl %ebx, 0(%eax)\n\t"´.
It is not a asm keyword, it is a ´data-sending´ funktion.
Such a reference mean that as Windows uses Visual BASIC, Linux in that funktion uses Visual Assembler (scripting).

If this is true, and if the kernel uses inline assembly, then how does the kernel work? If the kernel has to invoke an interpreter to run assembly routines, then it must first have initialized the bits of it that enable it to run the interpreter (HD access, etc.) Since some of this inline assembly (correct me if I'm wrong about this) is in the really low-level stuff, it can never be run.

Either my computer is pulling a very elaborate ruse on me right now consisting of running P-Code under an interpreter that can't logically be run, or you're an idiot.

Anyone care to invoke Occam's Razor for me?

-- Colin

ReaperSMS
02-22-02, 05:12 PM
Originally posted by Vovan

I don´t say so. To do this you should look in the CPU command reference
and find out the synonyme. I mean, with replacing the unsupported code.
Compiler plays here no role. You can write comands direct to data and
then execute this data.


What the previous person was referring to here is that MOV is not called MOV on all CPU's,
and registers are not the same across chips. If you were on a 6502, MOV would be called LDA,
and you'd only have 3 registers you can access directly...


Thankx for info, but linux is a P-Code OS. If you Look carefully,


I can't believe I just read that. You obviously have less clue about what goes on
in a compiler and an OS than even a n00b C coder. Please don't spread disinformation
like this, it prevents those that have a chance at learning something from doing so.

P-Code is an ancient invention of N. Wirth, generally used for some dialects of Pascal
and other bytecoded languages. It does not do full text parsing.


you´ll notice that asm code is passed like a parameter to something
(i think it is a programm), that !dynamically interpretates! (@run-time)
this code. This data is stored in the ready executable not as
´BE 04 A4 AF 09 ´ and so on , but like ´"movl 4(%esp), %eax\n\t
movl %ebx, 0(%eax)\n\t"´.


This is flat out *wrong*. asm, __asm, and __asm__ are not Standard C, either ANSI, K&R,
C99, Stroustrup, or ANSI C++. The are a compiler specific extension, which just happens
to (generally) follow the form

asm {
assembly code
}

It is quite different if you are using anything other than VC++ or a Borland C compiler.
Watcom for instance uses #pragma aux for it, but I dont' remember the exact syntax.

GCC uses the asm ("code" : outputs : inputs : clobbers); syntax. It does not place that
line of text into the executable, it pastes that verbatim into the assembly file the
compiler feeds to the assembler.

The code will be stored in the executeable as BE 04 A4 AF 09 (assuming you translated that into
machine code correctly, which you probably didn't judging from the rest of your post).


It is not a asm keyword, it is a ´data-sending´ funktion.
Such a reference mean that as Windows uses Visual BASIC, Linux
in that funktion uses Visual Assembler (scripting).


Visual Anything is a Windows only deal. There is no Visual Assembler. VBasic tends to produce native
code now anyways...


This means that anybody can dig in your code , modify it , cancel,
stop, abort and do other things!!! This is good for antivirus, but
not good for CD writing prog!!!


Aside from the fact that you are completely wrong, there's nothing stopping someone from digging through the machine
code output, changing a few bytes, and modifying your code to do anything else that happens to enter their minds.


-----------------------------------------------------------------------
I think he means that modern OSes won't let unprivileged userspace
programs do int86 functions. Not to mention we might not even be
talking about something running on x86.
------------------------------------------------------------------------

There are a lot of commands that they support! Especially outp(ort) and inp(ort), intXX!
Only dumb OSes prevent it!


Quite a number of CPU's do not have IN or OUT. Everything on those systems is memory mapped.

All modern operating systems (98 is not modern. Linux, NT, and 2k are. All the old unices are.)
ban direct access to most I/O ports for regular programs, as otherwise any program could take the
machine down rather quickly.

The only reason DOS and 98 let you is that they were built before PC's were used for anything important,
or before the x86 series had the capability to host a properly stable OS. NT was written to be a proper, stable
OS that takes advantage of features that have been on the chip since the 286.

Find a halfway decent book on machine architecture, a couple of CPU manuals for chips other than the x86,
and docs for a system that doesn't run windows, and don't come back until you know them cold. The only thing
more dangerous than a clueless person is a clueless person that *thinks* they know how to use ASM and C++ well.


But thanks for Info!:beer:

icculus
02-22-02, 05:34 PM
One more thing worth mentioning.

What's the best way to learn assembler?

The answer is, "Don't."

At least, not yet. Get your head wrapped around C (or C++, if you must), and come back to assembler later. ASM is inappropriate for almost all the places where you think it would make a good solution.

Very rarely should it be used in an application (on occasion in a game for the speed boost, but even there you can do without, although I'm sure Michael Abrash would disagree). It gets used with more frequency for Ring 0 programming (kernel coding) and embedded stuff, where you need to touch hardware directly, but any modern Unix (any modern anything?) will show that the ratio of C to assembly favors higher level languages here, too.

The Win32 API, or POSIX, or whatever system calls your OS has are there for a GOOD reason, and it's not less l33t to use them.

But learn to crawl before you run. Get your C coding down and figure out where to go from there.

--ryan.

Bmxpunk86pl
02-22-02, 08:37 PM
alright, i guess killing two birds with one stone will not work here. LOL

ButcherUK
03-07-02, 11:05 AM
Originally posted by Barium56


What header files do you need for that? My compiler can't find IOCTL_DISK_EJECT_MEDIA or IOCTL_DISK_LOAD_MEDIA....

Bit late but winioctl.h.

And yes i did mean the io ports in userspace, nearly all OSes require you to be in ring 0 (kernel mode) to do hardware access.