The MASM Forum

Miscellaneous => 16 bit DOS Programming => Topic started by: Gunther on April 04, 2015, 11:39:04 AM

Title: AVX and AVX2 for DOS applications
Post by: Gunther on April 04, 2015, 11:39:04 AM
Well, okay it follows a bit DOS system programming. So I'll explain a few things here in detail. We had in the past some interesting discussions about using the new AVX and AVX2 instructions under Win-32, for example here (http://masm32.com/board/index.php?topic=3191.0) and here (http://masm32.com/board/index.php?topic=3227.0). The intermediate result was, that an application can use AVX, if an appropriate CPU is available (Sandy Bridge (https://en.wikipedia.org/wiki/Sandy_Bridge), Ivy Bridge, Haswell (https://en.wikipedia.org/wiki/Haswell_%28microarchitecture%29), Bulldozer (https://en.wikipedia.org/wiki/Bulldozer_%28microarchitecture%29), Piledriver, Jaguar, Steamroller (https://en.wikipedia.org/wiki/Steamroller_%28microarchitecture%29)) and the Operating System (Windows 7 with SP1 and above) must support the new instruction set. For example, there's no chance to use AVX with Windows XP.

And now DOS? Yes, there is good news: It is possible to use AVX and AVX2 with a DOS application. After a long discussion with some old friends in Geneva (CERN) plus reading the appropriate Intel manuals again and again, here is my solution.

First of all, plain DOS (without fancy Multitaskers like JEMM, EMM386 and the like) is a single tasking Operating System. No task switches and context changes are necessary. That's the chance. There are 4 steps to do and all applications in a current DOS session can use the new instructions.
The tricky part is step 4. The Intel manual says:
Quote
This instruction must be executed at privilege level 0 or in real-address mode; otherwise, a general protection exception #GP(0) is generated.
So, before we can make the described 4 steps above, we have to check 2 things:
The attached archive avx.zip (sources included) contains the program avx.com which checks these 2 things and after that makes the 4 steps, described above. Therefore your CONFIG.SYS file should look similar like this (plain DOS):
Quote
DEVICE=C:\DOS\HIMEM.SYS
DEVICE=C:\HXCD-ROM\CDROM.SYS /D:MSCD000
LASTDRIVE=Z
FILES=40
BUFFERS=10
You can find some hints for preparing a bootable USB stick here (http://masm32.com/board/index.php?topic=3484.0).

I've included the application cpu.exe from this thread (http://masm32.com/board/index.php?topic=3757.0) into the archive for testing purposes. The application avx.com will run under plain DOS and as virtual machine under VMWare. It doesn't work under VirtualBox, because they are emulating only an old CPU (SSE3) for the DOS VM.

You can test it so. Start cpu.exe first. You should see the following screen:
Quote

      Supported Features
      ==================

Vendor String: GenuineIntel
Brand  String: Intel(R)Core(TM)i7-3770CPU@3.40GHz

      Instruction Sets
      ----------------

MMX  SSE  SSE2  SSE3  SSSE3  SSE4.1  SSE4.2

      Supported Special Instructions
      ------------------------------

Conditional Moves
FXSAVE and FXSTOR
XSAVE and XSTOR for processor extended state management.
POPCNT
RDRAND
AES (Advanced Encryption Standard) Instruction Set
16-bit floating-point Conversion Instructions

Please, press enter to end the application ...

Start now avx.com:
Quote

This program checks the usage of the AVX instruction
set for a plain DOS application.

Please press enter to continue ...

AVX is supported by the CPU.
Extended State Management is supported by the CPU.

Program ends now!

After a new start of cpu.exe, you should see the following screen:
Quote

      Supported Features
      ==================

Vendor String: GenuineIntel
Brand  String: Intel(R)Core(TM)i7-3770CPU@3.40GHz

      Instruction Sets
      ----------------

MMX  SSE  SSE2  SSE3  SSSE3  SSE4.1  SSE4.2 AVX

      Supported Special Instructions
      ------------------------------

Conditional Moves
FXSAVE and FXSTOR
XSAVE and XSTOR for processor extended state management.
POPCNT
RDRAND
AES (Advanced Encryption Standard) Instruction Set
16-bit floating-point Conversion Instructions

Please, press enter to end the application ...

Since jWasm for DOS isn't up to date, I've written the sources for YASM (NASM should also do the job).

Some test results under other configurations would be fine.
 
Gunther
Title: Re: AVX and AVX2 for DOS applications
Post by: sinsi on April 04, 2015, 04:41:24 PM
Booting to MS-DOS 7, plain DOS - no config.sys or autoexec.bat, second run of cpu.exe after avx.exe
Code: [Select]
Supported Features
==================

Vendor String: AuthenticAMD
Brand  String: AMDA10-7850KAPUwithRadeon(TM)R7Graphics

Instruction Sets
----------------

MMX  SSE  SSE2  SSE3  SSSE3  SSE4.1  SSE4.2  AVX

Supported Special Instructions
------------------------------

Conditional Moves
FXSAVE and FXSTOR
XSAVE and XSTOR for processor extended state management.
POPCNT
AES (Advanced Encryption Standard) Instruction Set
FMA (Fused Multiply Add) extensions using YMM state.
16-bit floating-point Conversion Instructions

Please, press enter to end the application ...
Title: Re: AVX and AVX2 for DOS applications
Post by: Gunther on April 04, 2015, 10:47:13 PM
Thank you sinsi.  :t A config.sys with HIMEM.SYS will work, too (that's tested). You can start it also inside VMWare (that's tested, too).

Gunther
Title: Re: AVX and AVX2 for DOS applications
Post by: nidud on April 05, 2015, 03:33:30 AM
The CheckWindows functions works in Windows but not in DOSBox or Oracle VM.

Testing the true version for 0005h may indicate DOSBox, but on other VM’s this may be a problem.
Title: Re: AVX and AVX2 for DOS applications
Post by: Gunther on April 05, 2015, 04:04:35 AM
Hi nidud,

The CheckWindows functions works in Windows but not in DOSBox or Oracle VM.

Testing the true version for 0005h may indicate DOSBox, but on other VM’s this may be a problem.

thank you for your hint. But at the moment it's not a problem. DOSBox emulates an 80486; there's no chance to use AVX. VirtualBox (Oracle) emulates only a CPU with SSE3, therefore no chance to use AVX.

Gunther
Title: Re: AVX and AVX2 for DOS applications
Post by: Gunther on April 05, 2015, 09:05:05 AM
Hi nidud,

only some supplements. It is not the question to check, if we're running under Windows. For example, avx.com runs fine as virtual machine under VMware Player with Windows 7-64 as Host and FreeDOS as guest operating system. It seems possible that it'll run in the future under VirtualBox. But at the moment emulates VirtualBox only a CPU with maximal SSE3 support. The same would be true for future versions of DOSBox.

But it would be problematic if the program runs - let's say - under Windows 7-32 inside cmd.exe. That's the point. For that case, CheckWindows makes the job and that's good enough.

Gunther
Title: Re: AVX and AVX2 for DOS applications
Post by: nidud on April 05, 2015, 08:23:44 PM
Yes, the programs works well emulated where the CPU test will handle this issue, so it may not be that important.

I use this test in the DOS version of the file manager for the idle functions, but this only works in Windows.
Title: Re: AVX and AVX2 for DOS applications
Post by: Gunther on April 06, 2015, 09:53:42 AM
Hi nidud,

Yes, the programs works well emulated where the CPU test will handle this issue, so it may not be that important.

I use this test in the DOS version of the file manager for the idle functions, but this only works in Windows.

did you test avx.com under DOs or inside a VM?

Gunther
Title: Re: AVX and AVX2 for DOS applications
Post by: nidud on April 06, 2015, 10:26:11 AM
I tested AVX.COM and CPU.EXE in XP, DOSBox, and VirtualBox.

They work well on all of these, but the CheckWindows() only works in XP. SSE3 was detected in VirtualBox (CPU.EXE) and AVX.COM exited with the "Program runs under V86 Mode and terminates!" message.

No test was done in real DOS in this case.
Title: Re: AVX and AVX2 for DOS applications
Post by: Gunther on April 06, 2015, 10:59:25 AM
I tested AVX.COM and CPU.EXE in XP, DOSBox, and VirtualBox.

They work well on all of these, but the CheckWindows() only works in XP. SSE3 was detected in VirtualBox (CPU.EXE) and AVX.COM exited with the "Program runs under V86 Mode and terminates!" message.

No test was done in real DOS in this case.

By using FreeDOS, avx.com will run under VMware Player (if you have an appropriate CPU, of course) and with plain DOS (only HIMEM.SYS is allowed).

Gunther