INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE 1 CAP

INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE 1 CAP 221 10/27/2020

Assembly Language Syntax • An assembly language program consists of statements. 2 CAP 221 10/27/2020

RULES Only one statement is written per line Each statement is either an instruction or an assembler directive instruction is translated into machine code assembler directive instructs the assembler to perform some specific task 3 CAP 221 10/27/2020

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 4 CAP 221 10/27/2020

Name Field • This field is used for: instruction label: if present, a label must be followed by a colon (: ) procedure names variable names. 5 CAP 221 10/27/2020

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 6 CAP 221 10/27/2020

Name Field Examples: Legal names Illegal names COUNTER 1 2 ABC @CHARACTER TWO WORDS $500 A 45. 26 SUM_OF_DIGITS YOU&ME . TEST DONE? 7 CAP 221 10/27/2020

Operation Field For an instruction • The opcode describes the operation’s function • Symbolic opcodes are translated into machine language opcode. 8 CAP 221 10/27/2020

Operation Field For an assembler directive • This field consists of a pseudooperation code (pseudo-op) • pseudo-ops tell assembler to do something 9 CAP 221 10/27/2020

Operand Field For an instruction • 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 10 CAP 221 10/27/2020

Numbers Examples: number 1010 B -2134 D ABFFH 0 ABFFH 1 BHH 1 BFFH 1, 23 11 type decimal binary decimal illegal hex illegal CAP 221 10/27/2020

Characters • Characters and character segments must be enclosed in single or double quotes; ‘A' , “hello“. • Assembler translates characters to their ASCII code 12 CAP 221 10/27/2020

Byte variables • Syntax: Name DB initial value Examples: ALPHA 13 DB 4 CAP 221 10/27/2020

Word variables ( 2 bytes) • 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 1234 H low byte WD contains 34 h, high byte contains 12 h 14 CAP 221 10/27/2020

Array Examples B_ARRAY DB 10, 25, 20 If array starts at offset address 0200 h, it will look like this: Symbol B-ARRAY+1 B-ARRAY+2 Address 0200 H+1 0200 H+2 Contents 10 25 20 15 CAP 221 10/27/2020

Array Examples W_ARRAY DW 0 FFFFh, 789 Ah, 0 BCDEh If array starts at offset address 0100 h, it will look like this: Symbol W_ARRAY+2 W_ARRAY+4 16 Address 0100 H 0102 H 0104 H CAP 221 Contents FFFFH 789 AH BCDEH 10/27/2020

Character strings Examples: 1) LETTERS Is equivalent to LETTERS 2) MSG Is equivalent to MSG 17 DB ‘Aa. BCbc‘ DB 41 H, 61 H, 42 H, 43 H, 62 H, 63 H DB ‘ABC‘, 0 AH, 0 DH, ‘$‘ DB 41 H, 42 H, 43 H, 0 AH, 0 DH, 24 H CAP 221 10/27/2020

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 18 CAP 221 10/27/2020

Constant Declaration Examples: 1) LF MOV 2) PMT instead of MSG EQU DL DL 0 AH ; LF can be used in place of 0 Ah LF 0 AH Have the same machine code EQU ‘TYPE YOUR NAME‘ ; DB ‘TYPE YOUR NAME‘ DB PMT We can use MSG 19 CAP 221 10/27/2020

BASIC INSTRUCTIONS MOV and XCHG 20 CAP 221 10/27/2020

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. 21 CAP 221 10/27/2020

Syntax MOV destination , source Example: MOV AX , WORD 1 This reads “ Move WORD 1 to AX “ The contents of register AX are replaced by the contents of the memory location WORD 1. 22 CAP 221 10/27/2020

Mov AX , WORD 1 Before After 0006 0008 AX AX 0008 WORD 1 23 CAP 221 10/27/2020

MOV AH , ‘A’ • This is a move of the 041 h ( the ASCII code of “A” ) into register AH. • The previous value of AH is overwritten ( replaced by new value ) 24 CAP 221 10/27/2020

XCHG instruction • (Exchange) operation is used to exchange the contents of – two registers, or – a register and a memory location 25 CAP 221 10/27/2020

Syntax XCHG destination , source 26 CAP 221 10/27/2020

Example XCHG AH , BL This instruction swaps the contents of AH and BL. 27 CAP 221 10/27/2020

XCHG AH , BL After Before 1 A 00 05 AH AL 00 05 00 BH BL 28 00 1 A BH BL CAP 221 10/27/2020

Example XCHG AX , WORD 1 • This swaps the contents of AX and memory location WORD 1. 29 CAP 221 10/27/2020

Restrictions on MOV Example : ILLEGAL : MOV WORD 1 , WORD 2 LEGAL: MOV AX , WORD 2 MOV WORD 1 , AX 30 CAP 221 10/27/2020

ADD & SUB • Are used to add & subtract the contents of – two registers, – a register & memory location , or – a register and a number – memory location and a number. 31 CAP 221 10/27/2020

Syntax ADD destination , source SUB destination , source 32 CAP 221 10/27/2020

Example ADD WORD 1 , AX This instruction , “ Add AX to WORD 1 “ , causes the contents of AX & memory word WORD 1 to be added, and the sum is stored in WORD 1. AX is unchanged. 33 CAP 221 10/27/2020

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. 34 CAP 221 10/27/2020

Example ADD BL , 5 This is an addition of the number 5 to the contents of register BL. 35 CAP 221 10/27/2020

ILLEGAL ADD BYTE 1 , BYTE 2 Solution : move BYTE 2 to a register before adding MOV AL , BYTE 2 ; AL gets BYTE 2 ADD 36 BYTE 1 , AL ; add it to BYTE 1 CAP 221 10/27/2020

INC ( increment ) Is used to add 1 to the contents of a • Register or • Memory location 37 CAP 221 10/27/2020

DEC ( decrement ) Is used to subtract 1 from the contents of a • Register or • Memory location 38 CAP 221 10/27/2020

Syntax INC destination DEC destination 39 CAP 221 10/27/2020

Example INC WORD 1 adds 1 to the contents of WORD 1 40 CAP 221 10/27/2020

Example DEC BYTE 1 subtracts 1 to the variable BYTE 1 41 CAP 221 10/27/2020

NEG • Is used to negate the contents of the destination. • It does this by replacing the contents by its two’s complement. 42 CAP 221 10/27/2020

Syntax NEG destination The destination may be a register or memory location. 43 CAP 221 10/27/2020

NEG BX 44 Before After 0002 FFFE BX BX CAP 221 10/27/2020

Translation of HLL to Assembly Language Statement B = A Translation MOV AX , A ; moves A into AX MOV B , AX ; and then into B WHY Because direct memory – memory move is illegal we must move the contents of A into a register before moving it to B. 45 CAP 221 10/27/2020

Translation of HLL to Assembly Language Statement A = 5 – A 46 Translation MOV AX , 5 SUB AX , A MOV A , AX CAP 221 ; put 5 in AX ; AX…. 5 – A ; put it in A 10/27/2020

Translation of HLL to Assembly Language Statement A = B – 2 * A 47 Translation MOV AX , B SUB AX , A MOV A , AX CAP 221 ; AX has B – A ; AX has B – 2 * A ; move results to B 10/27/2020

Program Structure • Machine language programs consist of : – – – Codes, Data, and Stack. Each part occupies a memory segment. They are structured as program segments. Each program segment is translated into a memory segment by the assembler. 48 CAP 221 10/27/2020

Memory Models The size of the code & data a program can have is determined by specifying a memory model using the . MODEL directive. 49 CAP 221 10/27/2020

Syntax . MODEL memory_mode 1 LARGE SMALL 50 MEDUIM COMPACT Code in one segment Code in more than one segment Code in one segment Data in more than one segment CAP 221 Code in more than one segment Data in more than one segment No array larger than 64 K bytes. 10/27/2020

• Unless there is a lot of code or data, the appropriate model is SMALL. • . MODEL directive should come before any segment definition. 51 CAP 221 10/27/2020

Data Segment • A program’s data segment contains all the variable definitions. Constant definitions are made here as well, but they may be placed elsewhere in the program since no memory allocation is involved. • We use the . DATA directive followed by variable & constant declarations. • Variable addresses are computed as offsets from the start of this segment 52 CAP 221 10/27/2020

Example . DATA WORD 1 WORD 2 MSG MASK 53 DW 2 DW 5 DB ‘ This is a message ‘ EQU 10010010 B CAP 221 10/27/2020

Stack Segment • Used to set aside storage for the stack • Stack addresses are computed as offsets into this segment • Use: . stack followed by a value that indicates the size of the stack 54 CAP 221 10/27/2020

Declaration Syntax . STACK size An optional number that specifies the stack area size in bytes. 55 CAP 221 10/27/2020

Example. STACK 100 H Sets aside 100 h bytes for the stack area ( a reasonable size for most applications ). If size is omitted , 1 KB is set aside for the stack area. 56 CAP 221 10/27/2020

Code Segment • It contains a program’s instructions. 57 CAP 221 10/27/2020

Syntax. CODE name Optional name for the segment there is no need for a name in a SMALL program Why? ? The assembler will generate an error 58 CAP 221 10/27/2020

Inside the code segment • Instructions are organized as procedures. • The simplest procedure definition is : name PROC ; body of the procedure name ENDP name is the name of the procedure, PROC and ENDP are pseudo-op that delineate the procedure 59 CAP 221 10/27/2020

Example. CODE MAIN PROC ; main procedure body MAIN ENDP ; other procedures go here 60 CAP 221 10/27/2020

Program Structure • A program has always the following general structure: . model . stack . data small 100 h ; Select a memory model ; Define the stack size ; Variable and array declarations ; Declare variables at this level. code main proc ; Write the program main code at this level main endp ; Other Procedures ; Always organize your program into procedures end main ; To mark the end of the source file 61 CAP 221 10/27/2020
- Slides: 61