64
Week 4 8088/8086 Microprocessor Programming I

Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

  • Upload
    others

  • View
    41

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

Week 4

8088/8086 Microprocessor Programming I

Page 2: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

2

MASM

myfile.exeLINK or TLINK

myfile.objLink the program

myfile.objMASM or TASM

myfile.asmAssemble the program

myfile.asmEditorKeyboardEdit the program

OutputProgram InputStep

Page 3: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

3

Simple Assembly Language Program.MODEL SMALL.STACK 64.DATA

DATA1 DB 52hDATA2 DB 29hSUM DB ?

.CODEMAIN PROC FAR

MOV AX,@DATAMOV DS,AXMOV AL,DATA1MOV BL,DATA2ADD AL,BLMOV SUM,ALMOV AH,4ChINT 21h

MAIN ENDPEND MAIN

Page 4: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

4

DataTypes and Data DefinitionDATA1 DB 25DATA2 DB 10001001bDATA3 DB 12h

ORG 0010hDATA4 DB “2591”

ORG 0018hDATA5 DB ?

This is how data is initialized in the data segment0000 190001 89 0002 120010 32 35 39 31 0018 00

Page 5: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

5

Other Definitions - Examples

DB 6 DUP(FFh) ;duplicate fill 6 bytes with ffh

DW 954DW 253FhDW 253Fh,’HI’ ; allocates two bytes

DD 5C2A57F2h ;allocates four bytesDQ “HI” ; allocates eight bytes

COUNT EQU 25COUNTER1 DB COUNTCOUNTER2 DB COUNT

Page 6: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

6

MS-DOS Functions and BIOS Calls

• 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

ApplicationPrograms

Command ProcessorCOMMAND.COM

MS-DOS KernelMSDOS.SYS

BIOS ROM plus IO.SYS

System Hardware

Page 7: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

7

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– Software interrupts– Hardware interrupts

• 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

• Each entry in the IVT 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 8: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

8

Interrupt Vector Table

Page 9: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

9

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– Common Interrupts

• INT 10h Video Services• INT 16h Keyboard Services• INT 17h Printer Services• INT 21h MS-DOS services

– Before the services, certain registers must have specific values in them, depending on the function being requested.

Page 10: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

10

INT 16h Keyboard Services• Checking a key press, we use INT 16h function AH = 01

MOV AH, 01INT 16h

• Upon return, ZF = 0 if there is a key press; ZF = 1 if there is no key press

• Whick key is pressed?• To do that, INT 16h function can be used immediately after the call

to INT 16h function AH=01

MOV AH,0INT 16h

• Upon return, AL contains the ASCII character of the pressed key

Page 11: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

11

INT 10H Video Services (full screen)• INT 10H subroutines are burned into the ROM BIOS of the 80x86-based PC

and are used to communicate with the computer’s screen video.•

• INT 10H Function 06– AL = number of lines to scroll (with AL=00, all lines)– BH = attribute of blank rows– CH, CL = upper row, left column– DH, DL = lower row, right column– As a window is scrolled up, its bottom line is replaced by a blank line

00,00 00,79

24,00 24,79

12,39

Cursor Locations

Video mode 3, color text, 80 columns by 25 rows

Page 12: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

12

INT 10H• INT 10H function 02; setting the cursor to a specific location

– Function AH = 02 will change the position of the cursor to any location.– The desired cursor location is in DH = row, DL = column

• EX. Write a program that clears the screen and sets the cursor at the center of the screen

• INT 10H function 03; get current cursor position

– Registers DH and DL will have the current row and column positions and CX provides info about the shape of the cursor.

; clearing the screenMOV AX, 0600H ;scroll the entire pageMOV BH, 07 ; normal attribute (white on black)MOV CX, 0000 ; upper leftMOV DX,184FH ; lower rightINT 10H

;setting the cursor at the centerMOV AH,02 ; set cursor optionMOV BH, 00 ; page 0MOV DL, 39 ; MOV DH, 12 ;INT 10H

MOV AH, 03MOV BH, 00INT 10H

Page 13: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

13

INT 21H

• INT 21H is provided by DOS to be invoked to perform extremely useful functions.

• INT 21H Option 09: Outputting a string of data to the monitor– INT 21H can be used to send a set of ASCII data to the monitor.– AH = 09; DX = offset address of the ASCII data to be displayed.– INT 21H option 09 will display the ASCII data string pointed to by DX

until it encounters the dollar sign “$”.

• INT 21H Option 02: Outputting a single character to the monitor– DL is loaded with the character first

DATA_ASC DB ‘The earth is one country’,’$’

MOV AH,09MOV DX, OFFSET DATA_ASCINT 21H

MOV AH 02MOVE DL,’J’INT 21H

Page 14: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

14

INT 21H

• INT 21H Option 01: Inputting a single character with echo– This function waits until a character is input from the keyboard, then

echoes it to the monitor. After the interrupt, the input character will be in AL.

• INT 21H Option 0AH: Inputting a string of data from the keyboard – AH = 0AH– DX = offset address at which the string is stored– First byte specifies the size of the buffer, second byte is the number of

characters

ORG 0010HDATA1 DB 6,?,6 DUP(FF)

MOV AH, 0AHMOV DX, OFFSET DATA1INT 21H

Ex. What happens if one enters USA and then <RETURN>

0010 0011 0012 0013 0014 0015 0016 0017

06 03 55 53 41 0D FF FF

Page 15: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

15

INT 21h

• INT 21h function 4C– Terminate process (recommended way of process termination)– Or INT 20h

Page 16: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

16

Example. The PC Typewriter

• Write an 80x86 program to input keystrokes from the PC’s keyboard and display the characters on the system monitor. Pressing any of the function keys F1-F10 should cause the program to end.

• Algorithm:1. Get the code for the key pressed 2. If this code is ASCII, display the key pressed on the monitor and

continue3. Quit when a non-ASCII key is pressed

• INT 16, BIOS service 0 – Read next keyboard character– Returns 0 in AL for non-ASCII characters or the character is simply

stored in AL • To display the character, we use INT 10, BIOS service 0E- write

character in teletype mode. AL should hold the character to be displayed.

• INT 20 for program termination

Page 17: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

17

Example continued

AGAIN: MOV AH,0INT 16CMP AL,00JZ QUITMOV AH, 0EINT 10JMP AGAIN

QUIT: INT 20

Page 18: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

18

Example continued with sign-on messages

MOV DX, OFFSET MESMOV AH,09INT 21

AGAIN: MOV AH,0INT 16CMP AL,00JZ QUITMOV AH, 0EINT 10JMP AGAIN

QUIT: INT 20MES DB ‘type any letter, number or punctuation key’

DB ‘any F1 to F10 to end the program”DB 0d,0a,0a,’$’

Page 19: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

19

Data Transfer Instructions - MOV

None(D) (S)MOV D, SMoveMOV

Flags Affected

OperationFormatMeaningMnemonic

Seg regMemory Seg regReg 16Mem16Seg regReg16Seg regImmediateMemoryImmediateRegisterRegisterMemory MemoryRegisterRegisterRegisterMemoryAccumulatorAccumulatorMemorySourceDestination

Direct memory to memory

is not allowed

Page 20: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

20

Data Transfer Instructions - XCHG

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

Flags Affected

OperationFormatMeaningMnemonic

MemoryRegister RegisterRegisterRegisterMemory Reg16AccumulatorSourceDestination

Example. XCHG [1234h], BX

Page 21: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

21

Data Transfer Instructions – LEA, LDS, LES

An important type of data transfer operation is loading a segment and a general purpose register with an address directly from memory

Flags Affected

OperationFormatMeaningMnemonic

None(Mem32) (Reg16)(Mem32 + 2) (ES)

LES Reg16, MEM32

Load Register and ES

LES

None(Mem32) (Reg16)(Mem32 + 2) (DS)

LDS Reg16, MEM32

Load Register and DS

LDS

NoneEA (Reg16)LEA Reg16,EALoad Effective Address

LEA

Example. LEA SI, DATA or MOV SI, OFFSET DATA

Page 22: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

22

Arithmetic Instructions – ADD, ADC, INC, AAA, DAA

Flags Affected

OperationFormatMeaningMnemonic

DAADecimal adjust for addition

DAA

AAAASCII adjust for addition

AAA

All but the carry flag

(D) + 1 (D)

INC DIncrement by one

INC

All(S) + (D) + (CF) (D)Carry (CF)

ADC D, SAdd with carry

ADC

All(S) + (D) (D)

Carry (CF)

ADD D, SAdditionADD

Page 23: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

23

Examples

Ex. 1 ADD AX, 2ADC AX, 2

Ex. 2 AL contains 32 (ASCII code for number 2)BL contains 34 (ASCII code for number 4) MOV AH,0ADD AL, BLAAA

; has to be adjusted via AL and only an addition instruction

Ex. 3 AL contains 25 (packed BCD)BL contains 56 (packed BCD)

ADD AL, BLDAA

Page 24: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

24

Program Example

Program 3.2:

Write a program that adds the following two multiword numbers

DATA1= 548fb9963ce7hDATA2 = 3fcd4fa23b8dh

Page 25: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

25

Example Cont’d DATA section

.MODEL SMALL

.STACK 64

.DATADATA1 DQ 548F9963CE7h

ORG 0010hDATA2 DQ 3FCD4FA23B8Dh

ORG 0020hDATA3 DQ ?

Page 26: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

26

Example Cont’d CODE section.CODE

MAIN PROC FARMOV AX,@DATAMOV DS,AXCLCMOV SI,OFFSET DATA1MOV DI,OFFSET DATA2MOV BX,OFFSET DATA3MOV CX,04

BACK: MOV AX,[SI]ADC AX,[DI]MOV [BX],AXADD SI,2ADD DI,2ADD BX,2LOOP BACKMOV AH,4ChINT 21h

MAIN ENDPEND MAIN

INC SIINC SIINC DIINC DIINC BXINC BX

Page 27: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

27

Arithmetic Instructions – SUB, SBB, DEC, AAS, DAS, NEG

NEG DNegateNEG

Flags AffectedOperationFormatMeaningMnemonic

AASASCII adjust for subtraction

AAS

DASDecimal adjust for subtraction

DAS

All but the carry flag

(D) - 1 (D)INC DDecrement by one

DEC

All(D) - (S) -(CF) (D)

SBB D, SSubtract with borrow

SBB

All(D) - (S) (D)Borrow (CF)

SUB D, SSubtractSUB

Page 28: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

28

Example• 32-bit subtraction of two 32 bit numbers X and Y that are stored in

the memory as– X = (DS:203h)(DS:202h)(DS:201h)(DS:200h)– Y = (DS:103h)(DS:102h)(DS:101h)(DS:100h)

• The result X - Y to be stored where X is saved in the memory

MOV SI, 200hMOV DI, 100hMOV AX, [SI]SUB AX, [DI]MOV [SI], AX ;save the LS word of resultMOV AX, [SI] +2SBB AX, [DI]+2MOV [SI] +2, AX

Ex. 12 34 56 78 – 23 45 67 89 = EF EE EE EE

Page 29: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

29

Multiplication and Division

EDX :EAXRegister or Memory

EAXDword * Dword

DX :AXRegister or memory

AXWord * Word

AXRegister or memory

ALByte * Byte

ResultOperand (Multiplier)

MultiplicantMultiplication(MUL or IMUL)

EAX : EDXRegister or MemoryEDX: EAXQword / Dword

AX : DXRegister or memoryDX:AXDword / Word

AL : AHRegister or memoryAXWord / Byte

Quotient : RemainderOperand (Divisor)

DividendDivision(DIV or IDIV)

Page 30: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

30

AAM, AAD, CBW, CWD• AAM: Adjust AX for multiply• AAD: Adjust AX for divide

MOV AL,07 ; first unpacked numberMUL AL, 09 ; second unpacked numberAAM ;AX should have 06 03

• Division instructions can also be used to divide an 8 bit dividend in AL by an 8 bit divisor.– In order to do so, the sign of the dividend must be extended to fill the AX

register– AH is filled with zeros if AL is positive– AH is filled with ones if the number in AL is negative

• Automatically done by executing the CBW (convert byte to word) instruction

• CWD (convert word to double word)Ex. MOV AL, 0A1h

CBWCWD

Page 31: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

31

Example

DATA DB +13,-10,+19,+14,-18 ;0d,f6,13,0e,eeMOV CX,5 ;LOAD COUNTERSUB BX, BX ;CLEAR BX, MOV SI, OFFSET DATA ;SET UP POINTER

BACK: MOV AL,[SI] ;MOVE BYTE INTO ALCBW ;SIGN EXTEND INTO AXADD BX, AX ;ADD TO BXINC SI ;INCREMENT POINTERDEC CX ;DECREMENT COUNTERJNZ BACK ;LOOP IF NOT FINISHEDMOV AL,5 ;MOVE COUNT TO ALCBW ;SIGN EXTEND INTO AXMOV CX,AX ;SAVE DENOMINATOR IN CXMOV AX,BX ;MOVE SUM TO AXCWD ;SIGN EXTEND THE SUMIDIV CX ;FIND THE AVERAGE

• Write a program that calculates the average of five temperatures and writes the result in AX

Page 32: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

32

Compare (CMP)

Write a program to find the highest among 5 grades and write it in DL

DATA DB 51, 44, 99, 88, 80 ;13h,2ch,63h,58h,50hMOV CX,5 ;set up loop counterMOV BX, OFFSET DATA ;BX points to GRADE dataSUB AL,AL ;AL holds highest grade found so

; farAGAIN: CMP AL,[BX] ;compare next grade to highest

JA NEXT ;jump if AL still highestMOV AL,[BX] ;else AL holds new highest

NEXT: INC BX ;point to next gradeLOOP AGAIN ;continue searchMOV DL, AL

For ex: CMP CL,BL ; CL-BL; no modification on neither operands

Page 33: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

33

Logical Instructions

• AND– Uses any addressing mode except memory-to-memory and segment

registers– Especially used in clearing certain bits (masking)

• xxxx xxxx AND 0000 1111 = 0000 xxxx (clear the first four bits)– Examples: AND BL, 0FH; AND AL, [345H]

• OR– Used in setting certain bits

• xxxx xxxx OR 0000 1111 = xxxx 1111

• XOR – Used in inverting bits

• xxxx xxxx XOR 0000 1111 = xxxx yyyy

• Ex. Clear b’ts 0 and 1, set bits 6 and 7, invert bit 5

AND CX, OFCH 1111 1100OR CX, 0C0H 1100 0000XOR CX, 020H 0010 0000

Page 34: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

34

TEST

• TEST instruction performs the AND operation but it does not change the destination operand as in AND but only the flags register.

• Similar to CMP but it tests a single bit or occasionally multiple bits. • Ex. TEST DL, DH ; TEST EAX, 256

• Bit Test Instructions (80386 thru Pentium 2) BT/BTC/BTR/BTS– BT AX,4 tests bit position 4 in AX and result is located in the carry flag,

C = 1 if bit 4 is 1, 0 otherwise

TEST AL, 1 ; test right bitJNZ RIGHT ; if setTEST AL, 128 ; test left bitJNZ LEFT ; if set

BTS CX, 9 ; tests and sets bit 9BTR CX, 10 ; tests and resets bit 10BTC CX, 12 ; tests and complements bit 12

Page 35: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

35

Shift

C

C

Target register or memory

C

C

0

0

0

Sign Bit

SHL

SAL

SHR

SAR

Page 36: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

36

Examples

; Multiply AX by 10SHL AX, 1MOV BX, AXMOV CL,2SHL AX,CLADD AX, BX

Ex.

Examples SHL AX,1SAL DATA1, CL ; shift count is a modulo-32 count

Ex. What are the results of SAR CL, 1 if CL initially contains B6H?

Ex. What are the results of SHL AL, CL if AL contains 75H and CL contains 3?

Page 37: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

37

Rotate

C

C

Target register or memory

C

C

RCL

ROL

RCR

ROR

Ex. What is the result of ROL byte ptr [SI], 1 if memory location 3C020 contains 41H?

Page 38: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

38

Write a program that counts the number of 1’s in a byte and writes it into BL

DATA1 DB 97 ; 61hSUB BL,BL ;clear BL to keep the number of 1sMOV DL,8 ;rotate total of 8 timesMOV AL,DATA1

AGAIN: ROL AL,1 ;rotate it onceJNC NEXT ;check for 1INC BL ;if CF=1 then add one to count

NEXT: DEC DL ;go through this 8 timesJNZ AGAIN ;if not finished go backNOP

Example

Page 39: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

39

BCD and ASCII Numbers

• BCD (Binary Coded Decimal)– Unpacked BCD: One byte per digit– Packed BCD: 4 bits per digit (more efficient in storing data)

• ASCII to unpacked BCD conversion– Keyboards, printers, and monitors all use ASCII.– Digits 0 to 9 are represented by ASCII codes 30 – 39.

• Example. Write an 8086 program that displays the packed BCD number in register AL on the system video monitor– The first number to be displayed should be the MSD– It is found by masking the LSD and then rotating the MSD into the LSD

position– The result is then converted to ASCII by adding 30h– The BIOS video service is then called

Page 40: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

40

ProgramMOV BL,ALAND AL,F0HMOV CL,4ROR AL,CLADD AL,30HMOV AH,0EHINT 10H

MOV AL,BLAND AL,0FHADD AL,30HINT 10HINT 20H ; RETURN TO DOS

Page 41: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

41

Example

• Write an 8086 program that adds two packed BCD numbers input from the keyboard and computes and displays the result on the system video monitor

• Data should be in the form 64+89= The answer 153 should appear in the next line.

= 98+46?#

1 2 3 4 5 6

Page 42: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

42

Example Continued

sub byte ptr[si+2], 30hsub byte ptr[si+3], 30hsub byte ptr[si+5], 30hsub byte ptr[si+6], 30h

Mov cl,4Rol byte ptr [si+3],clRol byte ptr [si+6],clRor word ptr [si+5], clRor word ptr [si+2], cl

Mov al, [si+3]Add al, [si+6]DaaMov bh,alJnc tempMov al,1Call displaytemp: Mov al,bhCall displayInt 20

Mov dx, buffer_addressMov ah,0aMov si,dxMov byte ptr [si], 8Int 21Mov ah,0ehMov al,0ahInt 10 ; BIOS service 0e line feed position cursor

Page 43: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

43

Flag Control Instructions

• LAHF Load AH from flags (AH) ← (Flags)• SAHF Store AH into flags (Flags) ← (AH)

– Flags affected: SF, ZF, AF, PF, CF• CLC Clear Carry Flag (CF) ← 0• STC Set Carry Flag (CF) ← 1• CLI Clear Interrupt Flag (IF) ← 0• STI Set interrupt flag (IF) ← 1• Example (try with debug)

LAHFMOV [MEM1], AHMOV ah, [MEM2]SAHF; MEM1 = 150 (FF) MEM2 = 151 (01)

SF ZF AF PF CF

Page 44: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

44

Jump Instructions

• Unconditional vsconditional jump

Example. JMP [BX]Assume BX = 1000h Ds:1000 = 200h

Page 45: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

45

Compare

Unsigned Comparison

01Dest < source

10Dest = source

00Dest > source

ZFCFCompOperands

Signed Comparison

SF<>OF?Dest < source

?1Dest = source

SF=OF0Dest > source

SF,OFZFCompOperands

Page 46: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

46

Compare Example

DATA1 DW 235Fh…

MOV AX, CCCCHCMP AX, DATA1JNC OVERSUB AX,AXOVER: INC DATA1

CCCC – 235F = A96D => Z=0, CF=0 => CCCC > DATA1

Page 47: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

47

Subroutines and Subroutine Handling Functions

A subroutine is a special segment of a program that can be called for execution from any point in the program

A RET instruction must be included at the end of the subroutine to initiate the return sequence to the main program environment

Examples. Call 1234hCall BXCall [BX]

Two calls•intrasegment•intersegment

Page 48: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

48

Calling a NEAR proc

The CALL instruction and the subroutine it calls are in the same segment.

Save the current value of the IP on the stack.

Then load the subroutine’s offset into IP.

Calling Program Subroutine Stack

Main proc sub1 proc001A: call sub1 0080: mov ax,1001D: inc ax …. retMain endp sub1 endp (not used)1fff

001ffe

1D1ffd

Page 49: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

49

The CALL instruction and the subroutine it calls are in the “Different” segments.

Save the current value of the CS and IP on the stack.

Then load the subroutine’s CS and offset into IP.

Calling Program Subroutine Stack

Main proc sub1 proc1FCB:001A: call sub1 4EFA:0080: mov ax,11FCB:001D: inc ax ….

… …… retMain endp sub1 endp N/A1fff

Calling a FAR proc

CB1ffb

1F1ffc

001ffe

1D1ffd

Page 50: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

50

Nested Procedure Calls

A subroutine may itself call other subroutines.

Example:main proc

000A call sub1000C mov ax,……

main endp

subr1 proc0030 nop

…call subr2

0040 ret …

subr1 endp

subr2 proc0050 nop

…call subr3

0060 ret …

subr2 endp

subr1 proc0070 nop

…0079 nop007A ret

subr1 endp

Do NOT overlap Procedure Declarations

Q: show the stack contents

at 0079?

X1fff

001ffa

401ffb

001ffc

0c1ffd

601ff0

X1fff

001ffe

Page 51: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

51

Push and Pop Instructions

Push S (16/32 bit or Mem)(SP) ← (SP) -2((SP)) ← (S)

Pop D (16/32 bit or Mem)(D) ← ((SP))(SP) ← (SP) + 2

Page 52: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

52

Loop and Loop Handling Instructions

Page 53: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

53

Loop

Page 54: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

54

Nested Loops

MOV CX,ABACK: …………LOOP BACK

MOV CX,AOUTER: NOP

MOV CX, 99INNER: NOP

………LOOP INNERNOPLOOP OUTER

MOV CX,AOUTER: PUSH CX

MOV CX, 99INNER: NOP

………LOOP INNERPOP CXLOOP OUTER

Nested Loops

Page 55: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

55

String Instructions

80x86 is equipped with special instructions to handle string operationsString: A series of data words (or bytes) that reside in consecutive memory locationsOperations: move, scan, compare

String Instruction: Byte transfer, SI or DI increment or decrement by 1Word transfer, SI or DI increment or decrement by 2DWord transfer, SI or DI increment or decrement by 4

Page 56: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

56

String Instructions - D Flag

The Direction Flag: Selects the auto increment D=0 or the auto decrement D=1 operation for the DI and SI registers during string operations. D is used only with strings

CLD Clears the D flag / STD Sets the D flag

Page 57: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

57

String Instructions

Page 58: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

58

Repeat String REP

Basic string operations must be repeated in order to process arrays of data; this is done by inserting a repeat prefix.

Page 59: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

59

Example. Find and replace

• Write a program that scans the name “Mr.Gohns” and replaces the “G” with the letter “J”.Data1 db 'Mr.Gones','$‘.code mov es,ds cld ;set auto increment bit D=0mov di, offset data1mov cx,09; number of chars to be scannedmov al,'G'; char to be compared againstrepne SCASB; start scan AL =? ES[DI]jne Over; if Z=0dec di; Z=1mov byte ptr[di], 'J'

Over: mov ah,09mov dx,offset data1int 21h; display the resulting String

Search.exe

search.asm

Page 60: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

60

Strings into Video Buffer

PUSH AxCLDMOV AX,0B800HMOV ES,AXMOV DI,0MOV CX,2000HPOP AxMOV AL,20hREP STOSW

Clear.exeFill the Video Screen with a value

Page 61: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

61

Example. Display the ROM BIOS Date

• Write an 8086 program that searches the BIOS ROM for its creation date and displays that date on the monitor.

• If a date cannot be found display the message “date not found”• Typically the BIOS ROM date is stored in the form xx/xx/xx

beginning at system address F000:FFF5 • Each character is in ASCII form and the entire string is terminated

with the null character (00)• First we scan the null character, if the null character is found in the

first eight characters, an invalid date is assumed• If not, the entire string is copied into RAM• Add a ‘$’ character to the end of the string and make it ready for

DOS function 09, INT 21

Page 62: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

62

Date.exe

date.asm

Page 63: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

63

Example. Binary (Hex) to ASCII (Decimal) Conversion

• CONVERSION PROCEDURE WITH AN EXAMPLE• 34Dh = 3 x 256 + 4 x 16 + 13 x 1 = 845• 34Dh / A = 84 remainder 5• 84h / A = 8 remainder 4• 8 < A the process stops • Taking the remainders in reverse order gives : 845 decimal

Write a program to convert a word sized hex number in data itemBINNUM

The result will be five digits, each digit will be converted to ASCII and placed in ASCNUM, the lowest digit will be in high memory as theconvention of ASCII storage in DOS

Page 64: Week 4 8088/8086 Microprocessor Programming Iumartalha.weebly.com/uploads/8/3/8/2/8382263/week45.pdf · 8088/8086 Microprocessor Programming I. 2 MASM LINK or myfile.exe TLINK Link

64

ProgramBINNUM DW 34DhASCNUM DB 5 DUP(‘0’).CODEMOV BX, 10 MOV SI OFFSET ASCNUMADD SI, 5DEC SIMOV AX,BINNUM

BACK: SUB DX, DXDIV BX; Dword division DX:AX / BX = AX ; rem DLOR DL,30hMOV [SI], DL DEC SICMP AX,0JA BACKINT 20h