Topics Introduction to Microprocessors. Chapter 2 Assembly Programming. Addressing Modes (cont.) Examples of Addressing Modes (I)

Topics ¾ Addressing modes (cont.) Assembly programming A sample program Directives Simplified segment definition ¾ ¾ Control transfer instructions...
Author: Kevin Shepherd
5 downloads 3 Views 64KB Size
Topics

¾

Addressing modes (cont.) Assembly programming A sample program Directives Simplified segment definition

¾ ¾

Control transfer instructions Instruction set of x86

¾

Executing assembly programs

¾ ¾

2102440 Introduction to Microprocessors

¾ ¾

Chapter 2 Assembly Programming

z

z

Directives

Unsigned addition and subtraction

Suree Pumrin, Pumrin, Ph.D. 1

Addressing Modes (cont.)

Examples of Addressing Modes (I)

It describes the types of operands and the way they are accessed from the CPU.

Register Immediate Direct Register indirect

1. 2. 3. 4.

¾ Direct

MOV AX, BX ADD AL, 10H MOV CX, [1200H] MOV AL, [BX]

z

5.

Based relative

MOV CX, [BX+10H]

6.

Indexed relative

MOV AX, [SI+15H]

MOV DL, [2400H] ; move contents of DS:2400H into DL

¾ Register indirect

SI,DI

z

MOV AL, [BX]

z

MOV [DI], AH

BP

DI

Based indexed relative

7.

MOV AH, [BX+DI+5H] BP

SI

2102440 Introduction to Microprocessors

3

Examples of Addressing Modes (II) ¾

2

2102440 Introduction to Microprocessors

; move the contents of the memory location DS:BX into AL ; move contents of AH into DS:DI 4

2102440 Introduction to Microprocessors

Offsets Registers for Various Segments

Based relative z

z

MOV CX, [BX] + 10H ; move DS:BX+10H and DS:BX + 10H + 1H into CX MOV AL, [BP] + 5H ; PA = SS:BP+5H

¾

Indexed relative

¾

Based indexed relative

z

z z

MOV DX, [SI] + 5H

Segment register

CS

Offset IP register(s) register(s)

DS

ES

SS

SI, DI, BX

SI, DI, BX

SP, BP

; PA = DS:SI+5H

MOV CL, [BX][DI]+8H MOV AH, [BP][DI]+12H

; PA = DS:BX+DI+8H ; PA = SS:BP+DI+12H

2102440 Introduction to Microprocessors

5

2102440 Introduction to Microprocessors

6

1

Summary of 80x86 Addressing Modes

Segment Overrides Instruction

Segment Used

MOV AX, CS:[BP]

Addressing Mode

Operand

Default Segment

Register

Reg

none

Immediate

Data

none

Direct

[offset]

DS

Register indirect

[BX] [SI] [DI]

DS DS DS

Default Segment

CS:BP

SS:BP

MOV DX, SS:[SI]

SS:SI

DS:SI

MOV AX, DS:[BP]

DS:BP

SS:BP

MOV CX, ES:[BX]+12H

ES:BX+12H

DS:BX+12H

MOV SS:[BX][DI]+32H, AX

SS:BX+DI+32H

DS:BX+DI+32

2102440 Introduction to Microprocessors

Based relative

[BX]+disp [BP]+disp

DS SS

Indexed relative

[DI]+disp [SI]+disp

DS DS

Based indexed relative

[BX][SI]+disp [BX][DI]+disp [BP][SI]+disp [BP][DI]+disp

DS DS SS SS

7

2102440 Introduction to Microprocessors

Assembly Instruction

Segments of a Program

label: mnemonic operands; comment

label Same

optional

¾ ¾ ¾

¾

optional

optional

label

Label cannot exceed 31 characters and must be unique. The mnemonic (instruction) and operands fields is used for the real tasks. PseudoPseudo-instructions called “directive” directive” give directions to the assembler on how it translate the instructions into machine code. Comment field begins with a “;”. 2102440 Introduction to Microprocessors

9

Shell of an Assembly Program

label

SEGMENT [options] ;place the statements here ENDS

Example: STSEG SEGMENT DB 64 DUP(?) STSEG ENDS

;the segment directive ;reserves 64 bytes

2102440 Introduction to Microprocessors

10

Simple Assembly Program

;THE FORM OF AN ASSEMBLY LANGUAGE PROGRAM STSEG SEGMENT DB 64 DUP(?) STSEG ENDS ;---------------------DTSEG SEGMENT ; ;place data here ; DTSEG ENDS ;---------------------CDSEG SEGMENT MAIN PROC FAR ;this is the program entry point ASSUME CS:CDSEG, DS:DTSEG, SS:STSEG MOV AX,DTSEG ;bring in the segment for data MOV DS,AX ;assign value to DS ; ;place code here ; MOV AH, 4CH ;return control to operating system INT 21H ;return to DOS MAIN ENDP CDSEG ENDS END MAIN ;this is the program exit point

2102440 Introduction to Microprocessors

8

;THE FORM OF AN ASSEMBLY LANGUAGE PROGRAM STSEG SEGMENT DB 64 DUP(?) STSEG ENDS ;---------------------DTSEG SEGMENT DATA1 DB 52H DATA2 DB 29H SUM DB ? DTSEG ENDS ;---------------------CDSEG SEGMENT MAIN PROC FAR ;this is the program entry point ASSUME CS:CDSEG, DS:DTSEG, SS:STSEG MOV AX,DTSEG ;load the data segment address MOV DS,AX ;assign value to DS MOV AL,DATA1 ;get the first operand MOV BL,DATA2 ;get the second operand ADD AL,BL ;add the operands MOV SUM,AL ;store result in location SUM AH,4CH ;set up to MOV INT 21H ;return to DOS MAIN ENDP CDSEG ENDS END MAIN ;this is the program exit point

11

2102440 Introduction to Microprocessors

12

2

Executable Program (I) EDITOR PROGRAM

Executable Program (II)

There are three steps to create an executable assembly language program.

myfile.asm

(1) Edit the program: ASSEMBLER PROGRAM

keyboard Æ myfile.asm (2) Assemble the program:

myfile.lst

myfile.crf

myfile.asm Æmyfile.obj

other files .obj

myfile.obj

(3) Link the program:

LINKER PROGRAM

myfile.obj Æ myfile.exe myfile.map

.asm: the source file .obj: the assembler converts the .asm file into machine code (object file). .lst: (optional) it lists all the opcodes and offset addresses as well as errors that the assembler detected. .crf: (optional) the cross-reference lists all the symbols and labels used in the program. .map: it gives the name of each segment, where it starts, where it stops, and its size in bytes. .exe: It can be executed by microprocessor. (ready-to-run version of a program)

myfile.exe 2102440 Introduction to Microprocessors

13

2102440 Introduction to Microprocessors

DEBUG executable program

TASM style DOSSEG; "TASM style“ IDEAL MODEL SMALL SEGMENT DDD ;your data in Data Segment ENDS SEGMENT SSS ;your data in Stack ENDS SEGMENT CCC ASSUME CS:CCC, DS:DDD, SS:SSS MAIN: MOVE AX,DDD MOV DS,AX ;your Assembly code MOV AH, 4CH INT 21H ENDS END MAIN

> DEBUG myfile.exe -U CS:0 1 (unassemble) unassemble) 1064:0000 B86610 MOV AX, 1066 -D 1066:0 F (Dump memory) 1066:0000 52 29 …00 R)… 16

ตัว

2102440 Introduction to Microprocessors

15

2102440 Introduction to Microprocessors

Data Directives ¾

ORG (origin)

¾

DB (define byte)

z

16

DUP ¾

Indicate the beginning of the offset address.

DUP (duplicate) Duplicate a given number of characters. ORG 0030H DATA7 DB 0FFH, 0FFH, 0FFH; Fill 3 bytes with FFH ORG 38H DATA8 DB 3 DUP (0FFH) ; Fill 3 bytes with FFH ORG 40H DATA9 DB 32 DUP (?) ; Set aside 32 bytes DATA10 DB 5 DUP (2 DUP (99H)); Fill 10 bytes with 99H z

Allocate memory in bytebyte-sized chunks ’ ’ indicates ASCII strings z DATA1 DB 25 ; Decimal DATA2 DB 10001001B ; Binary DATA3 DB 12H ; HEX ORG 0010H DATA4 DB ’2591’ ; ASCII Numbers 2591’ ORG 0018H DATA5 DB ? ; Set aside a byte ORG 0020H DATA6 DB ’Hello world’ ;ASCII Characters world’ z

2102440 Introduction to Microprocessors

14

17

2102440 Introduction to Microprocessors

18

3

DW ¾

EQU, DD, DQ, and DT

DW (define word) z

¾

Allocate memory 2 bytes (one word) at a time.

DATA11 DATA12 DATA13 DATA14 DATA15

Define a constant without occupying a memory location. COUNT EQU 25H MOV CX, COUNT

z

ORG 70H DW 954 ; Decimal DW 100110101110B ; Binary DW 253FH ; HEX ORG 78H DW 9,2,7,0CH,00100001B,5,’ 9,2,7,0CH,00100001B,5,’HI’ HI’; Miscellaneous data DW 8 DUP(?) ; Set aside 8 words 2102440 Introduction to Microprocessors

EQU (equate)

¾

DD ( define doubleword) doubleword)

¾

DQ (define quadword) quadword)

¾

DT (define ten bytes)

z

z

z

Allocate 44-byte memory locations. Allocate 88-byte memory locations. It is used for memory allocation of packed BCD numbers.

19

Examples of DD, DQ, and DT

Memory Model

ORG 00A0H DATA16 DD 1023 ; Decimal DATA17 DD 10010010110011000110B ; Binary DATA18 DQ 5C348FH ; HEX DATA19 DT 123922910650; BCD (maximum of 10 bytes or 18 digits) DATA20 DT ? ; Nothing 2102440 Introduction to Microprocessors

Model

Max. Code (bytes)

Max. Data (bytes)

Small

64K

64K

Medium

> 64K

64K

Compact

64K

> 64K

Large

> 64K

> 64K

Tiny

Code + Data < 64K

21

2102440 Introduction to Microprocessors

Shell of an Assembly Program (Simplified Segment Definition)

22

Directives

;THE FORM OF AN ASSEMBLY LANGUAGE PROGRAM USING SIMPLIFIED SEGMENT DEFINITION .MODEL SMALL .STACK 64 .DATA ; ;place data definitions here ; .CODE MAIN PROC FAR ;this is the program entry point MOV AX,@DATA ;load the data segment address MOV DS,AX ;assign value to DS ; ;place code here ; MOV AH,4CH ;set up to INT 21H ;return to DOS MAIN ENDP END MAIN ;this is the program exit point

2102440 Introduction to Microprocessors

20

2102440 Introduction to Microprocessors

¾

PAGE and TITLE z z

z

z

PAGE [lines], [column] Æ tell the printer how the list should be printed. Default setting PAGE (no numbers after it): 66 lines per page and and max. 80 characters PAGE 60,132 Æ the range of number of lines = 10 t0 255 and range of columns = 60 to 132. TITLE Æ put the name of the program and a brief description of the function function of the program.

¾

.STACK ; marks the beginning of the stack segment .DATA ; marks the beginning of the data segment .CODE; marks the beginning of the code segment

¾

.STACK 64 ; reserves 64 bytes of memory for the stack

¾ ¾

mov ax,@data ; data refers to the start of the data segment mov ds,ax 23

2102440 Introduction to Microprocessors

24

4

Control Transfer Instructions

8086 Conditional Jump (I)

¾ To transfer program control to a different

location. z

NEAR: control transfer within the current code segmentÆ segmentÆ intrasegment (within segment) - A NEAR jump Æ IP is updated; CS remains the same.

z

FAR: control transfer outside the current code segment Æ intersegment (between segment) - A FAR jump Æ both IP and CS are updated.

2102440 Introduction to Microprocessors

25

8086 Conditional Jump (II) Mnemonic

Condition Tested

“Jump IF …”

JLE/JNG

((SF xor OF) or ZF) = 1

less or equal/not greater

JNC

CF = 0

not carry

JNE/JNZ

ZF = 0

not equal/not zero

JNO

OF = 0

not overflow

JNP/JPO

PF = 0

not parity/parity odd

JNS

SF = 0

not sign

JO

OF = 1

overflow

JP/JPE

PF = 1

parity/parity even

JS

SF = 1

sign 2102440 Introduction to Microprocessors

above/not below nor zero

JAE/JNB

CF = 0

above or equal/not below

JB/JNAE

CF = 1

below/not above nor equal

JBE/JNA

(CF or ZF) = 1

below or equal/not above

JC

CF = 1

carry

JE/JZ

ZF = 1

equal/zero

JG/JNLE

((SF xor OF) or ZF) = 0

greater/not less nor equal

JGE/JNL

(SF xor OF) = 0

greater or equal/not less

JL/JNGE

(SF xor OF) = 1

less/not greater nor equal

2102440 Introduction to Microprocessors

26

-128 to +127 bytes of the IP (256 possible addresses). ¾ - for the backward jump and + for the forward jump.

27

2102440 Introduction to Microprocessors

28

CALL statements • CALL instruction is used to call a procedure.

SHORT JUMP Æ “JMP SHORT label” label” (-128 to 127 bytes) NEAR JUMP Æ “JMP label” label” (within the current code segment); the target address can be any of the addressing modes of direct, register, or register indirect. FAR JUMP Æ “JMP FAR PTR label” label” (CS and IP are replaced with new values) 2102440 Introduction to Microprocessors

“Jump IF …”

¾ The address of the target must be within

“JMP label” label” ¾ To transfer unconditionally to the target location label.



(CF = 0) and (ZF = 0)

¾ All conditional jumps are short jumps.

¾



Condition Tested

JA/JNBE

Short Jumps

Unconditional jump



Mnemonic

29

• NEAR call Æ within the current code segment. Only IP is saved on the stack. • FAR call Æ> outside the current code segment. Both CS and IP are saved on the stack.

;------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------MAIN PROC FAR ; This is the entry point for DOS MOV AX,@DATA MOV DS, AX CALL SUBR1 CALL SUBR2 MOV AH,4cH INT 21H MAIN ENDP ;------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------SUBR1 PROC ... ... RET SUBR1 ENDP ;------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------SUBR2 PROC ... ... RET SUBR2 ENDP ;------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------END MAIN ; This is the exit point

2102440 Introduction to Microprocessors

30

5

Unsigned Addition and Subtraction ¾

Executing an Assembly Program ¾

ADD destination, source ;

¾

dest. dest. operand = dest. dest. operand + source operand

¾

¾

Dest. Dest. Operand = dest. dest. Operand + source operand + carry

The destination operand can be a register or in memory. The source operand can be a register, in memory, or immediate. MemoryMemory-toto-memory operations are not ALLOWED in 80x86.

z

z

2102440 Introduction to Microprocessors

31

Write a program calculates the total sum of 5 bytes of data. Each byte represents the daily wages of a worker. This person does not make more than $255 (FFH) a day. The decimal data is as follows: 125, 235, 197, 91, 48.

33

Sample Program 1 (cont.)

BACK:

OVER:

MAIN

PROC MOV MOV MOV MOV MOV ADD JNC INC INC DEC JNZ MOV MOV INT ENDP END

FAR AX,@DATA DS,AX CX,COUNT SI,OFFSET DATA AX,00 AL,[SI] OVER AH SI CX BACK SUM,AX AH,4CH 21H

Run the Linker to generate an .EXE file from the .OBJ file

3. ¾

tlink yourfile

Run the Program

4. ¾

Your final program (if there were no errors in the previous step) step) will have an .EXE ending. To just run it, type: yourfile

If want to debug your program

5. ¾

td yourfile

2102440 Introduction to Microprocessors

32

;This program calculates the total sum of 5 bytes of data. Each byte represents the daily wages ; of a worker. This person does not make more than $255 (FFH) a day

Example 1: Addition of individual byte and word data

.CODE MAIN

tasm /z/zi yourfile.asm (The /z switch causes TASM to display the lines that generate compilation errors. The /zi /zi switch enables information needed by the debugger to be included in the .OBJ file.) file.)

Sample Program 1

Assembly Program Example 1

2102440 Introduction to Microprocessors

notepad yourfile.asm wordpad yourfile.asm edit yourfile.asm

Compile the source code to create an object module

2.

ADC destination, source; z

Edit yourfile.asm using one of the follows:

1.

Addition of unsigned numbers

TITLE PROG3PROG3-1A (EXE) ADDING 5 BYTES PAGE 60,132 .MODEL SMALL .STACK 64 ;-------------.DATA COUNT EQU 05 DATA DB 125,235,197,91,48 ORG 0008H SUM DW ? ;-------------2102440 Introduction to Microprocessors

34

Assembly Program Example 2 Example 2: Addition of multiword numbers

;CX is the loop counter ;SI is the data pointer ;AX will hold the sum ;add the next byte to AL ;If no carry, continue ;else accumulate carry in AH ;increment data pointer ;decrement loop counter ;if not finished, go add next byte ;store sum

Write a program to calculate the total sum of five words of data. Each data represents the yearly wages of a worker. This person does not make more than $65,555 (FFFFH) a year. The decimal data are 27345, 28521, 29533, 30105, and 32375.

;go back to DOS

MAIN

2102440 Introduction to Microprocessors

35

2102440 Introduction to Microprocessors

36

6

Sample Program 2

Sample Program 2 (cont.)

;This program calculates the total sum of five words of data. Each Each data value represents the yearly ; wages of a worker. This person does not make more than $65,555 $65,555 (FFFFH) a year TITLE PROG3PROG3-1B (EXE) ADDING 5 WORDS PAGE 60,132 .MODEL SMALL .STACK 64 ;-------------.DATA COUNT EQU 05 DATA DW 27345,28521,29533,30105,32375 ORG 0010H SUM DW 2 DUP(?) ;-------------2102440 Introduction to Microprocessors

.CODE MAIN

BACK:

MAIN

37

PROC MOV MOV MOV MOV MOV MOV ADD ADC INC INC DEC JNZ MOV MOV MOV INT ENDP END

FAR AX,@DATA DS,AX CX,COUNT SI,OFFSET DATA AX,00 BX,AX AX,[SI] BX,0 SI SI CX BACK SUM,AX SUM+2,BX AH,4CH 21H

;CX is the loop counter ;SI is the data pointer ;AX will hold the sum ;BX will hold the carries ;add the next word to AX ;add carry to BX ;increment data pointer twice ;to point to next word ;decrement loop counter ;if not finished, continue adding ;store the sum ;store the carries ;go back to DOS

MAIN

2102440 Introduction to Microprocessors

38

7