41
Free Powerpoint Templates Free Powerpoint Templates Assembly Language -I Submitted to : Ms. Indu Chabbra Submitted by : Devika Rangneka Rupam Jaspreet Kaur MCA – I (Morning) Rollno : 9 30 15

Assembly Language -I

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Assembly Language -I

Free Powerpoint TemplatesFree Powerpoint Templates

Assembly Language -I

Submitted to : Ms. Indu Chabbra

Submitted by : Devika Rangnekar Rupam Jaspreet Kaur

MCA – I (Morning)Rollno : 9 30 15

Page 2: Assembly Language -I

Free Powerpoint Templates

Assembly Language

• A Language that allows instruction and storage locations to be represented by letters and symbols instead of binary numbers is called Assembly Language or Symbolic Language.

Page 3: Assembly Language -I

Free Powerpoint Templates

INSTRUCTION SET

Page 4: Assembly Language -I

Free Powerpoint Templates

Instruction Set • DIRECTIVE:

Also called Pseudo- Instruction Tells assembler to perform specific action.

• INSTRUCTION: A set of statements that assembler translates into object code.

• FORMAT for INSTRUCTION [Identifier] Operation [Operand(s)] [;Comment]

Page 5: Assembly Language -I

Free Powerpoint Templates

Format for Instruction• IDENTFIER: Name or Label that you give to a data item or an

instruction.

• OPERATION: reserved symbols that correspond to instructions

• ex: ADD, MOV,SUB,AND, LD, LDR, …

• OPERAND : Data on which the operation is to be performed. Registers -- specified by Rn, where n is the register number Numbers -- indicated by # (decimal) or x (hex) Label -- symbolic name of memory locations

• ex: ADD R1,R1,#3

Page 6: Assembly Language -I

Free Powerpoint Templates

Source and Destination

• Source operands can be:– Register/Memory reference/Immediate value

• Destination operands can be:– Register/Memory reference

• Note: – The Intel CPU does NOT allow both source and

destination operands to be memory references

Page 7: Assembly Language -I

Free Powerpoint Templates

Examples of Instruction Set• DIRECTIVE:

COUNT DB 1;

• INSTRUCTION:– MOV AX, [0x00040222h]– Can also have register names instead of hex

addresses e.g. MOV AX, [EBX]

Contents of memory location 0x00040222

Refers to the contents of register EBX

COUNT will define a byte with initial value 1

Page 8: Assembly Language -I

Free Powerpoint Templates

Types of Instructions

Data Transfer Instruction

Arithmetic Instruction

Logic Instruction

Shift Instruction

Rotate instruction

Page 9: Assembly Language -I

Free Powerpoint Templates

Data Transfer Instruction

PURPOSE:to move data from source to destination

between internal register

b/w internal register and storage location in memory

Page 10: Assembly Language -I

Free Powerpoint Templates

MNUEMONIC MEANING FORMAT OPERATION FLAGS AFFECTED

MOV Move byte/word from source operand to destination operand

MOV D,S (S)(D) None

XCHG Swap data b/w 2 general purpose reg .or a storage location.

XCHG D,S (D) ↔ (S) None

LEA Load Effective Address directly from memory

LEA Reg16,EALEA CX,label

(EA) (Reg16) None

LDS Load Register and Data Segment(DS)

LDS Reg16,Mem32

Mem32Reg16

None

Data Transfer Instructions

Page 11: Assembly Language -I

Free Powerpoint Templates

Arithmetic Instructions

PURPOSE:

• Addition• Subtraction• Multiplication• Division

Results are

stored in flags

Following formats

• Unsigned binary bytes or word

• Signed binary bytes or words

Page 12: Assembly Language -I

Free Powerpoint Templates

Arithmetic InstructionsSymbol Meaning Format Operation Flags

ADD Add byte or word ADD D,S (S) + (D) (D)Carry (CF)

OF,SF,ZF,AF,PF,CF

ADC Add byte or word with carry

ADC D,S (S) + (D) + (CF) (D)Carry (CF)

OF,SF,ZF,AF,PF,CF

INC Increment byte or word by 1

INC D (D) + 1 (D) OF,SF,ZF,AF,PF

SUB Subtract SUB D,S (D) – (S) (D) OF,SF,ZF,AF,PF,CF

SBB Subtract with borrow SBB D,S (D) – (S) – (CF) (D) OF,SF,ZF,AF,PF,CF

DEC Decrement by 1 DEC D (D) – 1 (D) OF,SF,ZF,AF,PF

Page 13: Assembly Language -I

Free Powerpoint Templates

Contd…Symbol Meaning Format Operation Flag

NEG Negation NEG D (O) – (D) (D) OF,SF,ZF,AF,PF,CF

MUL Multiply unsigned bytes

MUL S (AL) . (S8) (AX) (AX) . (S16) (DX),(AX)

OF,SF,ZF,AF,PF,CFUndefined

IMUL Multiplication of signed integers

IMUL S (AL) . (S8) (AX)(AX) . (S16) (DX),(AX)

OF,SF,ZF,AF,PF,CFUndefined

DIV Divide unsigned bytes

DIV S • Q((AX) / (S8) ) (AL) R((AX) / (S8) ) (AH)• Q((DX, AX) / (S16) ) (AX) R((DX, AX) / (S16) ) (DX)

OF,SF,ZF,AF,PF,CFUndefined

IDIV Division of signed integers

IDIV S • Q((AX) / (S8) ) (AX) R((AX) / (S8) ) (AH)• Q((DX, AX) / (S16) ) (AX) R((DX, AX) / (S16) ) (DX)

OF,SF,ZF,AF,PF,CFUndefined

Page 14: Assembly Language -I

Free Powerpoint Templates

Logic Instruction

PURPOSE: to perform logical operations.

Perform operation bit by bit on specified source and results in destination operands.

Page 15: Assembly Language -I

Free Powerpoint Templates

Logic instructionsSymbol Meaning Format Operation

AND Logical AND AND D,S (S) . (D) (D)

OR Logical Inclusive OR OR D,S (S) + (D) (D)

XOR Logical Exclusive OR XOR D,S (S) + (D) (D)

NOT Logical NOT NOT D (D) (D)

EXAMPLE: Let AX=1010 BX=0111 AND BX,AX BX=0010

1010.01110010

Page 16: Assembly Language -I

Free Powerpoint Templates

Shift Instrun. Rotate Instrun.Mnemonic Meaning Format

ROL Rotate left ROL D, count

ROR Rotate Right ROR D, count

Mnemonic Meaning Format

SAL/SHL Shift arithmetic left/Logical left

SAL/SHL D, count

SHR Shift logical Right

SHR D, count

Page 17: Assembly Language -I

Free Powerpoint Templates

EXAMPLES

• SHIFT INSTUCTION• SHL BH,1 ;shift 1 bit left• BH :00001010

• OUTPUT:

• ROTATE INSTRUCTION• ROL BH,1 ; rotate 1 bit left• BH: 10110111

• OUTPUT:

0 0 0 0 1 0 1 0

0 0 0 1 0 1 0 0

1 0 1 1 0 1 1 1

0 1 1 0 1 1 1 1

LOST

INCLUDED

Page 18: Assembly Language -I

Free Powerpoint Templates

OPERATORS

Page 19: Assembly Language -I

Free Powerpoint Templates

ARITHMETIC OPERATORSThese operators include the arithmetic signs and perform arithmetic during the assembly.

SIGN TYPE EXAMPLE EFFECT

+ Positive +FLDA Treats FLDA as positive

- Negation -FLDA Reverses sign of FLDA

+ Addition FLDA+25 Adds 25 to address of FLDA

- Subtraction FLDB-FLDA Calculates difference between two offset address

* Multiplication Value*3 Multiplies value by 3

/ Division Value/3 Divides value by 3

MOD Remainder Value1 MOD value2 Delivers remainder for value1/value2

Page 20: Assembly Language -I

Free Powerpoint Templates

The LOGICAL operators process the bits in an expression.

LOGICAL OPERATORS

OPERATOR USED AS EFFECT

AND EXPR1 AND EXPR2 ANDs the bits

OR EXPR1 OR EXPR2 Ors the bits

XOR EXPR1 XOR EXPR2 Exclusive Ors the bits

NOT EXPR1 NOT EXPR2 Reverses the bits

EXAMPLE: MOV CL,00111100B AND 01010101B ; CL= 00010100B MOV DL,NOT 01010101B ; CL=10101010B

Page 21: Assembly Language -I

Free Powerpoint Templates

High/Highword & Low/Lowword Operators

HIGH and HIGHWORD OPERATORSHIGH operator returns the high(leftmost) byte of an expression.HIGHWORD Operator returns the high word of an expression.Example : EQUVAL EQU 1234H Mov CL,HIGH EQUVAL EFFECT : Load 12H in CL

LOW and LOWWORD OPERATORSLOW operator returns the low(rightmost) byte of an expression.LOWWORD operator returns the low word of an expression.Example : EQUVAL EQU 1234H MOV CL,LOW EQUVAL EFFECT : Load 34H in CL

Page 22: Assembly Language -I

Free Powerpoint Templates

Page 23: Assembly Language -I

Free Powerpoint Templates

SHL and SHR Operators

The operators SHL(shift left) and SHR (shift right) shift an expression during an assembly.

FORMAT: expression SHL/SHR count EXAMPLE: MOV BL,01011101B SHR 3 ; Load 00001011B

Page 24: Assembly Language -I

Free Powerpoint Templates

OFFSET OPERATOR

The OFFSET operator returns the offset address of a variable or label.

OFFSET : the distance in bytes from the segment address to another location within the segment.

CODED AS : offset variable/label EXAMPLE: MOV DX,OFFSET PART_TBL ; return offset address of part_tbl

Page 25: Assembly Language -I

Free Powerpoint Templates

PTR OPERATOR Provides flexibility to access part of a variable. Can also be used to combine elements of smaller type. Used to override the default type of variable. SYNTAX: type ptr expression Example :.dataDval dword 12345678h |<------ DVAL ------- | -----------ARRAY----- Array byte 00h,10h,20h,30h.codeMov al,dval ;errorMov al, byte ptr dval ;al=78hMov ax, dval ;errorMov ax, word ptr dval ;ax=5678hMov eax ,array ;errorMov eax, dword ptr array ;eax=30201000h

78 56 34 12 00 10 20 30 40

Page 26: Assembly Language -I

Free Powerpoint Templates

DUP OPERATOR USED: to generate multiple bytes or words with known as

well as un-initialized values. PURPOSE: to tell the assembler to duplicate or repeat the

data definition directive a specific number of times.

EXAMPLE: var1 BYTE 20 DUP(0) ; 20 bytes of zero var2 BYTE 20 DUP(?) ; 20 bytes uninitialized var3 BYTE 10, 3DUP(0) , 20 ; 5 bytes 10,0,0,0,20 The (?) with the dup means that storage allocated by the

directive is uninitialized or undefined. The assembler allocates the space but does not write in it.

Use ? for buffer areas or variables your program will initialize at run time.

Page 27: Assembly Language -I

Free Powerpoint Templates

SEG OPERATOR

The seg operator does two things : 1) Returns the address of the segment in which a specified

variable or label is placed.2) It converts the type of the specified expression from

address to constant. Coded As: seg variable/label Example : MOV AX,SEG WORDA ; Get address of data seg MOV AX,SEG A10BEGIN ; Get address of code seg

Page 28: Assembly Language -I

Free Powerpoint Templates

INDEX OPERATOR Add a constant to a register to generate an offset. Allows to reference a members in an array. Uses square brackets[ ] and acts like a plus(+) sign. Two equivalent forms:

– constant[ reg ]– [constant + reg ]

EXAMPLE: part_tbl DB 25 DUP(?) ;Defined table MOV CL<PART_TBL[4] ;accessing 5th entry MOV DX,[BX] ; offset address in base reg DS:BX MOV[BX+SI+4],AX ;base+index+constant

Page 29: Assembly Language -I

Free Powerpoint Templates

The LENGTH operator returns the number of entries defined by a DUP operator.

EXAMPLE : PART_TBL DW 10 DUP(?) MOV DX,LENGTH PART_TBL If the referenced operand does not contain a

DUP entry, the operator returns the value 01.

LENGTH OPERATOR

Page 30: Assembly Language -I

Free Powerpoint Templates

TYPE OPERATOR The TYPE operator returns the number of bytes,

according to the definition of the referenced variable. CODED AS: TYPE variable/labelDEFINITION NO. OF BYTES FOR NUMERIC VARIABLE

DB/BYTE 1

DW/WORD 2

DD/DWORD 4

DF/FWORD 8

DT/TWORD 10

STRUC/STRUCT No> of bytes defined by structure

NEAR label FFFFH

FAR label FFFFH

Page 31: Assembly Language -I

Free Powerpoint Templates

SIZE OPERATOR

Returns the number of bytes taken up by a structure Basically, LENGTH * TYPE Coded As: SIZE variable Example : BYTEA DB? ;Define one byte PART_TBL DW 10 DUP(?) ;Define 10 words MOV AX,TYPE BYTEA ;AX=0001H MOV AX,TYPE PART_TBL ;AX=0002H MOV CX,LENGTH PART_TBL ;CX=000AH(10) MOV DX,SIZE PART_TBL ;DX=0014H(20)

Page 32: Assembly Language -I

Free Powerpoint Templates

EXECUTION OF ASSEMBLY PROGRAM

Page 33: Assembly Language -I

Free Powerpoint Templates

Execution of an Assembly Lang. Program

Assembling

Linking

Execution

Includes following steps:

Page 34: Assembly Language -I

Free Powerpoint Templates

ASSEMBLING, LINKING AND EXECUTING

SOURCE CODE: The symbolic

instructions that we code in assembly

language.

OBJECT CODE:We use an assembler program to translate the source program into machine code.

EXECUTABLE MODULE:

Finally, using a linker, execution of object

code.

Page 35: Assembly Language -I

Free Powerpoint Templates

EDIT

PROG.ASM

ASSEMBLE

PROG.OBJ

LINK

PROG.EXE

EXECUTE

EDITOR

ASSEMBLER

LINKER

Page 36: Assembly Language -I

Free Powerpoint Templates

EDITING A PROGRAM

Page 37: Assembly Language -I

Free Powerpoint Templates

ASSEMBLING SOURCE PROGRAMOptional output files from the assembly step are OBJECT(.OBJ), LISTING(.LST) and CROSS REFERNCE(.CRF or .SBR).

Page 38: Assembly Language -I

Free Powerpoint Templates

LINKING AN OBJECT PROGRAM

When our program is free from

errors next step is to link

the object module

produced by the assembler

and that contains only

machine code.

Linker combines, if requested, more than

one separately assembled

module into one

executable program.

Generates an .EXE

module and initializes it with special

instructions to facilitate its subsequent loading for execution.

Output files from this step

are EXECUTABLE(.

EXE), MAP(.MAP)

and LIBRARY(.LIB)

Page 39: Assembly Language -I

Free Powerpoint Templates

EXECUTING A PROGRAM

• Having assembled and linked a program, we can now execute it .

• If the .EXE file is in the default drive, you could ask the loader to read it into memory for execution by typing

• C:\TASM\BIN>PROG.EXE

Page 40: Assembly Language -I

Free Powerpoint Templates

STEPS TO CREATE AND RUN A PROGRAM

Write the

source code in

a notepad file.

Save it in

tasm\bin with

.asm extensi

on.

Now , go to DOS

prompt.

Set the path to tasm\bin.

Write tlink

filename.asm to link

the file.

Type filename to

run the progra

m.

Give the

required

input.

Page 41: Assembly Language -I

Free Powerpoint Templates