English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Asm / Hardware

 
 
Thread Tools
Old 05 May 2021, 20:26   #1
colourspace
Registered User
 
colourspace's Avatar
 
Join Date: Apr 2020
Location: CHICAGO / USA
Posts: 32
Stream parsing techniques

i'm using dissassembler.library but i want a bit more flexibility on the output it generates, so i'm parsing the output. since the lib uses a putchar function, i'm parsing the text as it appears, making it a bit more tricky than if i were parsing a text buffer. i ended up using indices in the text to flip bit flags on section starts and do stuff accordingly but i'd rather not be relying on indices since this could break in a future version of the lib. here's a snippet:

Code:
DL_ParseOutput:
    cmp.b    #10,d0
    beq.w    DL_Parse_ResetLine

    move.w    DL_Pos,d1
    cmp.w    #1,d1
    beq.w    DL_Parse_AddrStart

    cmp.w    #13,d1
    beq.w    DL_Parse_HexStart

    cmp.w    #40,d1
    beq.w    DL_Parse_MnemonicStart

    btst    #DL_COMMENT,DL_Bits
    bne.w    DL_Parse_InComment

    btst    #DL_OPERANDS,DL_Bits
    bne.w    DL_Parse_InOperands

    btst    #DL_MNEMONIC,DL_Bits
    bne.w    DL_Parse_InMnemonic

    btst    #DL_HEX,DL_Bits
    bne.w    DL_Parse_InHex

    btst    #DL_ADDR,DL_Bits
    bne.w    DL_Parse_InAddr
any better tips or tricks for doing something like this? i'd rather not alloc a buffer just to parse it all at once

p.s. if thor is reading this, an _LVODisassembleF would be nice
colourspace is offline  
Old 05 May 2021, 20:44   #2
Thomas Richter
Registered User
 
Join Date: Jan 2019
Location: Germany
Posts: 3,214
Sorry, I do not quite understand.... what precisely is the problem?
Thomas Richter is offline  
Old 05 May 2021, 21:44   #3
colourspace
Registered User
 
colourspace's Avatar
 
Join Date: Apr 2020
Location: CHICAGO / USA
Posts: 32
there is no problem.. i'm asking if some of the m68k gurus could provide tips or techniques they use for parsing structured data when it's coming in a byte at a time. that i'm trying to parse dissassembler.library's output is just to provide context
colourspace is offline  
Old 06 May 2021, 07:53   #4
Thomas Richter
Registered User
 
Join Date: Jan 2019
Location: Germany
Posts: 3,214
Quote:
Originally Posted by colourspace View Post
there is no problem.. i'm asking if some of the m68k gurus could provide tips or techniques they use for parsing structured data when it's coming in a byte at a time. that i'm trying to parse dissassembler.library's output is just to provide context

Well, in this case it's quite simple: Wait for the LF = 0x0a to arrive, then parse the entire line. Or disassemble line by line, increasing the start of the disassembly each time again.


The classical approaches for this problem - though complete overshooting for this problem - are either a state machine (keep an internal state such that is updated upon the input), or co-routines (essentially, a stack swap such that each call you receive looks like a return-from-subroutine to your function).


The broader question I really had is why you would need to do some parsing?
Thomas Richter is offline  
Old 06 May 2021, 18:25   #5
colourspace
Registered User
 
colourspace's Avatar
 
Join Date: Apr 2020
Location: CHICAGO / USA
Posts: 32
Quote:
Originally Posted by Thomas Richter View Post
co-routines (essentially, a stack swap such that each call you receive looks like a return-from-subroutine to your function).
very interesting.. definitely overkill for this particular use case but this is the kind of knowledge i was hoping to get from the thread. having come from high-level languages, i didn't even have the faintest idea how i would implement something like co-routines in asm but this sheds some light on that

Quote:
Originally Posted by Thomas Richter View Post
The broader question I really had is why you would need to do some parsing?
i'm retrofitting disassembler.library in an application where having its output match the old format makes things much easier
colourspace is offline  
Old 07 May 2021, 19:29   #6
Thomas Richter
Registered User
 
Join Date: Jan 2019
Location: Germany
Posts: 3,214
Coroutines aren't hard, there is just a lot of high-level mumble-jumble around it in some high level languages. There isn't really much required to get them working:
Code:
/*
** The following structure handles the coroutine
*/
struct Coroutine {
  struct StackSwapStruct cr_StackSwap;
  /* potentially more private stuff here */
  UBYTE                  cr_Stack[CR_STACKSIZE];  /* Stack itself */
};
A very small assember stub is sufficient to swap between coroutine and main function:
Code:
_CallCo:                        ;a0 = coroutine structure, a1 = parameter
        movem.l d2-d7/a2-a3/a5-a6,-(a7) ;save registers
        move.l cr_StackSwap+stk_Pointer(a0),a6
        movem.l a0-a1,-(a6)     ;push argument on new stack
        move.l a6,cr_StackSwap+stk_Pointer(a0)
        move.l _SysBase(a4),a6
        jsr _LVOStackSwap(a6)
        movem.l (a7)+,d0-d7/a2-a3/a5-a6 ;retrieve parameter and registers of the caller.
        move.l d0,a0            ;also in a0
        move.l d1,a1
        move.l d1,d0            ;the return code is also the second argument
        rts
Then, use "ret=CallCo(co,args)" to call a co-routine. In the co-routine, the argument "arg" appears as result-code of "CallCo", and as soon as the called co-routine returns with "CallCo(co,args)", its own arg argument returns to the first caller as "ret".

It first creates a whirl in your head, but once you understand the idea, it is quite simple. Unlike what many high-level languages seem to imply, co-routines are quite simple.
Thomas Richter is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Factory]3d polygon techniques Samurai_Crow project.Amiga Game Factory 0 12 December 2017 09:06
Parsing an HTML file in Arexx Gundam Coders. General 7 09 April 2014 00:16
xml parsing in arexx amiga_user Coders. General 2 17 November 2011 15:42
error parsing global configuration file line 16 DDNI project.WHDLoad 7 21 March 2011 13:09

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 13:19.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.07343 seconds with 15 queries