Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What...

34
Week 3 8086/8088 Addressing Modes, Instruction Set & Machine Codes

Transcript of Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What...

Page 1: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

Week 3

8086/8088 Addressing Modes, Instruction Set & Machine Codes

Page 2: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

2

Addressing Modes

• When the 8088 executes an instruction, it performs the specified function on data

• These data, called operands, – May be a part of the instruction– May reside in one of the internal registers of the microprocessor– May be stored at an address in memory

• Register Addressing Mode – MOV AX, BX– MOV ES,AX– MOV AL,BH

• Immediate Addressing Mode– MOV AL,15h– MOV AX,2550h– MOV CX,625

Page 3: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

3

Direct Addressing Mode

02003 FF

Example:MOV AL,[03]

AL=?

MOV CX, [address]

BEED

Page 4: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

4

Register Indirect Addressing Mode

MOV AX, SS:BP

BXDISI

BEED

Page 5: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

5

Based-Relative Addressing Mode

MOV AH, [ ] + 1234hDS:BXSS:BP

AX

DS

BX

1234

3AH+

Page 6: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

6

Indexed Relative Addressing ModeMOV AH, [ ] + 1234hSI

DI

Example: What is the physical address MOV [DI-8],BL if DS=200 & DI=30h ?DS:200 shift left once 2000 + DI + -8 = 2028

Page 7: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

7

Based-Indexed Relative Addressing Mode

• Based Relative + Indexed Relative• We must calculate the PA (physical address)

CSSS BX SI 8 bit displacement

PA= DS : BP + DI + 16 bit displacementES

MOV AH,[BP+SI+29]or

MOV AH,[SI+29+BP]or

MOV AH,[SI][BP]+29

The register

order does not matter

Page 8: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

8

Based-Indexed Relative Addressing Mode

MOV BX, 0600hMOV SI, 0010h ; 4 records, 4 elements each.MOV AL, [BX + SI + 3]

OR

MOV BX, 0600hMOV AX, 004h ; MOV CX,04; MUL CXMOV SI, AXMOV AL, [BX + SI + 3]

Page 9: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

9

Summary of the addressing modesAddressing Mode Operand Default Segment

Register Reg None

Immediate Data None

Direct [offset] DS

Register Indirect [BX][SI][DI]

DSDSDS

Based Relative [BX]+disp[BP]+disp

DSSS

Indexed Relative [DI]+disp[SI]+disp

DSDS

Based IndexedRelative

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

DSSS

Page 10: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

10

16 bit Segment Register Assignments

Type of Memory Reference

Default Segment

Alternate Segment

Offset

Instruction Fetch CS none IP

Stack Operations

SS none SP,BP

General Data DS CS,ES,SS BX, address

String Source DS CS,ES,SS SI, DI, address

String Destination

ES None DI

Brey

Page 11: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

11

Segment override

Segment Registers

CS DS ES SS

Offset Register IP SI,DI,BX SI,DI,BX SP,BP

Instruction Examples Override Segment Used Default Segment

MOV AX,CS:[BP] CS:BP SS:BP

MOV DX,SS:[SI] SS:SI DS:SI

MOV AX,DS:[BP] DS:BP SS:BP

MOV CX,ES:[BX]+12 ES:BX+12 DS:BX+12

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

Page 12: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

12

Assembly Language

• There is a one-to-one relationship between assembly and machine language instructions

• What is found is that a compiled machine code implementation of a program written in a high-level language results in inefficient code– More machine language instructions than an assembled version of an

equivalent handwritten assembly language program• Two key benefits of assembly language programming

– It takes up less memory– It executes much faster

Page 13: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

13

Languages in terms of applications

• One of the most beneficial uses of assembly language programmingis real-time applications.

• Real time means the task required by the application must be completed before any other input to the program that will alter its operation can occur

• For example the device service routine which controls the operation of the floppy disk drive is a good example that is usually written in assembly language

• Assembly language not only good for controlling hardware devicesbut also performing pure software operations– searching through a large table of data for a special string of characters– Code translation from ASCII to EBCDIC– Table sort routines– Mathematical routines

• Assembly language: perform real-time operations• High-level languages: Those operations mostly not critical in time.

Page 14: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

14

Converting Assembly Language Instructions to Machine Code

OPCODE D W MOD REG R/M

• An instruction can be coded with 1 to 6 bytes• Byte 1 contains three kinds of information:

– Opcode field (6 bits) specifies the operation such as add, subtract, or move– Register Direction Bit (D bit)

• Tells the register operand in REG field in byte 2 is source or destination operand– 1: destination– 0: source

– Data Size Bit (W bit)• Specifies whether the operation will be performed on 8-bit or 16-bit data

– 0: 8 bits– 1: 16 bits

• Byte 2 has two fields:– Mode field (MOD)– Register field (REG)– Register/memory field (R/M field)

Page 15: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

15

Continued

• REG field is used to identify the register for the first operand

REG W = 0 W = 1000 AL AX

001 CL CX

010 DL DX

011 BL BX

100 AH SP

101 CH BP

110 DH SI

111 BH DI

Page 16: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

16

Continued

• 2-bit MOD field and 3-bit R/M field together specify the second operand

Page 17: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

17

Examples• MOV BL,AL• Opcode for MOV = 100010• We’ll encode AL so

– D = 0 (AL source operand)• W bit = 0 (8-bits)• MOD = 11 (register mode)• REG = 000 (code for AL)• R/M = 011MOV BL,AL => 10001000 11000011 = 88 C3h

ADD AX,[SI] => 00000011 00000100 = 03 04 hADD [BX][DI] + 1234h, AX =>

OPCODE D W MOD REG R/M

000000 0 1 10 000 001

00000001 10000001 __ __ h

=> 01 81 34 12 h

Page 18: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

18

Software

• The sequence of commands used to tell a microcomputer what to do is called a program

• Each command in a program is called an instruction• 8088 understands and performs operations for 117 basic instructions• The native language of the IBM PC is the machine language of the

8088• A program written in machine code is referred to as machine code• In 8088 assembly language, each of the operations is described by

alphanumeric symbols instead of just 0s or 1s.

ADD AX, BX

Opcode Source operand

Destination operand

Page 19: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

19

Instructions

[LABEL:] MNEMONIC [OPERANDS] [; COMMENT]

Address identifierMax 31 characters

: indicates it opcode generating instruction

Does not generate any machine codeInstruction

Ex. START: MOV AX,BX ; copy BX into AX

Page 20: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

20

DEBUG program instruction set

• Debug instructions • List of commands

– a Assemble [address] you can type in code this way – c range address ; compare c 100 105 200– d [range] ; Dump d 150 15A– e address [list] ; Enter e 100– f Fill range list F 100 500 ‘ ‘– g Go [=address] addresses runs the program – h Value1 Value2 ; addition and subtraction H 1A 10– i Input port I 3F8 – r Show registers Appears to show the same thing as t, but

doesn't cause any code to be executed. – t Trace either from the starting address or current

location.– u UnAssemble

Page 21: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

21

Example

Copy the contents of a block of memory (16 bytes) starting at location 20100h to another block of memory starting at 20120h

MOV AX,2000hMOV DS,AXMOV SI, 100hMOV DI, 120hMOV CX, 10h

NXTPT: MOV, AH, [SI]MOV [DI], AHINC SIINC DIDEC CXJNZ NXTPT

100-10f

120-12f

Page 22: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

22

Assembler Directives.MODEL SMALL ; selects the size of the memory model usually sufficient max 64K code 64K data

.STACK [size] ; beginning of the stack segment

.DATA ; beginning of the data segment

.CODE ; beginning of the code segment

Ex: .DATADATAW DW 213FHDATA1 DB 52HSUM DB ? ; nothing stored but a storage is assignedSUM DB 64 DUP (?)Ex: .CODEPROGRAMNAME PROC; Every program needs a name

…. ; program statementsPROGRAMNAME ENDP

END PROGRAMNAME

Page 23: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

23

MODELMemoryModel can be SMALL, COMPACT, MEDIUM, LARGE, HUGE, TINY

Tiny: This means that there is only one segment for both code and data. This type of program can be a .COM file.

Small: This means that by default all code is placed in one physical segment and likewise all data declared in the data segment is also placed in one physical segment. This means that all procedures and variables are addressed as NEAR by pointing at offsets only.

Compact: This means that by default all elements of code (procedures) are placed in one physical segment but each element of data can be placed in its own physical segment. This means that data elements are addressed by pointing at both at the segment and offset addresses. Code elements (procedures) are NEAR and variables are FAR.

Medium: This is the opposite to compact. Data elements are near and procedures are FAR. Large: This means that both procedures and variables are FAR. You have to point at both the segment and offset addresses.

Large: This means that both procedures and variables are FAR. You have to point at both the segment and offset addresses. Huge: Data set over 64K!

Page 24: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

24

DB DW DD

DTSEG SEGMENT

MESSAGE2 DB '1234567'

MESSAGE3 DW 6667H

data1 db 1,2,3

db 45h

db 'a'

db 11110000b

data2 dw 12,13

dw 2345h

dd 300h

DTSEG ENDS

31 32 33 34 35 36 37

67 66

1 2 3

45

61

F0

0C 00 0D 00

45 23

00 30 00 00

Page 25: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

25

Sample Programtitle Hello World Program (hello.asm); This program displays "Hello, world!".model small.stack 100h.datamessage db "Hello, world!",0dh,0ah,'$‘ ;newline+eoc.codemain proc

mov ax,@data ; address of datamov ds,axmov ah,9hmov dx,offset message ;disp.msg.starting at 0int 21h ; or LEA dx,message will do!mov ax,4C00h ; halt the program and returnint 21h

main endpend main

Page 26: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

26

The PTR Operator

• INC [20h] ; is this byte/word/dword? or• MOV [SI],5

– Is this byte 05? – Is this word 0005?– Or is it double word 00000005?

• Byte or word or doubleword?

• To clarify we use the PTR operator– INC BYTE PTR [20h]– INC WORD PTR [20h]– INC DWORD PTR [20h]

• or for the mov example:– MOV byte ptr [SI],5– MOV word ptr[SI],5

Page 27: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

27

The PTR Operator

• Would we need to use the PTR operator in each of the following?

MOV AL,BVALMOV DL,[BX]SUB [BX],2MOV CL,WVALADD AL,BVAL+1

.dataBVAL DB 10H,20HWVAL DW 1000H

MOV AL,BVALMOV DL,[BX]SUB [BX],byte ptr 2MOV CL,byte ptr WVALADD AL,BVAL+1

Page 28: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

28

MS-DOS Functions and BIOS Calls

ApplicationPrograms

Command ProcessorCOMMAND.COM

MS-DOS KernelMSDOS.SYS

BIOSROM plus IO.SYS

System Hardware

• BIOS is hardware specific• BIOS is supplied by the computer manufacturer• Resident portion which resides in ROM and nonresident portion IO.SYS

which provides a convenient way of adding new features to the BIOS

Page 29: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

29

80x86 Interrupts

• An interrupt is an event that causes the processor to suspend its present task and transfer control to a new program called the interrupt service routine (ISR)

• There are three sources of interrupts– Processor interrupts– Hardware interrupts generated by a special chip, for ex: 8259 Interrupt

Controller.– Software interrupts

• Software Interrupt is just similar to the way the hardware interrupt actually works!. The INT Instruction requests services from the OS, usually for I/O. These services are located in the OS.

• INT has a range 0 FFh. Before INT is executed AH usually contains a function number that identifies the subroutine.

Page 30: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

30

• Each interrupt must supply a type number which is used by the processor as a pointer to an interrupt vector table (IVT) to determine the address of that interrupt’s service routine

• Interrupt Vector Table: CPU processes an interrupt instruction using the interrupt vector table (This table resides in the lowest 1K memory)

• Each entry in the IVT=32 bit segment+offset adress in OS, points to the location of the corresponding ISR.

• Before transferring control to the ISR, the processor performs one very important task

– It saves the current program address and flags on the stack– Control then transfers to the ISR– When the ISR finishes, it uses the instruction IRET to recover the flags and old

program address from the stack• Many of the vectors in the IVT are reserved for the processor itself and

others have been reserved by MS-DOS for the BIOS and kernel.– 10-1A are used by the BIOS– 20 – 3F are used by the MS-DOS kernel

Page 31: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

31

80x86 Interrupts• The number after the mnemonic tells which entry to locate in the

table. For example INT 10h requests a video service.

mov..int 10hadd

Interrupt Handler

IRET

4

12

F000:F065

3

Entry for INT 10

Page 32: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

32

Interrupt Vector Table

Page 33: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

33

Interrupts

• There are some extremely useful subroutines within BIOS or DOS that are available to the user through the INT (Interrupt) instruction.

• The INT instruction is like a FAR call; when it is invoked– It saves CS:IP and flags on the stack and goes to the subroutine

associated with that interrupt.– Format:

• INT xx ; the interrupt number xx can be 00-FFH– This gives a total of 256 interrupts– Two of them are widely used: INT 10H and INT 21H– Before the services of INT 10H and 21H is requested, certain registers

must have specific values in them, depending on the function being requested.

Page 34: Week 3 8086/8088 Addressing Modes, Instruction Set ... · • Register Addressing Mode ... • What is found is that a compiled machine code implementation of a program written in

34

Common Software Interrupts (DOS or BIOS)

• Int 10 Video Services: Video Display Routines that control the cursor position, scroll the screen and display video graphics.

• Int 16h Keyboard Services. Routines that read the keyboard and checks the status.

• Int 17h Printer Services.• Int 1Ah Time of Day. Gets the number of clock ticks since the

machine was turned on.• Int 21h DOS services. DOS service routines for input and output, file

handling memory management. Also known as: “DOS Function Calls” There are 90 different functions supported by this interrupt.