IncFile() and IncArray() macros [Updated 22-1-2009]

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
counting_pine
Site Admin
Posts: 6093
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Jan 23, 2009 2:30

@vdecampo: what did you have to change?
voodooattack
Posts: 605
Joined: Feb 18, 2006 13:30
Location: Alexandria / Egypt
Contact:

Postby voodooattack » Jan 24, 2009 11:08

toneboy wrote:No, worries.

So would there be an limitations (like extended ascii characters) in what can be stored and used in the files if each element in the array is a byte?

I tried to just change the data type in the macro from byte to integer but in running the program the result was unsuccessful.


Yes, you'd have to change the relevant fields in the array descriptor to specify the size of each item (1 for byte, 4 in case of an integer), and how many items there are (total_file_size/item_size, must also be rounded and the buffer padded in case of uneven lengths)..

That's what the macro did before, but it looks like something has changed in the way the compiler emits the values into asm blocks... not sure what happened exactly though.
roook_ph
Posts: 402
Joined: Apr 01, 2006 20:50
Location: philippines
Contact:

Postby roook_ph » Nov 30, 2009 12:03

This reply is late sorry but voodooattack there are a lot of fields how do I know which one is the right field? I have been unsuccessful in turning ubytes to uinteger
roook_ph
Posts: 402
Joined: Apr 01, 2006 20:50
Location: philippines
Contact:

Postby roook_ph » Dec 05, 2009 10:59

I cannot understand macro but through trial and error I made incarray to work in uinteger finally.
interaction
Posts: 8
Joined: Feb 03, 2010 19:15

Postby interaction » Feb 18, 2010 17:06

hay is there an example code on how the IncFile code could be used?

and is it also possible for this source code to put all files in to a seperate data besides a .exe?
VANYA
Posts: 1286
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Postby VANYA » Sep 27, 2011 5:04

Please tell me how to use not the absolute path to a macro?

The following entry:

IncArray(bmpdata, "C:\2.bmp")


error:

FbTemp.asm: Assembler messages:
FbTemp.asm:49: Error: file not found: C:.bmp


Thank.
VANYA
Posts: 1286
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Postby VANYA » Sep 27, 2011 7:56

All I've figured out: it is a slash.

Correct: IncArray(bmpdata, "C:/2.bmp")

Not Correct: IncArray(bmpdata, "C:\2.bmp")
MOD
Posts: 554
Joined: Jun 11, 2009 20:15

Postby MOD » Sep 27, 2011 10:17

Looks like it would use escape sequences. It's pretty much the same as if you would do something like this in FB:

Code: Select all

Print !"\2"
Print  "\2"
Sleep
FXG861
Posts: 86
Joined: Feb 01, 2009 17:10
Location: Canada
Contact:

Re: IncFile() and IncArray() macros [Updated 22-1-2009]

Postby FXG861 » Aug 01, 2012 3:07

Hi,

Is there a way to make these Macro compatible with fbJPEG.bas ?

http://www.freebasic.net/forum/posting.php?mode=reply&f=7&t=15284
VANYA
Posts: 1286
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: IncFile() and IncArray() macros [Updated 22-1-2009]

Postby VANYA » Feb 10, 2019 16:26

Who else is left on the forum who understands the code GNU ASM ?

Here's the code from the first post does not work on 64-bit compiler:

Code: Select all

#IFNDEF __INCARRAY_BI__
#DEFINE __INCARRAY_BI__

#macro IncArray(name, filename)
#if __FUNCTION__ <> "__FB_MAINPROC__"
    #error "IncArray() cannot be used within a function."
    ' just to avoid extra pointless errors and keep things cleaner
    dim label() as ubyte
#else   
    extern "c"
    extern name() alias #name as ubyte
    end extern
   
    asm
    #IF __FB_DEBUG__
        jmp .LT_END_OF_FILE_##name##_DEBUG_JMP
    #ELSE       
        .section .data
    #ENDIF
        .balign 1
        __##name##__start = .
        .incbin filename
        __##name##__len = . - __##name##__start
        .long 0                 ' add one null int as padding
        .globl ##name
        .balign 4
        ##name:
        .int __##name##__start  'data
        .int __##name##__start  'ptr
        .int __##name##__len    'size
        .int 1                  'element_len
        .int 1                  'dimensions
       
        ' dimTB
        .int  __##name##__len   'elements
        .int 0                  'lbound
        .int  __##name##__len-1 'ubound
        .LT_END_OF_FILE_##name##_DEBUG_JMP:
        .section .text
        .balign 16
    End asm
   
    #undef Name##size
#endif
#endmacro

#ENDIF        '__INCARRAY_BI__


Who is able to fix the code so that he began to work with 64-bit compiler?
marpon
Posts: 277
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: IncFile() and IncArray() macros [Updated 22-1-2009]

Postby marpon » Feb 10, 2019 17:36

I've recently reviewed these macros, they were not working not only on 64bis but also on the recent fbc version, still work fine on fbc 0.24

so i've done some evolution

Code: Select all

#Macro Macro_IncCommunEx(label , file , sectionName)
    dim label##_data as UByte Ptr
    dim label##_size as ULong
    #If __FB_DEBUG__
        asm jmp .LT_END_OF_FILE_##label##_DEBUG_JMP
    #Else
        asm .section sectionName                       ' Switch to/Create the specified section
    #EndIf
    asm .LT_START_OF_FILE_##label##:                   ' Assign a label to the beginning of the file
    asm __##label##__start = .                         ' Include the file
    asm .incbin ##file
    asm __##label##__len = . - __##label##__start      ' Mark the end of the the file
    asm .LT_END_OF_FILE_##label##:
    'asm .LONG 0                               'Pad it with a NULL Long (harmless, yet useful for text files)
    #If __FB_DEBUG__
        asm .LT_END_OF_FILE_##label##_DEBUG_JMP:
    #Else
        asm .section .text                             ' Switch back to the .text (code) section
        asm .balign 16                                 'was asm .balign 16
    #EndIf
    asm .LT_SKIP_FILE_##label##:
    asm mov dword ptr [label##_data] , offset .LT_START_OF_FILE_##label
    asm mov dword ptr [label##_size] , offset __##label##__len
   label##_ptr = label##_data
   label##_len = label##_size
#EndMacro

#Macro Macro_IncFileEx(label , file , sectionName)
    #ifndef __INC__##label##__DEF__
        #define __INC__##label##__DEF__
        #If __FUNCTION__ = "__FB_MAINPROC__"
            dim shared label##_ptr as UByte Ptr
          dim shared label##_len as ULong
            sub Sub_Inc_##label##()
                Macro_IncCommunEx( label , file , sectionName )
            end sub
            Sub_Inc_##label##()
        #else
           dim label##_ptr as UByte Ptr
           dim label##_len as ULong
            Macro_IncCommunEx( label , file , sectionName )
        #endif
    #else
        #error ===> error ##label## already defined
    #endif
#EndMacro



sub SubName()
    Macro_IncFileEx(EXE2 , "test32.exe" , .Data)
    print EXE2_len ,, EXE2_ptr
END SUB

Macro_IncFileEx(EXE , "test32.exe" , .Data)
print EXE_len ,, EXE_ptr

SubName()
SubName()
Macro_IncFileEx(EXE3 , "test32.exe" , .Data)
print EXE3_len ,, EXE3_ptr
sleep

not done the array ... i've never used it.

work on 32/64 bits on windows on 32 gas or gcc also

i was not able to use extern , so for __FB_MAINPROC__ i've used shared vars
VANYA
Posts: 1286
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: IncFile() and IncArray() macros [Updated 22-1-2009]

Postby VANYA » Feb 10, 2019 19:25

marpon, thank you for your help!

However, I am interested in an example of exactly what I pointed out , that is with an array. In any case, you helped me a lot. I tried to correct Joshy example , and the example seems to work. You could watch it and fix errors if any? I know little about, and therefore asking for outside help.

Code: Select all

#ifndef __incany_bi__
#define __incany_bi__

#macro incany(strFile,lpAny)
dim shared as Ubyte ptr ##lpAny
dim shared as integer  ##lpAny##__len__mem__
#print  ##lpAny##__len__mem__
  asm
  .section .text
  jmp .end_##lpAny
  .section .data
  .align 16
  .start_##lpAny:
  __##lpAny##__start__ = .
  .incbin ##strFile
   __##lpAny##__len = . - __##lpAny##__start__
  .section .text
  .align 16
  .end_##lpAny:
  lea eax, .start_##lpAny
  mov dword ptr [lpAny], eax
  mov dword ptr [##lpAny##__len__mem__] , offset __##lpAny##__len
  end asm
#endmacro
#endif ' __incany_bi__

incany("/home/user/11.c",pBuf)

? *cast(zstring ptr ,pBuf) , pBuf__len__mem__

sleep
marpon
Posts: 277
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: IncFile() and IncArray() macros [Updated 22-1-2009]

Postby marpon » Feb 11, 2019 12:23

VANYA, tested your code
it does work at least on win 10 32 or 64 gas or gcc

but if you ask optimization -gen gcc -O 2 or -gen gcc -O 3 it is blocked -gen gcc -O 0 or -gen gcc -O 1 it is okay

with my code even with -gen gcc -O 2 or -gen gcc -O 3 it is working why? sorry too litle competencies on asm ...

not tested on linux
VANYA
Posts: 1286
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: IncFile() and IncArray() macros [Updated 22-1-2009]

Postby VANYA » Feb 11, 2019 13:52

Okay , thanks for testing.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: maachal and 2 guests