41
Assembler Programming Lecture 6

Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

Assembler Programming

Lecture 6

Page 2: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

Lecture 6

• Full segment definitions. Procedures. Parameters. Modules.

Page 3: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

Full segment definitions

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

Page 4: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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]]

Page 5: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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]]

Page 6: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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]]

Page 7: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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]]

Page 8: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

SEGMENT directive

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

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

Page 9: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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]]

Page 10: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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.

Page 11: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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

Page 12: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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

Page 13: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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.

Page 14: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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

Page 15: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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]

Page 16: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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

Page 17: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

Procedures

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

Page 18: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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.

Page 19: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

Procedure definition example

My_proc PROC FAR......ret

My_proc ENDP

My_proc PROC FAR......ret

My_proc ENDP

RET [constant] instruction.

Page 20: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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.

Page 21: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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

Page 22: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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

Page 23: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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

Page 24: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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.

Page 25: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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

Page 26: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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]]

Page 27: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

Extended PROC syntax

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

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

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

Page 28: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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

Page 29: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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

Page 30: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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

Page 31: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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]]

Page 32: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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

Page 33: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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.

Page 34: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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.

Page 35: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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

Page 36: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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.

Page 37: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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

Page 38: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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

Page 39: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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

Page 40: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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

Page 41: Lecture 6db.zmitac.aei.polsl.pl/KT/Lecture6.pdfSEGMENT directive • PRIVATE - Does not combine the segment with other segments, even if they have the same name. Default. • PUBLIC

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...