Author Topic: help with txt parser  (Read 4112 times)

0 Members and 1 Guest are viewing this topic.

xteraco

  • Guest
help with txt parser
« on: July 23, 2006 »
anybody know  how to writte  a txt parser in fasm?

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: help with txt parser
« Reply #1 on: July 26, 2006 »
I never coded a txt parser in ASM, what do you have coded so far ?

Do you have a working txt parser coded in C/C++ ?  Because you can recode it in ASM, it's not easy but possible.
Challenge Trophies Won:

Offline yetifoot

  • ZX 81
  • *
  • Posts: 3
  • Karma: 0
    • View Profile
Re: help with txt parser
« Reply #2 on: July 26, 2006 »
I've written some in the past.  Heres a couple of snippets I worked on

This one breaks down the command line, try dragging files onto the exe.

Code: [Select]
format PE console 4.0

include 'win32ax.inc'

;.code

start:
  invoke GetCommandLine
  mov [cmdline_str], eax
  cinvoke strlen, [cmdline_str]
  mov [cmdline_len], eax
  mov [cmdline_pos], 0

  call ReadChar
  call SkipWhite
  reeloo:
  cmp [Look], -1
  je reeloo_end

  cmp [Look], '"'
  jne nex
  call ReadQuotedString
  jmp endblo
  nex:
  call ReadUnQuotedString

  endblo:

  call SkipWhite


  jmp reeloo
  reeloo_end:

  cinvoke printf, pri, [cmdline_pos]
  cinvoke system, pau

exit:
  invoke  ExitProcess,0

proc ReadChar
  mov eax, [cmdline_len]
  cmp [cmdline_pos], eax
  jl rea
  mov [Look], -1
  jmp rea_end
  rea:
  mov eax, [cmdline_str]
  add eax, [cmdline_pos]
  movzx eax, byte ptr eax
  mov [Look], eax
  inc [cmdline_pos]
  rea_end:

  ret
endp

proc ReadQuotedString
  call ReadChar
  rqsloop:
  cmp [Look], -1
  je rqsloop_end
  cmp [Look], '"'
  je rqsloop_end

  cinvoke printf, "%c", [Look]
  call ReadChar

  jmp rqsloop
  rqsloop_end:

  call ReadChar

  cinvoke printf, "%c", 10

  ret
endp

proc ReadUnQuotedString
  ruqsloop:
  cmp [Look], -1
  je ruqsloop_end
  cmp [Look], ' '
  je ruqsloop_end

  cinvoke printf, "%c", [Look]
  call ReadChar

  jmp ruqsloop
  ruqsloop_end:

  call ReadChar

  cinvoke printf, "%c", 10

  ret
endp

proc SkipWhite
  skloop:
  cmp [Look], -1
  je skloop_end
  cmp [Look], ' '
  jne skloop_end
  call ReadChar
  jmp skloop
  skloop_end:

  ret
endp

;.data
msg db "Hello World!", 0
pau db "PAUSE", 0
pri db "%i", 13, 10, 0

cmdline_str dd 0
cmdline_len dd 0
cmdline_pos dd 0
Look dd 0

data import

  library kernel32, 'KERNEL32.DLL', \
  msvcrt,   'MSVCRT.DLL'

  import kernel32, \
ExitProcess, 'ExitProcess', \
GetCommandLine, 'GetCommandLineA'

  import msvcrt, \
puts, 'puts', \
system, 'system', \
sleep,  '_sleep', \
printf, 'printf', \
strlen, 'strlen'
end data

This one was the start of an asm parser, it needs a text file test.asm
just put a few lines in there like

mov eax, 0
cmp eax, 0

Code: [Select]
use32
format PE console 4.0
entry Start

include 'win32a.inc'

SEEK_SET equ 0
SEEK_CUR equ 1
SEEK_END equ 2

;-------------------------------------------------------------------------------
Align 4
  Start:
    call Init
    cmp [Buffer], 0
    je Failed
    cinvoke puts, InFile
    cinvoke printf, szformat, [InFileSize], szbytes

    mov ecx, 0
    mov eax, [Buffer]
  printloop:
    cmp ecx, [InFileSize]
    je printloop_end
    movzx edx, byte ptr eax+ecx
    push eax ecx
    ;cinvoke printf, szformat2, edx
    pop ecx eax
    inc ecx
    jmp printloop
  printloop_end:

    call Parse

    call DeInit

  Failed:
    cinvoke system, szpause
    invoke ExitProcess, 0
;-------------------------------------------------------------------------------
Align 4
  ReadChar:
    mov eax, [Buffer_Pos]
    cmp eax, [Buffer_Len]
    mov [Look], -1
    jge ReadChar_End
    mov ecx, [Buffer_Pos]
    mov edx, [Buffer]
    movzx eax, byte ptr edx+ecx
    inc [Buffer_Pos]
    mov [Look], eax
  ReadChar_End:
    ret
;-------------------------------------------------------------------------------
Align 4
  IsComma:
    cmp eax, ','
    je IsComma_True
    mov eax, 0
    jmp IsComma_End
  IsComma_True:
    mov eax, -1
  IsComma_End:
    ret
;-------------------------------------------------------------------------------
Align 4
  IsWhite:
    cmp eax, ' '
    je IsWhite_True
    mov eax, 0
    jmp IsWhite_End
  IsWhite_True:
    mov eax, -1
  IsWhite_End:
    ret
;-------------------------------------------------------------------------------
Align 4
  IsNewLine:
    cmp eax, 10
    je IsNewLine_True
    cmp eax, 13
    je IsNewLine_True
    mov eax, 0
    jmp IsNewLine_End
  IsNewLine_True:
    mov eax, -1
  IsNewLine_End:
    ret
;-------------------------------------------------------------------------------
Align 4
  SkipComma:
  SkipComma_Loop:
    cmp [Look], -1
    je SkipComma_Loop_End
    mov eax, [Look]
    call IsComma
    cmp eax, -1
    jne SkipComma_Loop_End
    call ReadChar
    jmp SkipComma_Loop
  SkipComma_Loop_End:
    ret
;-------------------------------------------------------------------------------
Align 4
  SkipWhite:
  SkipWhite_Loop:
    cmp [Look], -1
    je SkipWhite_Loop_End
    mov eax, [Look]
    call IsWhite
    cmp eax, -1
    jne SkipWhite_Loop_End
    call ReadChar
    jmp SkipWhite_Loop
  SkipWhite_Loop_End:
    ret
;-------------------------------------------------------------------------------
Align 4
  SkipNewLine:
  SkipNewLine_Loop:
    cmp [Look], -1
    je SkipNewLine_Loop_End
    mov eax, [Look]
    call IsNewLine
    cmp eax, -1
    jne SkipNewLine_Loop_End
    call ReadChar
    jmp SkipNewLine_Loop
  SkipNewLine_Loop_End:
    ret
;-------------------------------------------------------------------------------
Align 4
  Parse:
    call ReadChar
    call SkipWhite
  parseloop:
    cmp [Look], -1
    je parseloop_end
    call Symbol
    call SkipComma
    call SkipWhite
    call SkipComma
    call SkipNewLine
    call SkipComma
    call SkipWhite
    call SkipComma
    jmp parseloop
  parseloop_end:
    ret
;-------------------------------------------------------------------------------
Align 4
  Symbol:
  symbolloop:
    cmp [Look], -1
    je symbolloop_end
    mov eax, [Look]
    call IsWhite
    cmp eax, -1
    je symbolloop_end
    mov eax, [Look]
    call IsNewLine
    cmp eax, -1
    je symbolloop_end
    mov eax, [Look]
    call IsComma
    cmp eax, -1
    je symbolloop_end
    cinvoke printf, szformat2, [Look]
    call ReadChar
    jmp symbolloop
  symbolloop_end:
    cinvoke printf, szformat2, 10
    ret
;-------------------------------------------------------------------------------
Align 4
  Init:
    cinvoke fopen, InFile, file_rb
    cmp eax, 0
    mov [hInFile], eax
    je File_Open_Error
    cinvoke fseek, [hInFile], 0, SEEK_END
    cinvoke ftell, [hInFile]
    mov [InFileSize], eax
    cinvoke fseek, [hInFile], 0, SEEK_SET
    cinvoke malloc, [InFileSize]
    mov [Buffer], eax
    cmp eax, 0
    je Allocate_Error
    cinvoke fread, [Buffer], 1, [InFileSize], [hInFile]
    cinvoke fclose, [hInFile]
    mov eax, [InFileSize]
    mov [Buffer_Len], eax
    jmp Init_End
  Allocate_Error:
    cinvoke puts, errmsg2
    cinvoke fclose, [hInFile]
    jmp Init_End
  File_Open_Error:
    cinvoke puts, errmsg1
    jmp Init_End
  Init_End:
    ret
;-------------------------------------------------------------------------------
Align 4
  DeInit:
    cinvoke free, [Buffer]
    ret
;-------------------------------------------------------------------------------

Align 4

Look    dd -1
Buffer_Len dd  0
Buffer_Pos dd  0
Buffer    dd  0

hInFile    dd  0
InFileSize dd  0

file_rb    db  "rb", 0
szformat   db  "%i%s", 10, 13, 0
szformat2  db  "%c", 0
szbytes    db  " bytes.", 0
InFile    db  "test.asm", 0
errmsg1    db  "Error opening file.", 0
errmsg2    db  "Error allocating memory.", 0
szpause    db  "PAUSE", 0

data import

  library kernel32, 'KERNEL32.DLL', \
  msvcrt,   'MSVCRT.DLL'

  import kernel32, \
ExitProcess, 'ExitProcess'

  import msvcrt, \
fopen,  'fopen', \
fread,  'fread', \
fwrite, 'fwrite', \
fseek,  'fseek', \
ftell,  'ftell', \
fclose, 'fclose', \
puts, 'puts', \
system, 'system', \
malloc, 'malloc', \
free, 'free', \
printf, 'printf'

end data

These are not optimal, or finsished, but give an idea.  If you want to see my methods of parsing in action, check this out, its a much more
complete example in freebasic.  I learnt my techniques from the Jack Crenshaw compiler tutorial (search google for it)

(FB code)

http://www.streetcds.co.uk/HTML%20Parser%20v1.0.bas

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17426
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: help with txt parser
« Reply #3 on: July 26, 2006 »
Hi Yetifoot, and thanks for helping :) :hi: welcome to the forum.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: help with txt parser
« Reply #4 on: July 28, 2006 »
Yeah! nice code and  :hi:
Challenge Trophies Won: