Top Banner
Fundamentals of Assembly language
36

Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

Dec 26, 2015

Download

Documents

Norman Briggs
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: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

Fundamentals of Assembly language

Page 2: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

Lesson Plan

• Review concepts from the last lecture

• Practice exercise

• Learning Assembly language from examples

Page 3: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

Instruction Execution and Addressing

• Executing an instruction include– Fetch the next instruction– Decode the instruction– Execute the instruction

Page 4: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

Instruction Execution and Addressing

• Instruction address = Code Segment address (CS) + Instruction Offset (IP)

• Data address = Data Segment address (DS) + Data Offset

Page 5: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

26AECS 0044IP

Instruction address = ???????

Page 6: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

26AECS 0044IP

Instruction address = 26AE0+ 0044

_______________________ 26B24

25BDDS

A03F0026B24 Data address=??????

Page 7: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

26AECS 0044IP

Data address = 25BD0 + 003F

_______________________ 25C0F

25BDDS

Page 8: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

Tools

• Decimal to Hexadecimal converter http://www.tonymarston.net/php-mysql/converter.php

• EMU8086

Page 9: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

Review old concepts

• Address of BIOS data area: starts at 0040H

• Boot process:– CS: FFFF (Segment address: FFFF0)– IP:0000

Page 10: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

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 11: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

Practice

• Type the example program in EMU8086

• Run the program step by step

Page 12: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

; 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 13: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

; 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 0215HFLDE DW 0125HFLDF 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 14: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

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

Page 15: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

Practice

• Change the variables in the existing program

• Assign new values to them

• Compile and run

Page 16: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

• Instructions: ADD, MOV

• Directives: .TITLE, .MODEL

• Operators: FAR, SIZE

• Pre-defined symbols: @Data, @Model

• Register: AX,BX

RESERVED WORDS

Page 17: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

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 18: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

; 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 19: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

Directives

• Control the way a source program assembles and lists

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

Page 20: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

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

page 60,132 page [length(10-255)],[width(60-132)]TITLE 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

Page directive

Page 21: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

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

page 10,70TITLE 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

Page directive

Page 22: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

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

page 10,70TITLE 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

Title directive

Page 23: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

; 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 24: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

Segment directive

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

Segment-name ENDS

Example:

STACK SEGMENT PARA STACK 'Stack‘

STACK ENDS

Page 25: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

; 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 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 26: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

PROC directive

• Format:Procedure-name PROC Operand Comment

Procedure-name ENDP

Operand: relates to program execution (FAR)

Page 27: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

; 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 28: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

ASSUME directive

• Tells the assembler the purpose of each segment in the program

Example:

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

Page 29: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

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 30: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

Simplified Segment Directives

• STACK [size] (default: 1K)

• DATA (default size is 1K)

• CODE (default size is 1K)

• .EXIT directive

Page 31: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

EQUATE directives

• Equal-Sign directiveCOEFFICIENT= 100

• EQU directive

COEFFICIENT EQU 100

Page 32: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

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 33: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

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 34: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

Directives for defining Data

• Byte: DBWord: DW

Doubleword: DD

Farword: DF

Quadword: DQ

Tenbytes: DT

Page 35: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

Some instructions on arithmetic calculation• ADD:

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

• SubtractSUB register register/memory/immediateExample: SUB AX, 100

• MultiplicationMUL register/memoryExample: MUL CX

• DivisionDIV register/memoryExample DIV CX

Page 36: Fundamentals of Assembly language. Lesson Plan Review concepts from the last lecture Practice exercise Learning Assembly language from examples.

Practice

• Modify the existing code (last week) to perform the following operations:

Given A= 215, B= 125, C=100, D=20,E=10

Compute:

F= (A+B-C)*D/E