简体   繁体   中英

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?

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.

I work primarily in C++ Builder, But I've also programmed assembly and nasm for production code (mainly on micros). My recommendation is not to go near C++ builder for anything you don't have to. Learning can be an art and the way you go about learning has a lot to do with the outcome. C++ Builder integration with assembly is a detour through South Compton. Even though it may not be the way you normally work, it will pay off big time to learn nasm in pure command line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM