flat assembler
Message board for the users of flat assembler.

flat assembler > Main > IP address in natural form

Author
Thread Post new topic Reply to topic
ProMiNick



Joined: 24 Mar 2012
Posts: 356
Location: Russian Federation, Sochi
what for fasmg is easy
Code:
struc IP address
        match d1.d2.d3.d4,address
                . = d1+d2 shl 8+d3 shl 16+d4 shl 24
        end match
end struc    

for fasm1 not so easy
Code:
struc IP address& {
        local tmp1,tmp2,tmp3
        tmp2=0
        tmp3 =0
        virtual at 0
                db `address
                repeat $
                        load tmp1 byte from %-1
                        if tmp1 = '.'
                                tmp3 = tmp3 shl 8 + tmp2
                                tmp2 = 0
                        else
                                tmp2 = tmp2 * 10 + tmp1 - '0'
                        end if
                        if %=$
                                tmp3 = tmp3 shl 8 + tmp2
                        end if
                end repeat
        end virtual
        tmp2=0
        repeat 4
                tmp2=tmp2 + ((tmp3 shr ((4-%)*8)) and $FF) shl ((%-1)*8)
        end repeat
        .=tmp2
}    


test:
Code:
localhost IP 127.0.0.1
dd localhost    

as backside effect fasm even allow
Code:
localhost IP '127.0.0.1'
dd localhost    

that will required from fasmg same complex realization as in fasm1.

however if dword data defenition would support such data format in core

is such patch in core will do the same without macros:
Code:
data_dwords:
        call    define_data
        jc      instruction_assembled
        lods    byte [esi]
        cmp     al,'('
        je      get_dword
        cmp     al,'?'
        jne     invalid_argument
        mov     eax,edi
        and     dword [edi],0
        scas    dword [edi]
        jmp     undefined_data
      get_dword:
        push    esi
        call    get_dword_value
        pop     ebx
        cmp     byte [esi],':'
        je      complex_dword
        cmp     byte [esi],'.'   ;new
        je      complex_IP_dword ;new
        call    mark_relocation
        stos    dword [edi]
        ret
      complex_dword:
        mov     esi,ebx
        cmp     byte [esi],'.'
        je      invalid_value
        call    get_word_value
        push    eax
        inc     esi
        lods    byte [esi]
        cmp     al,'('
        jne     invalid_operand
        mov     al,[value_type]
        push    eax
        cmp     byte [esi],'.'
        je      invalid_value
        call    get_word_value
        call    mark_relocation
        stos    word [edi]
        pop     eax
        mov     [value_type],al
        pop     eax
        call    mark_relocation
        stos    word [edi]
        ret
      complex_IP_dword:          ;new
        mov     esi,ebx
        cmp     byte [esi],'.'
        je      invalid_value
        call    get_byte_value
        push    eax
        inc     esi
        lods    byte [esi]
        cmp     al,'('
        jne     invalid_operand
        mov     al,[value_type]
        push    eax
        cmp     byte [esi],'.'
        je      invalid_value
        call    get_byte_value
        push    eax
        inc     esi
        lods    byte [esi]
        cmp     al,'('
        jne     invalid_operand
        mov     al,[value_type]
        push    eax
        cmp     byte [esi],'.'
        je      invalid_value
        call    get_byte_value
        push    eax
        inc     esi
        lods    byte [esi]
        cmp     al,'('
        jne     invalid_operand
        mov     al,[value_type]
        push    eax
        cmp     byte [esi],'.'
        je      invalid_value
        call    get_byte_value
        call    mark_relocation
        stos    byte [edi]
        pop     eax
        mov     [value_type],al
        pop     eax
        call    mark_relocation
        stos    byte [edi]
        pop     eax
        mov     [value_type],al
        pop     eax
        call    mark_relocation
        stos    byte [edi]
        pop     eax
        mov     [value_type],al
        pop     eax
        call    mark_relocation
        stos    byte [edi]
        ret    
or fasm will mark it as floating number, so proccessing should be realized somewhere else.
Is such format would be evil for fasm? I mean, just "dd 127.0.0.1"

_________________
I don`t like to refer by "you" to one person.
My soul requires acronim "thou" instead.
Post 20 Jul 2019, 17:25
View user's profile Send private message Send e-mail Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7362
Location: Kraków, Poland
ProMiNick wrote:
as backside effect fasm even allow
Code:
localhost IP '127.0.0.1'
dd localhost    

that will required from fasmg same complex realization as in fasm1.
There's a simpler way to have that working with fasmg:
Code:
struc IP: address
        match d1.d2.d3.d4,address
                . = d1+d2 shl 8+d3 shl 16+d4 shl 24
        else
                eval string '. IP ' + address shl 40
        end match
end struc    
Post 20 Jul 2019, 19:52
View user's profile Send private message Visit poster's website Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 572
Location: Belarus
Why prefer some particular format over others? What if someone wants to use the same format for something similar to GUIDs but smaller? What if I wish to write IPv6 addresses the same way? What about writing port number as well?

In fact, the syntax is basically
Code:
db 1, 0, 0, 127    
written somewhat different. Then, if we take the idea further, we should allow a few numbers delimited with a dot to be treated as if they were reversed byte values to be packed into the size specified by data definition directive (db, dw, dd, etc.). Now, besides having ambiguous syntax with floats (dw 127.1) and strings (if we allow string syntax as well), we have a whole bunch of new problems:
* Should we allow specifying less numbers than needed to fill the size?
* Which value should be used as the default and which bytes (MSB or LSB) should be filled with it?
* Should word- and dword-sized numbers also be supported (like every number in a dot-delimited list specifies word or dword, not byte)?


Those are a lot of questions, and the answers highly vary depending on the project/task. Now, if you ask me (not the one to say the last word though), we already have the syntax, you just need a little imagination (or a Big-Endian machine Smile):
Code:
MyIP db 1,0,0,127    
Post 20 Jul 2019, 20:44
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16782
Location: In your JS exploiting you and your system
You could create a reversed db macro
Code:
struc dbip [bite] { reverse db bite }
MyIP dbip 127,0,0,1    
Post 21 Jul 2019, 05:16
View user's profile Send private message Visit poster's website Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 572
Location: Belarus
revolution wrote:
You could create a reversed db macro
Code:
struc dbip [bite] { reverse db bite }
MyIP dbip 127,0,0,1    

Your macro has really big teeth Smile
Post 21 Jul 2019, 10:50
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-2019, Tomasz Grysztar.

Powered by rwasa.