Assmbler 1
Assmbler 1
Assmbler 1
Program Statements
Program consist of statement, one per line. Each statement is either an instruction, which the instruction, assembler translate into machine code, or assembler directive, directive, which instructs the assembler to perform some spesific task, such as allocating memory space for a variable or creating a procedure. Both instructions and directives have up to four fields:
Program Statements
An example of an instruction is START: MOV CX,5 ; initialize counter
Program Statements
name operation operand(s) comment
A Name field identifies a label, variable, or symbol. It may contain any of the following character : A,B..Z ; a,b.z ; 0,1.9 ; ? ;
A Keyword, or reserved word, always _ (underline) ; @ ; $ ; . (period) have some predefined meaning to the Only the first assembler. 31 characters are recognized There is no distinction between uppercase and lower It may be an instruction (MOV, ADD), case letters. The first be an may not be a digit orit may characterassembler directive. If it is used, the period ( . ) may be used only as the (PROC, TITLE, END) first character.
A programmer-chosen name may not be the same as programmer-
Program Statements
name operation operand(s) comment
Operation field is a predefined or reserved word mnemonic - symbolic operation code. The assembler translates a symbolic opcode into a machine language opcode. Opcode symbols often discribe the operations function; for example, MOV, ADD, SUB assemler directive - pseudo-operation code. pseudoIn an assembler directive, the operation field (pseudocontains a pseudo-operation code (pseudo-op) pseudoPseudoPseudo-op are not translated into machine code; for example the PROC pseudo-op is used pseudoto create a procedure
Program Statements
name operation operand(s) comment
An operand field specifies the data that are to be acted on by the operation. An instruction may have zero, one, or two operands. For example: NOP No operands; does nothing
INC AX
one operand; adds 1 to the contents of AX two operands; adds 2 to the contents of memory WORD1
ADD WORD1,2
word
Program Statements
name operation operand(s) comment
The comment field is used by the programmer to say something about what the statement does. A semicolon marks the beginning of this field, and the assembler ignores anything typed after semicolon. Comments are optional, but because assembly language is low level, it is almost impossible to understand an assembly language program without comments.
Defining Data
Numeric data values
100 - decimal 100B - binary 100H - hexadecimal '100' - ASCII "100" - ASCII
ANum refers to a byte storage location, initialized to FCh The next word has no associated name ONE and UNO refer to the same word X is an unitialized doubleword
Multiple definitions can be abbreviated Example: message DB B DB y DB e DB 0DH DB 0AH can be written as message DB B,y,e,0DH,0AH More compactly as message DB Bye,0DH,0AH
Arrays
Any consecutive storage locations of the same size can be called an array
X DW 40CH,10B,-13,0 40CH,10B,Y DB 'This is an array' Z DD -109236, FFFFFFFFH, -1, 100B Components of X are at X, X+2, X+4, X+6 Components of Y are at Y, Y+1, , Y+15 Components of Z are at Z, Z+4, Z+8, Z+12
DUP
Allows a sequence of storage locations to be defined or reserved Only used as an operand of a define directive
DB 40 DUP (?) ; 40 words, uninitialized DW 10h DUP (0) ; 16 words, initialized as 0 Table1 DW 10 DUP (?) ; 10 words, uninitialized message DB 3 DUP (Baby) ; 12 bytes, initialized ; as BabyBabyBaby Name1 DB 30 DUP (?) ; 30 bytes, each ; initialized to ?
DUP
The DUP directive may also be nested
Example stars DB 4 DUP(3 DUP (*),2 DUP (?),5 DUP (!)) Reserves 40-bytes space and initializes it as ***??!!!!!***??!!!!!***??!!!!!***??!!!!! matrix DW 10 DUP (5 DUP (0)) defines a 10X5 matrix and initializes its elements to zero. This declaration can also be done by matrix DW 50 DUP (0)
Word Storage
Word, doubleword, and quadword data are stored in reverse byte order (in memory)
Directive DW 256 DD 1234567H DQ 10 X DW 35DAh Bytes in Storage 00 01 67 45 23 01 0A 00 00 00 00 00 00 00 DA 35
Word Storage
Named Constants
Symbolic names associated with storage locations represent addresses Named constants are symbols created to represent specific values determined by an expression Named constants can be numeric or string Some named constants can be redefined No storage is allocated for these values
EQU Directive
name EQU expression
expression can be string or numeric Use < and > to specify a string EQU these symbols cannot be redefined later in the program sample EQU 7Fh aString EQU <1.234> message EQU <This is a message>
reg can be any nonnonsegment register except IP cannot be the target register MOV's between a segment register and memory or a 16-bit 16register are possible
When a variable is created with a define directive, it is assigned a default size attribute (byte, word, etc) You can assign a size attribute using LABEL
LoByte LABEL BYTE aWord DW 97F2h
eXCHanGe
XCHG target, source
reg, reg reg, mem mem, reg
Arithmetic Instructions
ADD dest, source SUB dest, source INC dest DEC dest NEG dest Operands must be of the same size source can be a general register, memory location, or constant dest can be a register or memory location
except operands cannot both be memory
Stack Segment
used to set aside storage for the stack Stack addresses are computed as offsets into this segment
Code Segment
contains executable instructions
Segment directives
.data .code .stack size
Memory Models
.Model memory_model
tiny: code+data <= 64K (.com program) small: code<=64K, data<=64K, one of each medium: data<=64K, one data segment compact: code<=64K, one code segment large: multiple code and data segments huge: allows individual arrays to exceed 64K flat: no segments, 32-bit addresses, protected 32mode only (80386 and higher)
Program Skeleton
.model small .stack 100H .data ;declarations .code main proc ;code main endp ;other procs end main Select a memory model Define the stack size Declare variables Write code
organize into procedures
A 100 ; BEGIN ASSEMBLY AT LOCATION 100H MOV AX,5 ; FIRST PROGRAM STATEMENT MOV AX,10 ADD AX,20 MOV [120],AX ; SUM IS AT LOCATION 0120H INT 20 ; END PROGRAM ; PRESS ENTER TO END ASSEMBLY R ; DISPLAY THE REGISTER T ; TRACE ONE INSTRUCTION T T G ; EXECUTE THE REST OF THE PROGRAM Q ; QUIT DEBUG AND RETURN TO DOS