Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the...

Preview:

Citation preview

Assembler Programming

Lecture 6

Lecture 6

• Full segment definitions. Procedures. Parameters. Modules.

Full segment definitions

• Complete control over segments.• Control over segments’ order.• Posibility of groupping segments.

SEGMENT directive

• Name – the name of the segment.• Align – memory boundary of beginning.• READONLY – generate an error while

modifying item in the segment.• Combine – how to combine segments.• Use – size of the segment (16 or 32 bit).• Class – segments of the same class are

groupped together.

[[combine]][[READONLY]][[align]]SEGMENTname [[class]][[use]]

SEGMENT directive

• BYTE - Next available byte address.• WORD - Next available word address.• DWORD - Next available doubleword address.• PARA - Next available paragraph address (16

bytes per paragraph). Default.• PAGE - Next available page address (256

bytes per page).

[[combine]][[READONLY]][[align]]SEGMENTname [[class]][[use]]

SEGMENT directive

• Protects against illegal sefl-modyfying code.• Assembler checks for the instructions that

modify the segment.• Generates an error if You attempt to write

directly to read-only segment.

[[combine]][[READONLY]][[align]]SEGMENTname [[class]][[use]]

SEGMENT directive

• PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default.

• PUBLIC - Concatenates all segments having thesame name to form a single, contiguous segment.

• STACK - Concatenates all segments having the same name and set SS:SP to the top of the segment.

• COMMON - Overlaps segments. The length is the length of the largest of the combined segments.

• MEMORY - Used as a synonym for the PUBLIC.• AT address - Assumes address as the segment

location.

[[combine]][[READONLY]][[align]]SEGMENTname [[class]][[use]]

SEGMENT directive

• USE16 – offset is 16-bit.• USE32 – offset is 32-bit.• FLAT.

[[combine]][[READONLY]][[align]]SEGMENTname [[class]][[use]]

SEGMENT directive

• Two segments of the same name are not combined if their class is different.

• All segments with the same class are next to each other.

[[combine]][[READONLY]][[align]]SEGMENTname [[class]][[use]]

Controlling segment order

• .SEQ – order in which segments are declared.• .ALPHA – alphabetical order within a module.• .DOSSEQ:

– Code segments,– Data segments:

• Segments not in class BSS or STACK,• BSS segments,• STACK segments.

Defining segment group

• Collection of segments not greater than 64kB.• Group example is DGROUP containing _DATA,

_BSS, CONST and STACK segments.• Name is the label of the group.• Segment can be any segment name.• Segment can’t belong to more than one group.• Does not affect segments’ order• 16-bit and 32-bit segments can’t be in the same group.

Segment [[, segment ]]GROUPname

Groupping example

MYGROUP GROUP ASEG, BSEG ; MYGROUP contains two

; segments ASEG and BSEG

MYGROUP GROUP CSEG ; Adds the CSEG segment into

; MYGROUP so it contains now

; three segments

ASSUME DS:MYGROUP ; DS refers to MYGROUP

mov AX,MYGROUP

mov DS,AX

MYGROUP GROUP ASEG, BSEG ; MYGROUP contains two

; segments ASEG and BSEG

MYGROUP GROUP CSEG ; Adds the CSEG segment into

; MYGROUP so it contains now

; three segments

ASSUME DS:MYGROUP ; DS refers to MYGROUP

mov AX,MYGROUP

mov DS,AX

Assuming segment registers

• Many instructions work only on offset part of the adress.

• They assume they work in default segment.• Assembler must know what segment contains

the address.• ASSUME directive.

ASSUME directive

• Segregister is the name of the segment register.

• Seglocation must be the name of the segment or the group.

• CS is automatically assumed with code segment.

• ASSUME affects only assembler not the processor, You should set values of segment registers with instructions.

segregister : seglocationASSUME

ASSUME directive

• NOTHING cancels the assumptions.• ERROR prevents the use of register.

ASSUME segregister : seglocation [,segregister : seglocation]ASSUME datareg : qualifiedtype [, datareg : qualifiedtype]ASSUME register : ERROR [, register : ERROR]ASSUME [register :] NOTHING [, register : NOTHING]ASSUME register : FLAT [, register : FLAT]

ASSUME segregister : seglocation [,segregister : seglocation]ASSUME datareg : qualifiedtype [, datareg : qualifiedtype]ASSUME register : ERROR [, register : ERROR]ASSUME [register :] NOTHING [, register : NOTHING]ASSUME register : FLAT [, register : FLAT]

Full definitions exampleStaseg SEGMENT STACK

Staseg ENDS

Dtaseg SEGMENT

... ; Place variables here

Dtaseg ENDS

Codseg SEGMENT

ASSUME DS:Dtaseg, SS:Staseg

mov AX,Dtaseg

mov DS,AX

... ; Place program here

Codseg ENDS

END

Staseg SEGMENT STACK

Staseg ENDS

Dtaseg SEGMENT

... ; Place variables here

Dtaseg ENDS

Codseg SEGMENT

ASSUME DS:Dtaseg, SS:Staseg

mov AX,Dtaseg

mov DS,AX

... ; Place program here

Codseg ENDS

END

Procedures

• Divide large programs into manageable units.• Allow separate testing.• Make code more efficient for repetitive tasks.• Make easier using code in future applications.

Procedure definition

• PROC directive starts the procedure.• ENDP directive ends the procedure.• Additionally, PROC can automatically:

– Preserve register values that should not changebut that the procedure might otherwise alter.

– Set up a local stack pointer, so that you canaccess parameters and local variables placedon the stack.

– Adjust the stack when the procedure ends.

Procedure definition example

My_proc PROC FAR......ret

My_proc ENDP

My_proc PROC FAR......ret

My_proc ENDP

RET [constant] instruction.

Procedure calling

• CALL instruction– Pushes the address of the next instruction on

the stack.– Passes control to a specified address.– Address can be label, register or memory.– Operand is calculated at run time.– Calls can be near or far.

Procedure definition• Procedures can be nested

My_proc PROC NEAR...

Your_prc PROC NEAR...ret

Your_prc ENDP...ret

My_proc ENDP

My_proc PROC NEAR...

Your_prc PROC NEAR...ret

Your_prc ENDP...ret

My_proc ENDP

Procedure definition without PROC and ENDP

• NEAR procedures:

Call NEAR PTR My_procCall NEAR PTR Your_prc

My_proc: ......retn

Your_prc LABEL NEAR......retn

Call NEAR PTR My_procCall NEAR PTR Your_prc

My_proc: ......retn

Your_prc LABEL NEAR......retn

Procedure definition without PROC and ENDP

• FAR procedures:

Call FAR PTR My_proc

My_proc LABEL FAR......retf

Call FAR PTR My_proc

My_proc LABEL FAR......retf

Parameters of the procedure

• Parameters can be passed to the procedure:– Through the registers.– Through the common memory (variables).– Through the stack.– Mixed.

• Extended PROC syntax allows passingparameters automatically.

Extended PROC syntax

• Automates accessing arguments and savingregisters.– Attributes – distance, langtype and visibility,– Reglist – list of registers that should be saved,– Parameter – list of parameters passed to the

procedure on the stack.• Label is the name of the procedure• PROC is a required keyword

[[Parameter[[:tag]]...]][[USES reglist]][[attributes]]PROClabel

Extended PROC syntax

• Distance – NEAR or FAR, for 386 alsoNEAR16, NEAR32, FAR16, FAR32.

• Langtype – BASIC, FORTRAN, PASCAL, C, STDCALL or SYSCALL.

[[Parameter[[:tag]]...]][[USES reglist]][[attributes]]PROClabel

[[<prologuearg>]][[visibility]][[langtype]][[distance]]

Extended PROC syntax

• Visibility – PRIVATE, PUBLIC, EXPORT.• Prologuearg – LOADDS, FORCEFRAME.

[[Parameter[[:tag]]...]][[USES reglist]][[attributes]]PROClabel

[[<prologuearg>]][[visibility]][[langtype]][[distance]]

Extended PROC syntax

• Reglist:– Must be separated by spaces or tabs– Assembler generates prologue code to push

registers on the stack.– Assembler generates epilogue code to pop the

saved registers off the stack.

[[Parameter[[:tag]]...]][[USES reglist]][[attributes]]PROClabel

Extended PROC syntax

• List of parameters.• Parameter - parmname [[:tag]]

– Parmname is the name of the parameter.– Tag is the qualifier of type of the parameter.– Tag can be the VARARG keyword for procedures

with variable number of parameters.

[[Parameter[[:tag]]...]][[USES reglist]][[attributes]]PROClabel

Procedure parameters example

My_add PROC NEAR C,arg1:WORD, arg2:WORD,count:WORDmov ax, arg1add ax, countadd ax, arg2ret

My_add ENDP

My_add PROC NEAR C,arg1:WORD, arg2:WORD,count:WORDmov ax, arg1add ax, countadd ax, arg2ret

My_add ENDP

Local variables• LOCAL directive.

– Creates local variables on the stack.– Must be on the line following the PROC statement.– Syntax of LOCAL directive:LOCAL vardef [[, vardef]]...– Each vardef represents one local variable:Label [[ [count] ]] [[:type]]

Local variable example

My_proc PROC NEAR arg:WORDLOCAL loc:WORD

.

.mov loc, 3 ; Initialize local variableadd ax, loc ; Add local variable to AXsub arg, ax ; Subtract local from arg

.

.ret

My_proc ENDP

My_proc PROC NEAR arg:WORDLOCAL loc:WORD

.

.mov loc, 3 ; Initialize local variableadd ax, loc ; Add local variable to AXsub arg, ax ; Subtract local from arg

.

.ret

My_proc ENDP

Procedure prototypes• PROTO directive.• The same function as in other HLL.• Includes name, types and names of

parameters.• Enable the assembler to check for unmatched

parameters.• Useful for procedures called from other

modules and other languages.

INVOKE directive• Automatically pushes arguments onto the stack.• Procedure must be declared previously.• Generates a sequence of instructions that:

– Converts arguments to the expected types.– Pushes arguments on the stack in the correct order.– Calls the procedure.– Cleans the stack when the procedure returns.

Prototype and invoking example.

My_proc PROTO NEAR, C argcount:WORD,arg1:WORD, arg2:WORD

My_proc PROC NEAR, C argcount:WORD,arg2:WORD, arg3:WORD...

My_proc ENDP

INVOKE My_proc, ax, x, y

My_proc PROTO NEAR, C argcount:WORD,arg1:WORD, arg2:WORD

My_proc PROC NEAR, C argcount:WORD,arg2:WORD, arg3:WORD...

My_proc ENDP

INVOKE My_proc, ax, x, y

Program modules• Big projects can be divided into modules.• Modules can be libraries for other languages.• Symbols that have to be used in other modules

must be visible in theese modules.• Two methods:

– With include file.– Without include file.

Include files• Can contain any MASM statement.• If used in more than one module can’t contain

statements that define and allocate memory for symbols.

• INCLUDE directive:

• Filename must be fully specified (with theextension).

INCLUDE filenameINCLUDE filename

Modules with include file• EXTERNDEF directive

– Public in the defining module– External in referencing modules– For variables and procedures

• COMM (communal)– As EXTERNDEF but are allocated by linker– Can’t be initialized– For variables

• PROTO– For procedures– Automatically issues EXTERNDEF

Include file exampleInclude prcmod.inc

My_proc PROC NEAR, C argcount:WORD,arg2:WORD, arg3:WORD...

My_proc ENDP

Include prcmod.inc

My_proc PROC NEAR, C argcount:WORD,arg2:WORD, arg3:WORD...

My_proc ENDP

Include prcmod.inc

INVOKE My_proc, ax, x, y

Include prcmod.inc

INVOKE My_proc, ax, x, y

My_proc PROTO NEAR, C argcount:WORD,arg1:WORD, arg2:WORD

My_proc PROTO NEAR, C argcount:WORD,arg1:WORD, arg2:WORD

My_proc PROTO NEAR, C argcount:WORD,arg1:WORD, arg2:WORD

My_proc PROTO NEAR, C argcount:WORD,arg1:WORD, arg2:WORD

Modules without include file• PUBLIC

– Makes a name visible outside the module in which itis defined.

– Must appear in defining module.• EXTERN

– Referenced name is defined and declared public inanother module.

• PUBLIC and EXTERN work with variables andprocedures

PUBLIC and EXTERN example.MODEL small, Pascal

PUBLIC Build_table, Var LABEL FAR....DATA

Var BYTE 0....CODE

Build_table PROC USES cx, dx, sizevar:WORD...ret

Build_table ENDP

.MODEL small, PascalPUBLIC Build_table, Var LABEL FAR

...

.DATAVar BYTE 0

...

.CODEBuild_table PROC USES cx, dx, sizevar:WORD

...ret

Build_table ENDP

EXTERN Var:BYTE, Build_table:FAR...mov al, Varcall Build_table...

EXTERN Var:BYTE, Build_table:FAR...mov al, Varcall Build_table...