flat assembler
Message board for the users of flat assembler.

Index > DOS > Output own source code

Author
Thread Post new topic Reply to topic
El Tangas



Joined: 11 Oct 2003
Posts: 120
Location: Sunset Empire
El Tangas
This is a program I made some years ago, found it in a dusty folder in my HDD Wink .

It outputs its own source (a classic programming challenge).


Description:
Download
Filename: auto.zip
Filesize: 945 Bytes
Downloaded: 124 Time(s)

Post 30 Dec 2006, 17:30
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Here another very easier way
Code:
org $100

; Print source code
  mov     ah, $40
  xor     bx, bx
  mov     cx, sourceText.size
  mov     dx, sourceText
  int     $21

; Wait for keystroke
  xor     ax, ax
  int     $16

; Exit to DOS
  int     $20

sourceText:
  file 'auto.asm'
sourceText.size = $ - sourceText    
Post 30 Dec 2006, 18:19
View user's profile Send private message Reply with quote
El Tangas



Joined: 11 Oct 2003
Posts: 120
Location: Sunset Empire
El Tangas
Yeap, that's very nice and much cleaner. I knew something like that would come along, but that was pretty fast...
Post 30 Dec 2006, 19:17
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
LocoDelAssembly: that's cheating, though - outputting your own source is only really interesting if done without external file access Smile
Post 01 Jan 2007, 23:51
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Well is not too much external, the file that has the code I posted is named "auto.asm" so the source is accessing it self. Now I'm developing a way to exclude some parts, as example:
Code:
org $100 
;HIDING_ON
; Print source code 
  mov     ah, $40 
  xor     bx, bx 
  mov     cx, sourceText.size 
  mov     dx, sourceText 
  int     $21 
;HIDING_OFF
; Wait for keystroke 
  xor     ax, ax 
  int     $16 

; Exit to DOS 
  int     $20 

;HIDING_ON
sourceText: 
  file 'auto.asm' 
sourceText.size = $ - sourceText    


I have my brain so burned that I simple can't get it done. If someone wants to implement it I'm posting my unfinished code to provide an idea of how to do it but it doesn't work at this time...

Code:
macro InsertSourceCode labelName, sourceFile
{
local higing, index, base, start, fileSize, char, strChar, i, pStr, strLen

labelName:
  virtual at 0
    file sourceFile
    fileSize = $
  end virtual

  index = -1
  hiding = 0
  char = 0

  while fileSize > index
    while fileSize > index
      virtual at 0
        rb 2
        rb 2

        start = $
        store byte $ at 1
        db ";HIDING_OFF"
        store byte $-start at 3

        start = $
        store byte $ at 0
        db ";HIDING_ON"
        store byte $-start at 2

        text = $
        file sourceFile:index

        load pStr   byte from hiding
        load strLen byte from hiding+2

        i = 0
        while (fileSize > index+i) & (strLen > i)
          load strChar byte from pStr+i
          load char    byte from text+index+i

          i = i + 1
          if strChar <> char
            index = index + i
            break
          end if

        end while

        if strLen = i
          if hiding
            hiding = 0
            base = index
            index = index - base
          else
            hiding = 1
            end virtual
            break
          end if
        end if

        ; Skip line
        while (char <> 10) & (fileSize > index)
          load char byte from text+index
          index = index + 1
        end while

      end virtual
    end while
    file sourceFile:index, fileSize-index-1

  end while
  file sourceFile:index-1
  labelName#.size = $ - labelName
}    


[edit]Better I'll explain a little what I'm trying to do. The first thing is to check if the first word of the source line is the opposite of the current hiding (it searches for ";HIDING_ON" when hiding is 0 and searches for ";HIDING_OFF" when hiding is 1). If the search is successful then if the previous state was hiding off then it's set to on and breaks the while loop to write from a base displacement to the actual index (the current source it's a little far of this because I messed it up Sad) and enters to the loop again. If the string searching is for ";HIDING_OFF" then there is a base change and index correction but the while loop keeps working without interruption because switching to off (the default state) serves as start point for source "dumping".

Well, more or less that is my idea of how to get this work[/edit]
Post 02 Jan 2007, 00:00
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
Post 02 Jan 2007, 08:26
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.