100% found this document useful (1 vote)
366 views

Assembly Language Program Development With MASM

The document discusses assembly language statement syntax and directives used in MASM (Microsoft Macro Assembler). It describes register operands, addressing modes, data types for constants, arithmetic/logical operators, segment directives and data directives for defining variables. Modular programming directives like PROC, ENDP, PUBLIC and EXTRN are used to define and call procedures from other modules.

Uploaded by

Limberto Suarez
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
366 views

Assembly Language Program Development With MASM

The document discusses assembly language statement syntax and directives used in MASM (Microsoft Macro Assembler). It describes register operands, addressing modes, data types for constants, arithmetic/logical operators, segment directives and data directives for defining variables. Modular programming directives like PROC, ENDP, PUBLIC and EXTRN are used to define and call procedures from other modules.

Uploaded by

Limberto Suarez
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

Assembly Language Program Development with MASM Statement Syntax for a Source Program -Assembly language statement syntax

Labe OPCod Operand( ; l: e s) Comment( s) Example: Start: MOV CX, 10 ;Load a count of 10 into register CX MOV CX 10 ;Initialize the count in CX CLC ;Clear the carry flag Symbols used for specifying register operand Symb Register ol AX Accumulator register AH Accumulator register high byte AL Accumulator register low byte BX Base register BH Base register high byte BL Base register low byte CX Counter register CH Counter register high byte CL Counter register low byte DX Data register DH Data register high byte DL Data register low byte SI Source index register DI Destination index register SP Stack pointer register BP Base pointer register CS Code segment register DS Date segment register SS Stack segment register ES Extra segment register Addressing Mode Register Immediate Direct Register Indirect Operand Destinati on Source Destinati on Source Example MOV AX, VARIABLE MOV AL, 15H MOV VARIABLE, AX MOV AX, [SI] MOV AX, [BP] MOV AX, [DI] MOV AX, [BX] Segme nt Data Data Stack Data Data

Based

Destinati on Source Destinati on

Indexed Based Indexed

MOV [BX]+DISP, AL MOV [BP]+DISP, AL MOV AL, [SI] MOV AL, [DI] MOV [BX][SI] +DISP, AH MOV [BX][DI] +DISP, AH MOV [BP][SI] +DISP, AH MOV [BP][DI]+ DISP, AH

Data Stack Data Data Data Data Stack Stack

-Directive statement syntax Labe Directi Operand ; l: ve (s) Comment( s) Example: DB 0FFH ; Allocate a byte location initialized to FFH DB 0FFH, 0FFH, 0FFH, 0FFH, 0FFH -Constants in a statement Constants in an instruction or directives can be expressed in any of may data types such binary, decimal, hexadecimal, octal, and character data types. The first digit of a hexadecimal number must be on of the numbers in the range 0 through 9. Typically, data and addresses are expressed in hexadecimal form, and the count for shift, rotate and string instructions in commonly expressed in decimal form. 2s complement of the number must be used for negative numbers expressed in binary, hexadecimal, or octal form. Example: The repeat count in CX for a string instruction is to be equal to decimal 255. Assume that the instruction that is to load the count has the form MOV CX, XX where XX stands for the count, an immediate operand that is to be loaded into CX. Show how the instruction should be written. Solution: MOV CX, 255D or MOV CX, 225 or MOV CX, 0FFH Example: The count in a move instruction that is to load CX is to -10. Write the instruction and express the immediate operand in binary form. Solution: The binary form of 1010 is 010102. Forming the 2s complement by complementing each bit and adding 1, we get 10101 + 1

10110 Therefore, the instruction is written as MOV CX, 10110B -Operand expressions using the arithmetic, relational, and logical operators Type Arithmet ic Operato r * / MOD SHL SHR + Relation al EQ NE LT GT LE GE Logical NOT AND OR XOR Exampl e A*B A/B A MOD B A SHL B A SHR B A+B A-B A EQ B A NE B A LT B A GT B A LE B A GE B NOT A A AND B A OR B A XOR B Function Multiples A with B and makes the operand equal to the product. Divides A by B and makes the operand equal to the quotient. Divides A by B and assigns the remainder to the operand. Shifts the value in A left by n bits position and assigns this shifted value to the operand. Shifts the value in A right by n bits position and assigns this shifted value to the operand. Adds A to B and makes the operand equal to the sum. Subtract B for A and makes the operand equal to the difference. Compares value of A to that of B. If A equals B, the operand is set to FFFFH and if they are not equal it is set to 0H. Compares value of A to that of B. If A is not equal to B, the operand is set to FFFFH and if they are equal it is set to 0H. Compares value of A to that of B. If A is less than B, the operand is set to FFFFH and if it is equal or greater than it is set to 0H. Compares value of A to that of B. If A is greater than B, the operand is set to FFFH and if it is equal or less than it is set to 0H. Compares value of A to that of B. If A is less than or equal to B, the operand is set to FFFFH and if it is greater than it is set to 0H. Compares value of A to that of B. If A is greater than or equal to B, the operand is set to FFFFH and if it is less than it is set to 0H. Takes the logical NOT of A and makes the value that results equal to the operand. A is ANDed with B and makes the value that results equal to the operand. A is ORed with B and makes the value that results equal to the operand. A is XORed with B and makes the value that results equal to the operand.

Example: Find the value the assembler assigns to the operand for the instruction MOV BH, (A * 4 2) / (B 3) for A = 8 and B = 5 Solution:

The solution is calculated as (8*4-2)/(5-3) = (32-2)/(2) = (30)/(2) = 15 And using hexadecimal notation, we get the instruction MOV, BH, 0FH Example: What value is used for the source operand in the expression MOV AX, A LE (B C) If A = 234, B = 345, and C = 111? Solution: Substituting into the expression, we get 234 LE (345 111) 234 LE 234 since the relational operator is satisfied, the instruction is equivalent to MOV AX, 0FFFFH -Value-returning and attribute operators Type Valuereturning Operator SEG OFFSET TYPE Example SEG A Function Assigns the contents held in the segment register corresponding to the segment in which A resides to the operand. OFFSET A Assigns the offset of the location A in its corresponding segment to the operand. TYPE A Returns to the operand a number representing the type of A; 1 for a byte variable and 2 for a word variable; NEAR of FAR for the label. SIZE A Returns the byte count of variable A to the operand. LENGTH A Returns the number of units (as specified by TYPE) allocated for the variable A to the operand NEAR PTR Overrides the current type of label operand A A and assigns a new pointer type: BYTE, WORD, NEAR, or FAR to A. ES: A Overrides the normal segment for operand A and assigns a new segment to A. JMP SHORT Assigns to operand A an attribute that A indicates that it is within +127 or -128 bytes of the next instruction. This lets the instruction be encoded with the minimum number of bytes THIS BYTE Assigns to operand A a distance or type A attribute: BYTE, NEAR, or FAR, and the corresponding segment attribute. HIGH A Returns to the operand A the high byte of the word of A. LOW A Returns to the operand A the low byte of the word of A.

SIZE LENGTH Attribute PTR DS:, ES:, SS: SHORT

THIS HIGH LOW Assembler Directives

Type Data

ASSUME COMMENT DB or BYTE DD or DWORD DQ or GWORD DT or TBYTE DW or WORD END ENDP ELSE ENDIF IF IFB ENDM EXTTM IRP .CERF .LALL .LFCOND .LIST %OUT

Directives ENDS EQU = (EQUAL SIGN) EVEN EXTRN GROUP INCLUDE LABEL IFDEF IFDIF IFE IFIDN IRPC LOCAL MACRO PAGE .SALL .SFCOND SUBTTL .TFCOND

NAME ORG PROC PUBLIC .RADIX RECOR D SEGME NT STRUC IFNB IFNDEF IF1 IF2 PURGE REPT TITLE .XALL .XCREF .XLIST

Conditional

Macro

Listing

-Data directives Commonly used data directives Directive Meaning EQU Equate = Equal to DB or BYTE Define byte DW or Define word WORD DD or Define double DWORD word Examples: AA EQU 0100H BB EQU AA+5H AA = 0100H BB = AA+5H CC DB 7 EE DB ? MESSAGE DB JASBIR TABLE_A DB 10 DUP(?), 5 DUP(7) TABLE_B DB 0,1,2,3,4,5,6,7,8,9 Function Assign a permanent value to a symbol. Set or redefine the value of a symbol. Define or initialize byte size variables or locations. Define or initialized word size (2 byte) variables or locations. Define or initialize double word size (4 byte) variable or locations.

-Segment-control directives Three kinds of memory segment: Code segment Data segment Stack segment The segment-control directives partitions and assigns a source program to a specific memory segment. The beginning of a segment is identified by the segment (SEGMENT) directive and its end is marked by the end of segment (ENDS) directive. Segment Directiv e SEGME NT ENDS ASSUM E directives Function Defines the beginning of a segment and specifies its kind, at what type of address boundary it is to be stored in memory, and how it is to be positioned with respect to other similar segments in memory. Specifies the end of a segment. Specifies the segment address register for a given segment.

Examples: SEGMEN SEGA T ASSUME MOV . . . ENDS

PARA CS:SE GA AX, BX

PUBLI C

'CODE '

SEGA

Align-type attributes Attribu Function te PARA Segment begins on a 16 byte address boundary in memory (4 LSB of the address are equal to 0). BYTE Segment begins anywhere in memory. WORD Segment begins on a word (2 byte) address boundary in memory (LSB of the address is 0). PAGE Segment begins on a 256 byte address boundary in memory (8 LSB of the address are equal to 0). Combine-type Attribute PUBLIC COMMON AT [expression] STACK MEMORY attributes Function Concatenates segments with the same name. Overlaps form the beginning segments with the same name. Locates the segment at the 16-bit paragraph number evaluated from the expression. The segment is part of the run-time stack segment. Locates the segment at an address above all other segments.

Class attributes Attribu Function te

CODE DATA STACK EXTRA

Specifies the segment. Specifies the segment. Specifies the segment. Specifies the segment.

code date stack extra

-Modular programming directives Directive proc-name PROC [NEAR] proc-name PROC FAR proc-name ENDP PUBLIC Symbol[] EXTRN name:type[] Function Defines the beginning of a near-proc procedure. Defines the beginning of a far-proc procedure. Defines the end of a procedure. The defined symbols can be referenced from other modules. The specified symbols are defined in other modules and are to be used in this module

Two kinds of procedures: Near procedure IP in stack when called. Far procedure IP and CS in stack when called. NEAR is assumed as the default attribute of a procedure. If a procedure can be called from other modules its name must be made public by using the PUBLIC directive. If a procedure in another module is to be called from the current module, its name must be declared external by using the EXTRN directive. Module 1 CSE G1 PUBLIC SEGMEN T . . PROC MOV . . RET ENDP . . . CSE G1 ENDS SUB CSE G2 Module2 EXTRN SEGME NT . . CALL . . . . . CSE G2 ENDS SUB:F AR

SUB

FAR AX, BX

SUB

SUB

-Directives for memory usage control Directive ORG [expression] END [expression] Function Specifies the memory address starting from which the machine code must be placed. Specifies the end of the source program.

Example: ORG 100H ORG 100H ORG $+200H (memory location 10016 to 30016 are skipped and the machine code of the program can start at address 30116) -Directive for program listing control Directive PAGE operand_1 operand_2 TITLE text SBUTTL text Example: PAGE 50 100 PAGE + -Examples of a source program using directives TITLE BLOCK-MOVE PROGRAM PAGE ,132 COMMENT *This program moves a block of specified number of bytes from one place to another place* ;Define constants used in this program H = 16 ;Bytes to be moved BLK1ADDR= 100H ;Source block offset address BLK2ADDR= 120H ;Destination block offset addr DATASEGADDR=2000H ;Data segment start address STACK_SEG SEGMENT STACKSTACK D6 64DUP(?) STACK_SEG CODE_SEG SEGMENT CODE BLOCK PROC FAR ASSUME CS:CODE_SEG,SS:STACK_SEG ;To return to DEBUG program put return address on the stack PUSH DS MOV AX,0 PUSH AX ;Set up the data segment address MOV AX, DATASEGADDR MOV DS,AX ;Set up the source and destination offset addresses Function Selects the number of lines printed per page and the maximum number of character printed per line in the listing. Prints text on the second line of each page of the listing. Prints text on the third line or each page of the listing.

MOV SI, BLK1ADDR MOV DI, BLK2ADDR ;Set up the count of bytes to be moved MOV CX, N ;Copy source block to destination block NXTPT: MOV AH, [SI] ;Move a byte MOV [DI], AH INC SI ;Update pointers INC DI DEC CX ;Update byte counter JHZ NXTPT ;Repeat for next byte RET ;Return to DEBUG program BLOCK ENDP CODE_SEG ENDS END BLOCK ;End of program Creating a Source File with an Editor C:\_ EDIT A:BLCOK_SRC[enter] Type the program line by line . MOV AX, DATASEGADDR MOV DS, AX . etc. Alt-F Menu to save or print the file NEW OPEN . etc. Loaded file not saved. Save it now <Yes><No><Cancel><Hel Assembling and Linking Programs p> [enter ] Assembling a source program Object Module Source Program Assembler Program Source Listing

You might also like