1

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 Answers1

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.

Shift_Left
  • 1,208
  • 8
  • 17
  • Thanks for answering, I have just one more question: i see you used RAX, however I am writing 32 bit assembly. Do I just change it to EAX? – Segmentation fault Sep 27 '16 at 00:12
  • Wait I get it, you negate RAX because it is negative and that way you get the positive value. The possible reason for this is that, since eax is either the number of characters entered or the error code, then it has to be negated as not to be confused with the number of characters entered. Therefore, you need to negate eax/rax to get the real error code. Am i right? – Segmentation fault Sep 27 '16 at 00:23
  • Sorry about that, _edited it out_ getting to be habit as I write 64 bit code exclusively. That's exactly it, otherwise if you entered 15 characters let's say, then how would it differentiate from error code 15. – Shift_Left Sep 27 '16 at 00:32