assemblyflags

Assembly error handling


I created a function for reading in decimal numbers in assembly, and I want to do something like throwing an exception if a non-numeric character is entered. My idea was to set some flag(s) accordingly, and handle these in main. Which flags are best for this situation, and how could I set this/these?


Solution

  • On the x86 architecture, the carry flag (CF) has been historically used for such purposes, probably because it is the most convenient to manipulate: the STC and CLC instructions can be used to set and clear it. The interrupt and direction flags (IF, DF) have similar instructions, but altering these flags tends has a significant impact on other code; the direction flag, in particular, is normally required by various calling conventions to be set one way or another, and the interrupt flag masks interrupts, a visible effect that should not depend on failure to parse an integer (for example).

    While I wouldn't compare it to proper exception throwing and handling, setting the carry flag before return can indicate an error, and the caller can check the carry flag (using the jc instruction) on return to handle the error appropriately.