NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: stephane on February 07, 2022, 05:07:13 AM

Title: Try Catch Exceptions implementation
Post by: stephane on February 07, 2022, 05:07:13 AM
Hi assembleros & assembleras,

Is it reasonable/meaningful/feasible to implement in Assembly the try/catch exceptions feature that some high-level languages offer?

For examples:

Python

Code: [Select]
"""
A basic exception block for error(s) handling.
It is possible to declare multiple `except` statements for each
errors type we want to catch.

Variables
------------

fd: _io.TextIOWrapper
    A file object

errmsg: string (object)
    The error message to output
"""

try:
    fd: TextIO = open("./path/to/file.txt", 'r')
    # Some code with fd...
    fd.close()
except FileNotFoundError:
    errmsg: str = f"Error {errno.ENOENT}: {os.strerror(errno.ENOENT)} {fd}"
    raise FileNotFoundError(errmsg)

# End of example (Python 3)

C++

Code: [Select]
/.**
* stdc++ >= 11
*/

std::fstream ifs;  // input file stream object (in `fstream` header)

try
{
    ifs.open("./path/to/file.txt", std::ios::in);
    ifs.exceptions(ifs.failbit); // std::ios::failbit
    /* Some code with ifs */
    ifs.close();
}
catch (const std::ios_base::failure& ex)
{
    std::cerr << "Error code: " << ex.code() << ", Error message: " << ex.what() << 'n';
}

You obviously understand that as a beginner I am not at all fluent in Assembly (https://forum.nasm.us/index.php?topic=2796.msg12538#msg12538), I can barely babble a few lines of code, even some sequence of onomatopoeia... so maybe my question is incongruous(1), or worst: preposterous, in the (n)asm context.

Perhaps you would be inclined to tell me: "Wow, slow down, you inconsequent evanescent nitwit dreamer! Learn the very basics first, then you'll dive in more complexe topics."
To that I would answer: yeah, I agree... partially, because these kind of questions helps me understand the core concepts of the language.
For example, while searching about printing to the screen, I was very disappointed when I discovered that in Assembly, most of the time many things are done via sytem calls; initially I thought that the language in the first place was a mean to control directly the hardware, independently from the OS, and then I learned about Real/Protected Modes (https://wiki.osdev.org/Real_mode), and how the system and the kernel play a major role in the process of producing and running "executable binary strings".

From misconceptions to clarity and understanding there are some steps that require (at least for me) an approach trying to embrace a more holistic view (2).



(1) Lets go to incongruous speed (https://www.youtube.com/watch?v=Rek9KYnqQnU) right now!
(2) As well as the need for the provision of an adequat toolchain (more pragmatically, with the aim of moving from theory to practice), but that's another subject.
     From beginner to master, the learning curve is more a 3 (or more) dimensions one than a 2D...

(https://upload.wikimedia.org/wikipedia/commons/thumb/2/29/Helix.svg/410px-Helix.svg.png)

https://commons.wikimedia.org/wiki/File:Helix.svg (https://commons.wikimedia.org/wiki/File:Helix.svg)


Title: Re: Try Catch Exceptions implementation
Post by: Frank Kotler on February 08, 2022, 04:27:13 AM
Hi Stephane,
Bonjour (the limit of my French)

I don't think I can help you much. What you ask about is, I believe, called "Structured Excaption Handling" or "SEH". I think it's a "Windows thing". Maybe you could find more searching by that name. I see in your other post that you're a Linux user, so maybe this is not available to you? I don't know much about it.

In any case,  Good  Luck!

Best,
Frank

Title: Re: Try Catch Exceptions implementation
Post by: debs3759 on February 08, 2022, 06:49:43 PM
Is this not something that can be done with "if, elseif,..., else" blocks?

If it can, in assembler that's equivalent to compares and conditional instructions such and jumps and others.
Title: Re: Try Catch Exceptions implementation
Post by: Frank Kotler on February 09, 2022, 01:33:58 AM
I think what you propose would happen at assemble time. I think try/exceot happens at run time. IF I understand it... which I may not...

Best,
Frank
Title: Re: Try Catch Exceptions implementation
Post by: JohnG on February 09, 2022, 11:17:04 PM
Hi,

I would be curious to see how you were going to write the 'Try' section asm code. As there is an ÖpenFile function in the kernel32, and there is an error code returned if it fails. You could just test what is returned. You could also look at the 'GetLastError' or its alternative.

John
Title: Re: Try Catch Exceptions implementation
Post by: Frank Kotler on February 10, 2022, 12:58:09 AM
https://docs.microsoft.com/en-us/windows/win32/debug/structured-exception-handling

I don't know how to do this. That doesn't ,mean much.

Best,
Frank



Title: Re: Try Catch Exceptions implementation
Post by: alCoPaUL on February 16, 2022, 12:30:51 AM
i thought iczelion had a tutorial on this..

http://www.interq.or.jp/chubu/r6/masm32/masm006.html

but scour repos of vx groups (virus writing groups) zines and you can see SEH implemented in their Viruses (and some funky term like delta offset) [e.g. 29a zines "Jacky Qwerty"]
Title: Re: Try Catch Exceptions implementation
Post by: debs3759 on February 16, 2022, 12:44:05 AM
i thought iczelion had a tutorial on this..

http://www.interq.or.jp/chubu/r6/masm32/masm006.html

but scour repos of vx groups (virus writing groups) zines and you can see SEH implemented in their Viruses (and some funky term like delta offset) [e.g. 29a zines "Jacky Qwerty"]

Or find the English language version of the tutorials by googling "Iczelions Win32 Assembly Tutorials". I'd forgotten about that one, have just downloaded the set as a .chm file. Will have to translate the code to nasm format :)
Title: Re: Try Catch Exceptions implementation
Post by: alCoPaUL on February 16, 2022, 12:47:16 AM
hey debs,

the site got both English (left column) and Japanese (right column) versions. they even got the sample ready to assemble files..

;)
Title: Re: Try Catch Exceptions implementation
Post by: debs3759 on February 16, 2022, 01:04:05 AM
OK, sorry. My browser translates for me, so I didn't realise that.
Title: Re: Try Catch Exceptions implementation
Post by: JohnG on February 16, 2022, 02:23:14 AM
Hi all,

for a little more reading, this from the FASM forum..

https://board.flatassembler.net/topic.php?t=11736

John
Title: Re: Try Catch Exceptions implementation
Post by: munair on February 27, 2022, 10:06:34 AM
A while ago I copied the following snippet (which I might use as reference for a compiler to generate a try block). The snippet may have come from this forum, but I'm not sure.
Code: [Select]
Function1:
  ;// Set up 3 nested _try levels (thereby forcing 3 scopetable entries)

  ; Beginning of first _try
  push dword .End_of_First_Frame
  push dword .EXCEPTION_HANDLER_A
  push dword [fs:0]
  mov [fs:0], esp

  ;_try {

    ; Beginning of second _try
    push dword .End_of_Second_Frame
    push dword .EXCEPTION_HANDLER_B
    push dword [fs:0]
    mov [fs:0], esp

    ;_try {

      ; Beginning of third _try
      push dword .End_of_Third_Frame
      push dword .EXCEPTION_HANDLER_C
      push dword [fs:0]
      mov [fs:0], esp

      ;_try {

        ;WalkSEHFrames();    // Now show all the exception frames
        call WalkSEHFrames

      ;}

      ; End of third _try
      jmp .End_of_Third_Frame

    .EXCEPTION_HANDLER_C:
      ;_except( EXCEPTION_CONTINUE_SEARCH ) {

      ;}
      ret

    .End_of_Third_Frame:
      pop [fs:0]
      add esp, 4
    ;}

    ; End of second _try
    jmp .End_of_Second_Frame

  .EXCEPTION_HANDLER_B:
    ;_except( EXCEPTION_CONTINUE_SEARCH ) {

    ;}
    ret

  .End_of_Second_Frame:
    pop [fs:0]
    add esp, 4
  ;}

  ; End of first _try
  jmp .End_of_First_Frame

.EXCEPTION_HANDLER_A:
  ;_except( EXCEPTION_CONTINUE_SEARCH ) {

  ;}
  ret

.End_of_First_Frame:

  pop [fs:0]
  add esp, 4

  ; This is the unprotected end of our Function1 procedure.

  ret