Image Map Image Map
Results 1 to 3 of 3

Thread: Borland C ASM Inline Coding Question

  1. #1

    Default Borland C ASM Inline Coding Question

    Hi,

    I was trying to code a put sprite routine using inline assembly to optimize the code, but I'm not sure if I have to restore all the registers you modify inside your routine before you return to C.
    Is it necesary to restore them like you do on an Interrupt Service Routine? or does the C compiler takes care of this for you?
    I was getting a "Divide error" but the routine worked ok, until I pushed and poped the DS register. Then everything worked fine, that's why I'm asking this...

    Thanks!

  2. #2
    Join Date
    Jun 2012
    Location
    UK - Worcester
    Posts
    1,179

    Default

    You are generally responsible for saving and restoring the state of the CPU not the compiler writer. If the compiler writer was to do it, he/she would end up saving and restoring everything - which would be quite an overhead (in terms of CPU cycles).

    If you modify a register - the compiler could already be using this register itself and you would cause corruption. Some CPU registers are vitally important to the operation (e.g. DS, SP, BP, ...) others less so (e.g. AX) as they are used as temporary registers. Unfortunately, it can be the misuse of these registers that cause the unpredictable results.

    Mor 'clever' compilers could look at register usage (at the assembly level or lower) and possibly decide to do some save/restore operations of its own?

    My advice would be to leave the machine state in the condition you found it. Of course, read the manual to see what the compiler vendor says about code inserts (they may have some specific rules to make you life easier).

    Dave

  3. #3
    Join Date
    Dec 2014
    Location
    The Netherlands
    Posts
    1,584

    Default

    Quote Originally Posted by daver2 View Post
    Mor 'clever' compilers could look at register usage (at the assembly level or lower) and possibly decide to do some save/restore operations of its own?
    Yes, it seems to depend a bit on what the compiler writers want.
    OpenWatcom doesn't seem to require you to restore any registers in an inline asm function.
    With Borland I believe you need to restore *some* registers, but not necessarily all.
    The best way to find out is to just write some asm, compile it, disassemble, and look at what the compiler puts around your code.
    Experiment a bit with using different registers (also segment registers), and see how the compiler reacts.

    I'm not sure, but I vaguely recall that Borland required you to preserve the segment registers, but not the regular registers (or at least not some of them, such as ax, bx, cx and dx).

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •