Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. Conditional assembly

Conditional assembly

Scheduled Pinned Locked Moved Algorithms
questionalgorithmshelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    A Jordison
    wrote on last edited by
    #1

    I've written a 6502 assembler (for Commodore machines) and one of its features is conditional assembly, like this;

    ifdef var1

    else

    elseif

    So if var1 is defined then is assembled, else . However I want to expand this by allowing nested conditional assembly, like this:

    ifdef var1
    ifdef var2

    else

    endif
    else

    endif

    Basically the way my assembler works at the moment is if the 'ifdef' condition fails (the variable is not defined) then all of the code following it (until the corresponding 'else' directive) is ignored. It's the 'corresponding else' part which is the problem here. How do I determine which 'else' corresponds with which 'ifdef'? Please note that I can't use the code indentation to determine which 'level' the directives are on. In real life, all of the code will be in the first column (no indentation at all) as my assembler needs directives to be in the first column. I don't need specific code as such, I'm more interested in an algorithm for this. In case you're interested, here's my assembler: www.ajordison.co.uk

    J L 2 Replies Last reply
    0
    • A A Jordison

      I've written a 6502 assembler (for Commodore machines) and one of its features is conditional assembly, like this;

      ifdef var1

      else

      elseif

      So if var1 is defined then is assembled, else . However I want to expand this by allowing nested conditional assembly, like this:

      ifdef var1
      ifdef var2

      else

      endif
      else

      endif

      Basically the way my assembler works at the moment is if the 'ifdef' condition fails (the variable is not defined) then all of the code following it (until the corresponding 'else' directive) is ignored. It's the 'corresponding else' part which is the problem here. How do I determine which 'else' corresponds with which 'ifdef'? Please note that I can't use the code indentation to determine which 'level' the directives are on. In real life, all of the code will be in the first column (no indentation at all) as my assembler needs directives to be in the first column. I don't need specific code as such, I'm more interested in an algorithm for this. In case you're interested, here's my assembler: www.ajordison.co.uk

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      The common method for processing such nested commands is using a stack. If you encounter a condition, you push it onto the stack which holds the required information like type (if) and evaluated result. Upon an else, you modify the current stack value accordingly (e.g. by setting an else flag), and upon an endif you pop (discard).

      A 1 Reply Last reply
      0
      • J Jochen Arndt

        The common method for processing such nested commands is using a stack. If you encounter a condition, you push it onto the stack which holds the required information like type (if) and evaluated result. Upon an else, you modify the current stack value accordingly (e.g. by setting an else flag), and upon an endif you pop (discard).

        A Offline
        A Offline
        A Jordison
        wrote on last edited by
        #3

        Yes this is the approach I tried first, but I still had the same problem when I hit an 'else', i.e. which 'else' is it?

        J 1 Reply Last reply
        0
        • A A Jordison

          Yes this is the approach I tried first, but I still had the same problem when I hit an 'else', i.e. which 'else' is it?

          J Offline
          J Offline
          Jochen Arndt
          wrote on last edited by
          #4

          It is the else for the current (top of stack, recent) if.

          1 Reply Last reply
          0
          • A A Jordison

            I've written a 6502 assembler (for Commodore machines) and one of its features is conditional assembly, like this;

            ifdef var1

            else

            elseif

            So if var1 is defined then is assembled, else . However I want to expand this by allowing nested conditional assembly, like this:

            ifdef var1
            ifdef var2

            else

            endif
            else

            endif

            Basically the way my assembler works at the moment is if the 'ifdef' condition fails (the variable is not defined) then all of the code following it (until the corresponding 'else' directive) is ignored. It's the 'corresponding else' part which is the problem here. How do I determine which 'else' corresponds with which 'ifdef'? Please note that I can't use the code indentation to determine which 'level' the directives are on. In real life, all of the code will be in the first column (no indentation at all) as my assembler needs directives to be in the first column. I don't need specific code as such, I'm more interested in an algorithm for this. In case you're interested, here's my assembler: www.ajordison.co.uk

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            It's called "lexical analysis"; and includes "token matching" (which is what you're trying to do): [Lexical analysis - Wikipedia](https://en.wikipedia.org/wiki/Lexical\_analysis) [Compiler Construction/Lexical analysis - Wikibooks, open books for an open world](https://en.wikibooks.org/wiki/Compiler\_Construction/Lexical\_analysis)

            "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups