Top Banner
Review of Assembly language
54

Review of Assembly language

Jan 18, 2016

Download

Documents

Lois

Review of Assembly language. Recalling main concepts. Recalling main concepts. Segment: special areas defined to contain CODE, DATA and STACK Paragraph boundary: location evenly divisible by 16 or 10H. Recalling main concepts. Stack Segment. SS. Data Segment. DS. CS. Code Segment. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Review of Assembly language

Review of Assembly language

Page 2: Review of Assembly language

Recalling main concepts

Page 3: Review of Assembly language

Recalling main concepts

Segment: special areas defined to contain CODE, DATA and STACK

Paragraph boundary: location evenly divisible by 16 or 10H

Page 4: Review of Assembly language

Recalling main concepts

Stack Segment

Data Segment

Code Segment

SSDSCS

Segment Registers

Page 5: Review of Assembly language

Example program; Add two numbers and store the results into the third variablepage 60,132TITLE A04ASM1 (EXE) Move and add operations; ---------------------------------------------STACK SEGMENT PARA STACK 'Stack'

DW 32 DUP(0)STACK ENDS; ----------------------------------------------DATASEG SEGMENT PARA 'Data'

FLDD DW 215FLDE DW 125FLDF DW ?

DATASEG ENDS; -----------------------------------------------CODESEG SEGMENT PARA 'Code'MAIN PROC FAR

ASSUME SS:STACK,DS:DATASEG,CS:CODESEGMOV AX,DATASEG ;Set address of data MOV DS,AX ; segment in DS MOV AX,FLDD ;Move 0215 to AXADD AX,FLDE ;Add 0125 to AXMOV FLDF,AX ;Store sum in FLDFMOV AX,4C00H ;End processingINT 21H

MAIN ENDP ;End of procedureCODESEG ENDS ;End of segment

END MAIN ;End of program

Page 6: Review of Assembly language

; Add two numbers and store the results into the third variablepage 60,132TITLE A04ASM1 (EXE) Move and add operations

; ---------------------------------------------STACK SEGMENT PARA STACK 'Stack'

DW 32 DUP(0)STACK ENDS

; ----------------------------------------------DATASEG SEGMENT PARA 'Data'

FLDD DW 215FLDE DW 125FLDF DW ?

DATASEG ENDS

; -----------------------------------------------CODESEG SEGMENT PARA 'Code'MAIN PROC FAR

ASSUME SS:STACK,DS:DATASEG,CS:CODESEGMOV AX,DATASEG ;Set address of data MOV DS,AX ;Segment in DS MOV AX,FLDD ;Move 0215 to AXADD AX,FLDE ;Add 0125 to AXMOV FLDF,AX ;Store sum in FLDFMOV AX,4C00H ;End processingINT 21H

MAIN ENDP ;End of procedureCODESEG ENDS ;End of segmentEND MAIN ;End of program

Comments

; <your comments>

COMMENTS

Page 7: Review of Assembly language

; Add two numbers and store the results into the third variablepage 60,132TITLE A04ASM1 (EXE) Move and add operations

; ---------------------------------------------STACK SEGMENT PARA STACK 'Stack'

DW 32 DUP(0)STACK ENDS

; ----------------------------------------------DATASEG SEGMENT PARA 'Data'

FLDD DW 215FLDE DW 125FLDF DW ?

DATASEG ENDS

; -----------------------------------------------CODESEG SEGMENT PARA 'Code'

MAIN PROC FARASSUME SS:STACK,DS:DATASEG,CS:CODESEGMOV AX,DATASEG ;Set address of data MOV DS,AX ;Segment in DS MOV AX,FLDD ;Move 0215 to AXADD AX,FLDE ;Add 0125 to AXMOV FLDF,AX ;Store sum in FLDFMOV AX,4C00H ;End processingINT 21H

MAIN ENDP ;End of procedureCODESEG ENDS ;End of segmentEND MAIN ;End of program

IDENTIFIERS

Page 8: Review of Assembly language

Identifiers

Identifier is a name applied to an item in a program to reference

• Name (e.g: FLDD DW 215)

• Label (e.g: MAIN PROC FAR) Identifiers must not a reserved word and only contain:

• Alphabetic letters (A-Z,a-z)

• Digits (0-9)

• ?,_,$,@,dot (.) (but not for the first character) Maximum length is 247

IDENTIFIERS

Page 9: Review of Assembly language

Instructions: ADD, MOV Directives: .TITLE, .MODEL Operators: FAR, SIZE Pre-defined symbols: @Data, @Model Register: AX,BX

RESERVED WORDS

Page 10: Review of Assembly language

STATEMENT

Instructions: are translated to object code

MOV, ADD, LEA.. Directives: tell the assembler to perform

a specific action.

[identifier] operation [operand(s)] [;comments]

Page 11: Review of Assembly language

; Add two numbers and store the results into the third variablepage 60,132TITLE A04ASM1 (EXE) Move and add operations; ---------------------------------------------STACK SEGMENT PARA STACK 'Stack'

DW 32 DUP(0)STACK ENDS; ----------------------------------------------DATASEG SEGMENT PARA 'Data'

FLDD DW 215FLDE DW 125FLDF DW ?

DATASEG ENDS; -----------------------------------------------CODESEG SEGMENT PARA 'Code'MAIN PROC FAR

ASSUME SS:STACK,DS:DATASEG,CS:CODESEGMOV AX,DATASEG ;Set address of data MOV DS,AX ;Segment in DS MOV AX,FLDD ;Move 0215 to AXADD AX,FLDE ;Add 0125 to AXMOV FLDF,AX ;Store sum in FLDFMOV AX,4C00H ;End processingINT 21H

MAIN ENDP ;End of procedureCODESEG ENDS ;End of segmentEND MAIN ;End of program

STATEMENTS

Page 12: Review of Assembly language

Directives

Control the way a source program assembles and lists

Generate no machine code (unlike instructions which generate object code)

Page 13: Review of Assembly language

; Add two numbers and store the results into the third variablepage 60,132TITLE A04ASM1 (EXE) Move and add operations; ---------------------------------------------

STACK SEGMENT PARA STACK 'Stack'DW 32 DUP(0)

STACK ENDS; ----------------------------------------------DATASEG SEGMENT PARA 'Data'

FLDD DW 215FLDE DW 125FLDF DW ?

DATASEG ENDS; -----------------------------------------------CODESEG SEGMENT PARA 'Code'MAIN PROC FAR

ASSUME SS:STACK,DS:DATASEG,CS:CODESEGMOV AX,DATASEG ;Set address of data MOV FLDF,AX ;Store sum in FLDFMOV AX,4C00H ;End processingINT 21H

MAIN ENDP ;End of procedureCODESEG ENDS ;End of segmentEND MAIN ;End of program

Segment directive

Page 14: Review of Assembly language

Segment directive

Name Operation OperandSegment-name SEGMENT [align][combine] [`class’]

Segment-name ENDS

Example:

STACK SEGMENT PARA STACK 'Stack‘

STACK ENDS

Page 15: Review of Assembly language

; Add two numbers and store the results into the third variable

page 60,132TITLE A04ASM1 (EXE) Move and add operations; ---------------------------------------------STACK SEGMENT PARA STACK 'Stack'

DW 32 DUP(0)STACK ENDS; ----------------------------------------------DATASEG SEGMENT PARA 'Data'

FLDD DW 215FLDE DW 125FLDF DW ?

DATASEG ENDS; -----------------------------------------------CODESEG SEGMENT PARA 'Code'

MAIN PROC FARASSUME SS:STACK,DS:DATASEG,CS:CODESEGMOV AX,DATASEG ;Set address of data MOV DS,AX ;Segment in DS MOV AX,FLDD ;Move 0215 to AXMOV FLDF,AX ;Store sum in FLDFMOV AX,4C00H ;End processingINT 21H

MAIN ENDP ;End of procedureCODESEG ENDS ;End of segmentEND MAIN ;End of program

PROC directive

Page 16: Review of Assembly language

PROC directive

Format:Procedure-name PROC Operand

Comment

Procedure-name ENDP

Operand: relates to program execution (FAR)

Page 17: Review of Assembly language

; Add two numbers and store the results into the third variablepage 60,132TITLE A04ASM1 (EXE) Move and add operations; ---------------------------------------------STACK SEGMENT PARA STACK 'Stack'

DW 32 DUP(0)STACK ENDS; ----------------------------------------------DATASEG SEGMENT PARA 'Data'

FLDD DW 215FLDE DW 125FLDF DW ?

DATASEG ENDS; -----------------------------------------------CODESEG SEGMENT PARA 'Code'MAIN PROC FAR

ASSUME SS:STACK,DS:DATASEG,CS:CODESEGMOV AX,DATASEG ;Set address of data MOV DS,AX ;Segment in DS MOV AX,FLDD ;Move 0215 to AXMOV FLDF,AX ;Store sum in FLDFMOV AX,4C00H ;End processingINT 21H

MAIN ENDP ;End of procedureCODESEG ENDS ;End of segmentEND MAIN ;End of program

ASSUME directive

Page 18: Review of Assembly language

ASSUME directive

Tells the assembler the purpose of each segment in the program

Example:

ASSUME SS:STACK,DS:DATASEG,CS:CODESEG

Page 19: Review of Assembly language

Simplified Segment Directives

Model memory-model# Code segment #Data segment

Small: 1, <=64K 1,<=64KMedium: any number,size 1, <=64KCompact: 1, <=64K any

number,size

Large: any number,size any number,size

Huge: any number,size any number,size

Page 20: Review of Assembly language

Simplified Segment Directives

STACK [size] (default: 1K) DATA (default size is 1K) CODE (default size is 1K) .EXIT directive

Page 21: Review of Assembly language

EQUATE directives

Equal-Sign directiveCOEFFICIENT= 100

EQU directive

COEFFICIENT EQU 100

Page 22: Review of Assembly language

Data type Format for data definition

[name] Dn expression

Name: identifierDn: Directives and can be:

DB: byte DF:farwordDW: word DQ:quadwordDD: doubleword DT:tenbytes

Expression:can be unnitialized: ?can be assigned a constant: such as 25, 21.

Example:• DATAZ DB 21,22..• DW 10 DUP(?)

Page 23: Review of Assembly language

Data type

Constant:• String: is defined within ‘ ‘ or “ “

MESSAGE DB “I am learning assembly language”

• Numeric:

• Is stored in reverse sequence

• Binary: 01B

• Decimal: 9D( D is optional)

• Hexadecimal: 1FH

• Real: 12R

Page 24: Review of Assembly language

Directives for defining Data

Byte: DBWord: DW

Doubleword: DD

Farword: DF

Quadword: DQ

Tenbytes: DT

Page 25: Review of Assembly language

Some instructions on arithmetic calculation

ADD:ADD register register/memory/immediateExample: ADD AX,FLDE

SubtractSBB register register/memory/immediateExample: SUB AX, 100

MultiplicationIMUL registerExample: IMUL CX

DivisionDIV registerExample DIV CX

Page 26: Review of Assembly language

Data transfer instructions MOV instruction

• Transfers data referenced by the address of the second operand to the address of the first operand

• Destination has to have the same length as source[label:] MOV register/memory register/memory/immediate

Example:

MOV F, AX ; // Move content of AX to the variable F

MOV CX, D ;// Move value of D to CX

MOV ES, AX

MOV AX, 215

Page 27: Review of Assembly language

Note Note

• MOV instruction can’t:

• set the value of the CS and IP registers.

• copy value of one segment register to another segment register (should copy to general register first).

MOV ES, DS

•copy immediate value to segment register (should copy to general register first).

MOV DS, 100

• MOV instruction can’t:

• set the value of the CS and IP registers.

• copy value of one segment register to another segment register (should copy to general register first).

MOV ES, DS

•copy immediate value to segment register (should copy to general register first).

MOV DS, 100

Page 28: Review of Assembly language

MOVSB:

Copy byte at DS:[SI] to ES:[DI]. Update SI and DI.

Algorithm:

ES:[DI] = DS:[SI]

if DF = 0 then SI = SI + 1 DI = DI + 1

else SI = SI - 1 DI = DI - 1

DF: direction flag from the flag register

MOVSB and MOVSW

Page 29: Review of Assembly language

MOVSW:

Copy word at DS:[SI] to ES:[DI]. Update SI and DI.

ES:[DI] = DS:[SI]

if DF = 0 then SI = SI + 2 DI = DI + 2

else SI = SI - 2 DI = DI - 2

DF: direction flag from the flag register

MOVSB and MOVSW

Page 30: Review of Assembly language

XCHG swap the two data items

[label:] XCHG register/memory, register/memory

Example:

MOV AL, 5

MOV AH, 2

XCHG AL, AH ; AL = 2, AH = 5

XCHG AL, AH ; AL = 5, AH = 2

XCHG instruction

Page 31: Review of Assembly language

Load Effective Address. REG = address of memory (offset)

[label:] LEA register/memory

Example:

LEA AX, m ;load offset address of m to AX

LEA instruction

Page 32: Review of Assembly language

Arithmetic instructions INC and DEC instruction

• Increasing or decreasing the contents of register or memory location by 1

[label:] INC/DEC register/memory

Flag: OF, SF and ZFOF:is set when an instruction resulted in a carry into the

sign bit of the result. SF: is set if the sign bit of a result is set ZF: is set if the result is equal to 0.

Page 33: Review of Assembly language

Arithmetic instructions

ADD[label:] ADD/SUB operand1, operand 2

operand1 =operand 1 + operand 2

Operand 1: register/memory

Operand 2: register/memory/immediate

Page 34: Review of Assembly language

Arithmetic instructions

SUB[label:] SUB operand1, operand 2

operand1 =operand 1 - operand 2

operand 1: register/memory

operand 2: register/memory/immediate

Page 35: Review of Assembly language

Arithmetic instructions

MUL operandUnsigned multiply.

Operand: register/memory

Page 36: Review of Assembly language

Arithmetic instructions

IMUL operandSigned multiply.

Operand: register/memory

Example:

MOV AX, -2

MOV CX, -3

IMUL CX ; AX = +6

CF = 0

Page 37: Review of Assembly language

Arithmetic instructions

DIV operandUnsigned multiply.

Operand: register/memory

when operand is a byte:AL = AX / operandAH = remainder (modulus)

when operand is a word:DX = remainder (modulus)

Page 38: Review of Assembly language

Arithmetic instructions

IDIV operandSigned multiply.

Operand: register/memory

when operand is a byte:AL = AX / operandAH = remainder (modulus)

when operand is a word:DX = remainder (modulus)

Page 39: Review of Assembly language

Repetitive move instructionsTITLE A04ASM1 (EXE) Move and add operations; ---------------------------------------------STACK SEGMENT PARA STACK 'Stack'

DW 32 DUP(0)STACK ENDS; ----------------------------------------------DATASEG SEGMENT PARA 'Data' STRING1 DW "12345678","$" STRING2 DW ? DATASEG ENDS

Page 40: Review of Assembly language

Repetitive move instructionsMAIN PROC FAR MOV AX, dataseg MOV DS, AX MOV ES, AX MOV CX, 09 ; Initialize to move 9 characters LEA SI, STRING1 ; Initialize source index register to offset of string 1 LEA DI, STRING2 ; Initialize destination index register to offset of string 2

BEGINLOOP: MOV AL,[SI] ; Get a current character from string 1 to AL MOV [DI], AL ; Move it to the current character in string 2 INC SI ; Move to the next character in string 1 INC DI ; Move to the next character in string 2 DEC CX ; Decrease the count for loop JNZ BEGINLOOP ; Continue to loop if count is not 0 MOV AH, 09H LEA DX, STRING2 int 21H ; Display String 2 MAIN ENDP ;End of procedureEND MAIN ;End of programCODESEG ENDS

Page 41: Review of Assembly language

Result

Page 42: Review of Assembly language

Repetitive move instructions

DEC CXZF = 1 if CX = 0

JNZ LABEL

if ZF = 0 then jump to the label

Page 43: Review of Assembly language

Addressing mode

Register addressing: E.g ADD AX, BX

fastest type of operations Immediate addressing

Immediate contains a constant value or an expressionE.g: MOV AX, 0245H

Direct memory addressingOne of operand references a memory location and the

other operand references a registerE.G MOV FLDF, AX

Page 44: Review of Assembly language

Addressing mode

Direct-Offset addressing

use arithmetic instruction to modify an address

e.g MOV CX, DATAZ+2 Indirect memory addressing

Use BX and BP, DI and SI within [ ]

e.g. MOV [BX], CL

Page 45: Review of Assembly language

Addressing mode

Base Displacement AddressingUses BX, BP and DI, SI and combine with a displacement to form an effective addressE.g MOV AL,[SI+2]

Base-Index AddressingCombine BX,BP with DI,SI to form effective address

E.G MOV AL,[BX+SI]

Page 46: Review of Assembly language

Addressing mode

Base-Index Displacement Addressing

Combine BX, BP and DI, SI and a displacement to form an effective address

E.g MOV AL,[BX+SI+2]

Page 47: Review of Assembly language

NEAR and FAR address

NEAR address

consists of 16 bit offset portion of an address

used in real mode

FAR address

consists of both the segment and offset portions in the form of 32 bit segment:offset

Page 48: Review of Assembly language

CMP Instruction

[label:] CMP register/memory, register/memory/immediate

Compares the first to the second operand Affects: AF, CF, OF, PF, SF and ZF flag

CMP AX, DX

JE Startloop

Page 49: Review of Assembly language

Conditional Jump instructions

Jump based on unsigned data

[label:] JE/JZ short-address

Jump if equal or Jump if zero

[label:] JNE/JNZ short-address

Jump if not equal or Jump if not zero

Flag: ZF

Page 50: Review of Assembly language

Example

MOV AL, 5

CMP AL, 5

JE label1

JMP exit

label1: MOV CX, BX

exit: …..

Page 51: Review of Assembly language

Conditional Jump instructions

JG: Jump if first operand is Greater then second operand (as set by CMP instruction). Signed.

if (ZF = 0) and (SF = OF) then jump

Syntax: [label:] JG short-address

Page 52: Review of Assembly language

Example

MOV AL, 5

CMP AL, -5

JG label1

JMP exit

label1: MOV CX, -5 ; in this case AL > -5

exit:

Page 53: Review of Assembly language

Conditional Jump Instruction

JL: Jump if first operand is Less then second operand (as set by CMP instruction). Signed.

if SF <> OF then jump

Syntax: [label:] JL short-address

Page 54: Review of Assembly language

Example

MOV AL, -2

CMP AL, 5

JL label1

JMP exit

label1: MOV CX, 5 ; in this case AL < 5

exit: …