Inferno #06
03 декабря 2004

For Coderz - Description of the modular structure of programs.

<b>For Coderz</b> - Description of the modular structure of programs.
  Description of modular structure of programs.



   Modules are files that contain parts of the code and all 
necessary information for their primeneniya.Kod module is 
relotsiruemym, as adjusted for any address. It is also possible 
communication module with other modules in which may contain 
the necessary functions and constants. 

   The library is still quite crude, but
tested in rabote.Napisan linker, which, however, still needs a 
thorough optimization, but their functions are carried out.


The header format of the module:
Offset Length Purpose



    0 8 Module Name

   8 2 Length of the module in bytes

                 including header

    10 a version of the module

   11 1 Flag of the module (bit 0 -

                 debug version)

   12 2 Offset Table

                 export points

   14 2 Offset Table

                 import points

    16 2 Offset Package

   18 6 free region


   Header is 24 bytes. After
it is a table of address configuration. She
has the format:



   0 2 Offset of a point from the beginning

                 code (Point_offset)

   +2 2 offset values ​​from

                start code (Value_offset)

                + Flags.


   If the high byte Value_offset> = # f0,
here is a link to the exported
tochku.Mladshy bytes Value_offset determines
number of points. In another case, after
module loading into memory is setting package on all points, ie 
at (CodeAddress + (Point_offset '# 3fff)) recorded value 
(CodeAddress + Value_offset) according to two senior bats

Point_offset. 0 in the corresponding bit
allows downloading junior or senior
bytes (CodeAddress + Value_offset) in the code.
Marker end table - # ffff.


   The table has the format of exported points:



   +0 N Symbolic name of the point in

                ASCIIZ format

   + N 1 Flag point. Each bit

                responsible for the type of point

   + N +1 2-point drift in the code or

               its value (for constants)


   Marker end table - # ff. Symbolic
name can contain additional
information about the types of parameters and return values, as 
well as the format of the transfer parameters to a function:


PointName [@ ] [# <OutputParams>]
[% CallType], 0



   InputParams' OutputParams list
registers, which store, respectively,
input and output parameters: a = A, b = B,
c = C, d = D, e = E, h = H, l = L, B = BC, D = DE, H = HL,
 X = IX, Y = IY, mN = memory in the stack N bytes.

   CallType - format settings:
F <Fastcall> - passing parameters in registers, C  
- parameters on the stack, stack clears the calling procedure, 
and P  - parameters on the stack,

stack clears the called procedure.

   For example:
Search @ HD # a% F

   Here, the input parameters passed to
register HL 'DE, output parameter - in
register A.


   Table of imported points is a sequence of ASCIIZ strings
zavershayuschyusya marker # ff.


   The presence of all three tables in the module
obyazatelno.Oni may be empty, in which case the table can only 
handle complete the table. 


   The linker is designed for bonding
two object files into one. For bonding large number of files 
needed to use the linker sequence for pairs files.


    Linker sequence of actions:

  1) Download both files into memory at
They are divided into male and female.
Master module is considered the first
 some functions.

  2) Making a single table exported points. If there are 
duplicates (Both the module export points with the same name), 
then an error error and the process prekraschaetsya.Pri added 
to the table names from the slave file is their correction (if 
they are not  constants) on the code length of the master file.


  3) View each name from a table imported points on the object 
of his presence in the Table export points. If there is

coincidences, the correction table is
configuration - redirect links to external points on the 
interior points. Otherwise If it is added is the name of the new

table imported tochek.Poisk in the table points are exported 
from the allowance of descriptive tags (@,#,%). 

  4) Optimize table exported
 points, as it may contain duplicates.

   5) Fill header structure.

   6) Merging tables in one area of ​​memory.

  7) Saving your module on
disc.


   Example assembler file that uses the library.

;*****************************************
*
; * MEM.H MEMORY MANAGEMENT FUNCTIONS
*
; * Copyright (C) Vitamin/CAIG/2001 2004
*
;*****************************************



         INCLUDE "MODULE", 197

                       , File with the library

         INCLUDE "MACRO", 196

                       ; Other files

        INCLUDE "ERRNO", 193

DEFINE _DEBUG_; define debug

                   ; Module version

, Setting the address of workspaces.
; Adres_koda,
; Adres_tablitsy_relokatsii,
; Adres_tablitsy_eksporta,
; Adres_tablitsy_importa
__MODULE # 8000, # 9000, # A000, # B000
__VERSION 0, module version
__MODULESTART; Beginning of the module

__EXTERN "InitMemDrv", InitMemDrv

                      ; Imported points
__EXTERN "Page", Page
__EXTERN "MemPortMask", MemPortMask
__EXTERN "MemMap", MemMap
__EXTERN "LoMemMap", LoMemMap
__EXTERN "MemMapSize", MemMapSize
__EXTERN "CurrentPage", CurrentPage
__EXTERN "PagesAvailable", PagesAvailable
__EXTERN "CurrentPID", CurrentPID
__EXTERN "SetErr", SetErr

;=========================================

;---------------------------------------- __PUBLIC "MEMINIT # 
a% F", FUNC 

                      ; Ad points
 MEMINIT

         CALL_ InitMemDrv

         LD_ (PagesAvailable), A

         INC A

         JR Z, MI_2

         LD E, 0

         SRA A

         RR E

         RRA

         RR E

         LD D, A

         LD_ (MemMapSize), DE

         ADD HL, DE

         LD DE, -64

         ADD HL, DE

         LD_ (LoMemMap), HL

         LD A, 16

         LD_ (MemPortMask), A

         LD_ HL, MemMap

         LD D, H, E, L

         INC E

         LD_ BC, (MemMapSize)

         DEC BC

         LD (HL), -1

        LDIR



         XOR A

        JP_ SetErr
 MI_2 LD A, ENOMEM

        JP_ SetErr

;----------------------------------------__ PUBLIC "GetPage # 
a% F", FUNC  GetPage; A - Current page


         LD_ A, (CurrentPage)

        RET

;----------------------------------------__ PUBLIC "GetPages # 
a% F", FUNC  GetPages


         LD_ A, (PagesAvailable)

        RET

; BLOCK MEMORY FUNCS
; MEMORY MAP CONSISTS OF ELEMENTS, EACH OF
; THEM MAY BE THE NEXT:
; 255 - FREE
, 254 - TEMPORARY BUSY (USED FOR
; GETMEM FUNC)
; OTHER-BUSY BY PROCESS (PID IS DATA)

;----------------------------------------__ PUBLIC "GetMem @ bD 
# H% F ", FUNC  GetMem


         LD A, B; B-QUANTITY

         AND A; DE-START BLOCK

                 ; (USE ONLY IN LOMEM CALLS)

         JR Z, GM_ERR; HL-> ADRESS SETS ERR

         CP 1964

        JR NC, GM_ERR



         IF? _DEBUG_

         DI

         ELSE

         CALL_ _MemSpinLock

        ENDIF



         _IsHiMem

         JR NC, GM_HMA

         LD_ HL, MemMap

        ADD HL, DE
 GM_LMC CALL_ _IsValidLogic

         JR C, GM_ERR

         CALL_ GM_GPAGE

        JR C, GM_LMC, GM_REL
 GM_HMA

        CALL_ _GetCurrentPageMapData



         CALL_ GM_GPAGE

        JR NC, GM_REL



         LD_ HL, (LoMemMap)

         CALL_ GM_GPAGE

        JR NC, GM_REL
 GM_ERR LD HL, 0, A, ENOMEM

        JR GM_REL +1
 GM_REL XOR A

         IF? _DEBUG_

         EI

         ELSE

         CALL_ _MemSpinUnlock

         ENDIF

        JP_ SetErr

 GM_GPAGE

        LD C, B
GM_L4 LD E, 64
 GM_L3 BIT 7, (HL)

         JR Z, GM_L1

         DEC B

         JR NZ, GM_L5

        LD_ A, (CurrentPID)
 GM_L2 LD (HL), A

         DEC HL, C

         JR NZ, GM_L2

         INC HL

        JP_ _LogicToPhisic

GM_L1 LD B, C
 GM_L5 INC HL

         DEC E

         JR NZ, GM_L3

         LD HL, 0

        RET

;----------------------------------------__ PUBLIC "FreeMem @ H 
# a% F ", FUNC ; Out: A = 0 - OK =- 1 - ERROR

; Sets Err
 FreeMem; HL-ADDRESS

         LD A, H

         CP 128

         LD A, EFAULT

        JPC_ SetErr



         IF? _DEBUG_

         DI

         ELSE

         CALL_ _MemSpinLock

        ENDIF



         LD A, H

         CP 192

         JR C, FM_LMA

         SUB 192

         LD L, A

         LD_ A, (CurrentPage)

         CALL_ _AddPageMapData

         LD B, 64

        JR FM_L2

 FM_LMA SUB 128

         LD E, A, D, 0

         LD_ HL, (LoMemMap)

         ADD HL, DE

         SUB 64

         NEG

        LD B, A
FM_L2 LD_ A, (CurrentPID)
 FM_L1 CP (HL)

         JR NZ, FM_RET

         LD (HL), -1

         INC HL

        DJNZ FM_L1
 FM_RET

         IF? _DEBUG_

         EI

         ELSE

         CALL_ _MemSpinUnlock

         ENDIF

         XOR A

        JP_ SetErr

;----------------------------------------__ PUBLIC "ReleaseMem% 
F", FUNC  ReleaseMem


         IF? _DEBUG_

         DI

         ELSE

         CALL_ _MemSpinLock

        ENDIF



         LD_ HL, MemMap

         LD_ DE, (MemMapSize)

         INC D

        LD_ A, (CurrentPID)
 RM_L2 CP (HL)

         JR NZ, RM_L1

        LD (HL), -1
 RM_L1 INC HL

         DEC E

         JR NZ, RM_L2

         DEC D

        JR NZ, RM_L2



         IF? _DEBUG_

         EI

         RET

         ELSE

         JP_ _MemSpinUnlock

        ENDIF

;----------------------------------------__ PUBLIC "MemTotal # 
B% F", FUNC  MemTotal; BC-> BLOCKS


         LD_ A, (PagesAvailable)

         INC A

         LD BC, 0

         RRA

         RR C

         RRA

         RR C

        RET

;----------------------------------------__ PUBLIC "MemAvail # 
B% F", FUNC  MemAvail; BC-> BLOCKS


         LD A, -1

        JR MemProcUsage

;----------------------------------------__ PUBLIC "MemUsage # 
B% F", FUNC  MemUsage


        LD_ A, (CurrentPID)
;----------------------------------------__ PUBLIC 
"MemProcUsage @ a # B% F ", FUNC MemProcUsage; A-PID


                     ; BC-> BLOCKS

         LD_ HL, MemMap

         LD B, L, C, L

         LD_ DE, (MemMapSize)

        INC D
 MPU_L2 CP (HL)

         JR NZ, MPU_L1

        INC BC
 MPU_L1 INC HL

         DEC E

         JR NZ, MPU_L2

         DEC D

         JR NZ, MPU_L2

        RET

;=========================================

 _MemSpinLock

        RET

 _MemSpinUnlock

        RET

 _GetCurrentPageMapData

        LD_ A, (CurrentPage)
 _GetPageMapData

        LD_ HL, MemMap
__PUBLIC "_AddPageMapData", FUNC
 _AddPageMapData

         LD E, 0

         RRA

         RR E

         RRA

         RR E

         ADD A, H

         LD H, A, A, E

         ADD A, L

         LD L, A

        RET

 _LogicToPhisic

         LDH_ A, MemMap

         SUB H

         NEG

         LD H, A, A, L

         AND 192

         RLA

         RL H

         RLA

         RL H

         LD A, L

         AND 63

         LD L, A

         LD_ A, (PagesAvailable)

         CP H

         LD A, 128

         JR Z, LTP1, C, LTP1

         LD A, H

         CALL_ Page

        LD A, 192
 LTP1 ADD A, L

         LD H, A, L, 0

        RET

 _IsValidLogic; HL-BLOCK POINTER IN TABLE

         PUSH DE, HL

         LD_ DE, MemMap

         AND A

         SBC HL, DE

         JR C, IVLR

         LD_ DE, (MemMapSize)

         SBC HL, DE

        CCF
 IVLR POP HL, DE

        RET

__MODULEEND; End module

 __COMPILE "Mem"; create a service package

               , And copying the file to disk
 __MAKEHEADER "Mem", a

          , The creation of an auxiliary file


   After compiling a 1 page created
file named "mem" to read:

; Header file for modules construction.
; (C) Vitamin/CAIG/2001 all rights reserved



         MACRO MEMINIT_

         CALL_ MEMINIT

        ENDM



         MACRO GetPage_

         CALL_ GetPage

        ENDM



         MACRO GetPages_

         CALL_ GetPages

        ENDM



         MACRO GetMem_

         LD B,




Other articles:

Inferno - Entered from the editor.

Interview - Interview with AIG - coder from the group MKHG.

Softinka - ACE 0.888: different from 0.666

Softinka - macro assembler debugger ALASM 4.47: difference from 4.44

For Coderz - Arithmetic coding.

Inferno - The authors of the magazine.

Softinka - BGE 4 graphical editor for ZX.

Events - The Compo 2: The results of the vote.

For Coderz - Decompiling programs - the revival of the old prog.

Inferno - Errors in the previous numbers.

For Coderz - Small programmers' tricks.

DIY - The scheme of my elektrofumigatora.

Gameland - about passed games: Imperia 2, Hexagonal Filler, From Beyond.

Iron - device extended keyboard (58 keys).

Gamedev - Gaming cycle - a cycle within which caused all the sub games.

Gameland - the passage of Lords of Time on Level 9.

For Coderz - Macros Part 2 - makes your life in programming.

Inferno - Letters to the Editor.

Gameland - passing a level playing Raven Black.

For Coderz - Description of the modular structure of programs.

Inferno - On the shell.

Softinka - the benefits of packing algorithm Optimal LZH.

Events - Serpukhov Festival ParaDiGMus party 2003. As it was.

Events - Serpukhov Festival ParaDiGMus party 2003. Afterparty.

Gameland - the passing game The Price of Magik by Level 9.

Iron - Description of a block of memory from the printer Robotron CM 6329.01 M. Part 1.

Iron - Description of a block of memory from the printer Robotron CM 6329.01 M. Part 2.

Advertising - advertising and announcements.

DIY - advice on repair hours, Dream Cast and joystick.

Interview - An Interview with Shaitan / Stars of Keladan: Interred Inferno.

Gameland - the passing game from the Level 9 Snowball.

Iron - Video GoldStar RN800AW Art vision. The history of repair.

Iron - Video GoldStar RN800AW Art vision. Tips on disassembly and repair.

Interview - an interview with musician Visual ^ Extreme (Sergei Agapov).

Gamedev - the assembly of the game Wolfenstein 2004. Part 1.

Gamedev - the assembly of the game Wolfenstein 2004. Part 2.

For Coderz - How to get the sound device more bits.


Темы: Игры, Программное обеспечение, Пресса, Аппаратное обеспечение, Сеть, Демосцена, Люди, Программирование

Similar articles:
WANTED - Searching software ...
Rat romp - a full analysis of feces - the magazine "Scream 2".

В этот день...   29 April