depwl9992 depwl9992 - 1 month ago 13
C++ Question

Inline Assembly with Borland/Embarcadero

A quick question here:

I'm trying to experiment with inline assembly using Embarcadero C++ Builder XE3 on 32-bit Windows 7, specifically to start re-teaching myself assembly with the possibility of future pragmatic application.

So far I've been able to write a set of instructions within a simple console project which compiles fine using all C++ Builder syntax at my disposal:

int _tmain(int argc, _TCHAR* argv[]) {
_asm{
mov dl, 0x24
mov ah, 2 // Print character
int 0x21 // Dos Interrupt - Run operation in AH

mov dl, 0x25
mov ah, 2 // Print character
int 0x21 // Dos Interrupt - Run operation in AH

mov dl, 0x26
mov ah, 2 // Print character
int 0x21 // Dos Interrupt - Run operation in AH

mov dl, 0x27
mov ah, 2 // Print character
int 0x21 // Dos Interrupt - Run operation in AH

int 0x20 // terminate COM
}

return 0;
}


This compiles ok, and these instructions work straight in NASM and the Windows native DEBUG environment. What I expect is a simple write to the console:
$%&'
followed by a successful termination.

However, when I try to run
Project1.exe
via the command line I immediately get a standard Win7 runtime error popup which disappears after a few seconds and terminates the program.

Running with debugging, I seem to be successfully reaching the first
int 0x21
before it hangs, at which point Borland gives me the message:


Project Project1.exe raised exception class $C00000005 with message 'access violation at 0x00401213: read of address 0xffffffff'.


My theory is that I'm not properly initializing the segment with a 256-byte offset (NASM would use something like
org 0x100
at the beginning of the instruction set). But I'm not entirely sure that's the issue.

Any ideas?

Answer

Those int 0x21 instructions are for MS-DOS and not Windows. They mean something else in a Windows program, with the result that you go up in flames.

Comments