Exception/Error handling in NASM assembly

2019-07-27 18:05发布

问题:

How do I handle errors in NASM assembly? For example I have this code to read user Input:

mov eax,3
mov ebx,0
mov ecx,Buffer
mov edx,BUFFERLENGTH
int 80H

If for some reason this system call cannot be executed, I'd like to have the program jump to a label that prints "An error has occured" or something like that. How do I do that?

Also, is it possible to get the name of the exception or error code?

Thanks

回答1:

After the kernel call, EAX is going to have two possibilites;

  • Number of characters entered.
  • Negated error code.

                int     80H
                or      eax, eax
                jns     OK        ; Tests sign flag
    
                neg     eax       ; Converts error code to positive value
        ;   Error trapping here
    
           OK:  dec     eax       ; Bump by one cause length includes CR
                jnz     Good
        ; Do something special if operator only entered CR
    
         Good:  nop
    

    This is an example how you could evaluate if there is an error and if operator even entered anything.