820 likes | 963 Views
INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE. Assembly Language Syntax. An assembly language program consists of statements. The syntax of an assembly language program statement obeys the following rules:. RULES. Only one statement is written per line
E N D
Assembly Language Syntax • An assembly language program consists of statements. • The syntax of an assembly language program statement obeys the following rules: CAP221
RULES • Only one statement is written per line • Each statement is either aninstructionor anassembler directive • instruction is translated into machine code • assembler directiveinstructs the assembler to perform some specific task CAP221
Program Statement • The general format for an assembly language program statement is as follows: name operation operand’(s) comment Examples: START: MOV CX,5 ; initialize counter MAIN PROC CAP221
Name Field • This field is used for: • instruction label: if present, a label must be followed by a colon (:) • procedure names • variable names. CAP221
Name Field • Assembler translates names into memory addresses. • Names can be from 1 to 31 characters long: (letters, digits, and special characters: ?, ., _, $, @, %) • Embedded blanks are not allowed, names may not begin with a digit, period (if used) must be the first character CAP221
Name Field Examples: Legal namesIllegal names COUNTER1 2ABC @CHARACTER TWO WORDS $500 A45.26 SUM_OF_DIGITS YOU&ME .TEST DONE? CAP221
Operation Field For an instruction • This field consists of a symbolic operation code, known as opcode • The opcode describes the operation’s function • Symbolic opcodes are translated into machine language opcode. CAP221
Operation Field For an assembler directive • This field consists of a pseudo-operation code (pseudo-op) • pseudo-ops tell assembly to do something CAP221
Operand Field For an instruction • This field specifies data to be acted on. It may have one, two or no operands at all. • Examples of instructions with different operand fields NOP ; Instruction with no operand field INC AX ; Instruction with one operand field ADD AX, 2 ; Instruction with two operand field If 2 operands: the first is destination, the second is the source operand CAP221
Operand Field For an assembler directive • This field contains more information about the directive CAP221
Comment Field • A semicolon marks the beginning of a comment • A semicolon in the beginning of a line makes it all a comment line • Good programming practice dictates the use of a comment on almost every line. CAP221
Key rules for the use of comments • Do not say something that is obvious • Put instruction in context of program CAP221
Comment Field Examples of good and bad Comments MOV CX , 0 ; Move 0 to CX (This is not a good comment.) MOV CX , 0 ; CX counts terms, initially set to 0 (This is a good comment.) CAP221
Numbers • Binary number is written as a bit string followed by the letter `b`. • decimal number is written as a string of decimal digits followed by the letter `d`. • Hex number is written as a string of hex digits followed by the letter `h`. • Hex number must begin with a decimal digit • Numbers may have an optional sign CAP221
Numbers Examples: numbertype 1010 decimal 1010B binary -2134D decimal ABFFH illegal 0ABFFH hex 1BHH illegal 1BFFH hex 1,23 illegal CAP221
Characters • Characters and character segments must be enclosed in single or double quotes; ‘A' , “hello“. • Assembler translates characters to their ASCII code CAP221
Variables Declaring Integer Variables: • An integer is a whole number, such as 4 or 4444. Integers have no fractional part. Integer variables can be initialized in several ways with the data allocation directives. CAP221
Variables Allocating Memory for Integer Variables: • When an integer variable is declared, the assembler allocates memory space for the variable. The variable name becomes a reference to the memory space allocated to that variable. CAP221
Syntax name directive initializer initial value CAP221
Variables Pseudo-optypesizerange • DB unsigned 1 byte 0 to 255. signed 1 byte -128 to +127. • DW unsigned 2 bytes 0 to 65,535 (64K). signed 2 bytes -32,768 to +32,767. • DD unsigned 4 bytes 0 to 4,294,967,295 (4 Mbytes). signed 4 bytes -2,147,483,648 to +2,147,483,647. • DQ 8-byte integer 4 consecutive words • DT 10-byte integer 10 consecutive bytes CAP221
Byte variables • Syntax: Name DB initial value Examples: ALPHA DB 4 BYT DB ? CAP221
Word variables • Syntax: Name DW initial value Example: WRD DW -2 • The assembler stores integers with the least significant byte in the lowest address of the memory area allocated to the integer Example: WD DW 1234H low byteWD contains 34h, high byte contains 12h CAP221
Array Declaration • An array is a sequential collection of variables, all of the same size and type • Array elements occupy contiguous memory locations. • The program references each element relative to the start of the array. • An array is declared by giving it a name, a type, and a series of initializing values or placeholders (?). CAP221
Array Examples B_ARRAY DB 10, 25, 20 If array starts at offset address 0200h, it will look like this: SymbolAddressContents B-ARRAY 0200H 10 B-ARRAY+1 0200H+1 25 B-ARRAY+2 0200H+2 20 CAP221
Array Examples W_ARRAY DW 0FFFFh, 789Ah, 0BCDEh If array starts at offset address 0100h, it will look like this: SymbolAddressContents W_ARRAY 0100H FFFFH W_ARRAY+2 0102H 789AH W_ARRAY+4 0104H BCDEH CAP221
Character strings • An array of characters can be initialized by a string of characters. • Inside a string, the assembler differentiates between upper and lower cases (different ASCII codes). • It is possible to combine characters and numbers in one definition CAP221
Character strings Examples: 1) LETTERS DB ‘AaBCbc‘ Is equivalent to LETTERS DB 41H,61H,42H,43H,62H,63H 2) MSG DB ‘ABC‘,0AH,0DH,‘$‘ Is equivalent to MSG DB 41H,42H,43H,0AH,0DH,24H CAP221
Constant Declaration • In an assembly language program, constants are defined through the use of the EQU directive. • Syntax: Name EQU constant • The EQU directive is used to assign a name to a constant. • Use of constant names makes an assembly language easier to understand. • No memory is allocated for a constant. • The symbol on the right of EQU cab also be a string CAP221
Constant Declaration Examples: 1) LF EQU 0AH ; LF can be used in place of 0Ah MOV DL LF MOV DL 0AH 2) PMT EQU ‘TYPE YOUR NAME‘ ; instead of MSG DB ‘TYPE YOUR NAME‘ We can use MSG DB PMT Have the same machine code CAP221
BASIC INSTRUCTIONS MOV and XCHG CAP221
MOV instruction • Is used to transfer data : • between registers, • between a register & a memory location. Or • To move a number directly into a register or memory location. CAP221
Syntax MOV destination , source Example: MOV AX , WORD1 This reads “ Move WORD1 to AX “ The contents of register AX are replaced by the contents of the memory location WORD1. CAP221
Mov AX , WORD1 After Before 0006 0008 AX AX 0008 0008 WORD1 WORD1 CAP221
MOV AX , BX • AX gets what was previously in BX , BX is unchanged. CAP221
MOV AH , ‘A’ • This is a move of the 041h ( the ASCII code of “A” ) into register AH. • The previous value of AH is overwritten ( replaced by new value ) CAP221
XCHG instruction • (Exchange) operation is used to exchange the contents of • two registers, or • a register and a memory location CAP221
Syntax XCHG destination , source CAP221
Example XCHG AH , BL This instruction swaps the contents of AH and BL. CAP221
XCHG AH , BL After Before 1A 00 05 00 AH AL AH AL 00 05 00 1A BH BL BH BL CAP221
Example XCHG AX , WORD1 • This swaps the contents of AX and memory location WORD1. CAP221
Restrictions on MOV & XCHG MOV Destination Operand CAP221
Restrictions on MOV & XCHG XCHG Destination Operand CAP221
Restrictions on MOV & XCHG Example : ILLEGAL : MOV WORD1 , WORD2 LEGAL: MOV AX , WORD2 MOV WORD1 , AX CAP221
ADD & SUB • Are used to add & subtract the contents of • two registers, • a register & memory location , or • add ( subtract ) a number to ( from ) a register or a memory location. CAP221
Syntax ADD destination , source SUB destination , source CAP221
Example ADD WORD1 , AX This instruction , “ Add AX to WORD1 “ , causes the contents of AX & memory word WORD1 to be added, and the sum is stored in WORD1. AX is unchanged. CAP221
ADD WORD1 , AX Before After 01BC 01BC AX AX 06DF 0523 WORD1 WORD1 CAP221
Example SUB AX , DX This instruction , “ Subtract DX from AX “ , the value of DX is subtracted from the value of AX , with the difference being stored in AX. DX is unchanged. CAP221
SUB AX , DX Before After 0000 FFFF AX AX 0001 0001 DX DX CAP221