Author Topic: Try Catch Exceptions implementation  (Read 737 times)

Offline stephane

  • Jr. Member
  • *
  • Posts: 2
  • Country: fr
  • “We don't have here an enduring city.” Rav Shaoul
    • My website [WIP]
Try Catch Exceptions implementation
« 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, 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, 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 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...




« Last Edit: February 07, 2022, 05:51:46 AM by stephane »
QU Wanna contribute?!

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2547
  • Country: us
Re: Try Catch Exceptions implementation
« Reply #1 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


Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 164
  • Country: gb
    • GPUZoo
Re: Try Catch Exceptions implementation
« Reply #2 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.
My graphics card database: www.gpuzoo.com

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2547
  • Country: us
Re: Try Catch Exceptions implementation
« Reply #3 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

Online JohnG

  • Jr. Member
  • *
  • Posts: 27
Re: Try Catch Exceptions implementation
« Reply #4 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

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2547
  • Country: us
Re: Try Catch Exceptions implementation
« Reply #5 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




Offline alCoPaUL

  • Jr. Member
  • *
  • Posts: 5
Re: Try Catch Exceptions implementation
« Reply #6 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"]

Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 164
  • Country: gb
    • GPUZoo
Re: Try Catch Exceptions implementation
« Reply #7 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 :)
My graphics card database: www.gpuzoo.com

Offline alCoPaUL

  • Jr. Member
  • *
  • Posts: 5
Re: Try Catch Exceptions implementation
« Reply #8 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..

;)

Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 164
  • Country: gb
    • GPUZoo
Re: Try Catch Exceptions implementation
« Reply #9 on: February 16, 2022, 01:04:05 AM »
OK, sorry. My browser translates for me, so I didn't realise that.
My graphics card database: www.gpuzoo.com

Online JohnG

  • Jr. Member
  • *
  • Posts: 27
Re: Try Catch Exceptions implementation
« Reply #10 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

Offline munair

  • Jr. Member
  • *
  • Posts: 27
  • Country: nl
  • keep it simple
    • SharpBASIC
Re: Try Catch Exceptions implementation
« Reply #11 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
SharpBASIC (www.sharpbasic.com) is a compiler in development that uses NASM as backend.

 

SMF spam blocked by CleanTalk