Week /8086 Microprocessor Programming I

Similar documents
Arithmetic and Logic Instructions And Programs

Week /8086 Microprocessor Programming II

8088/8086 Programming Integer Instructions and Computations

Instructions moving data

Mnem. Meaning Format Operation Flags affected ADD Addition ADD D,S (D) (S)+(D) (CF) Carry ADC Add with ADC D,C (D) (S)+(D)+(CF) O,S,Z,A,P,C

Week /8086 Microprocessor Programming

Arithmetic Instructions

8086 Programming. Multiplication Instructions. Multiplication can be performed on signed and unsigned numbers.

Kingdom of Saudi Arabia Ministry of Higher Education. Taif University. Faculty of Computers & Information Systems

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

3.1 DATA MOVEMENT INSTRUCTIONS 45

EC 333 Microprocessor and Interfacing Techniques (3+1)

Intel 8086: Instruction Set

Q1: Multiple choice / 20 Q2: Memory addressing / 40 Q3: Assembly language / 40 TOTAL SCORE / 100

Logic Instructions. Basic Logic Instructions (AND, OR, XOR, TEST, NOT, NEG) Shift and Rotate instructions (SHL, SAL, SHR, SAR) Segment 4A

complement) Multiply Unsigned: MUL (all operands are nonnegative) AX = BH * AL IMUL BH IMUL CX (DX,AX) = CX * AX Arithmetic MUL DWORD PTR [0x10]

Ex : Write an ALP to evaluate x(y + z) where x = 10H, y = 20H and z = 30H and store the result in a memory location 54000H.

Defining and Using Simple Data Types

Integer Arithmetic. Pu-Jen Cheng. Adapted from the slides prepared by Kip Irvine for the book, Assembly Language for Intel-Based Computers, 5th Ed.

APPENDIX C INSTRUCTION SET DESCRIPTIONS

Lecture (07) x86 programming 6

Assignment no:4 on chapter no :3 : Instruction set of 8086

8086 INSTRUCTION SET

Signed number Arithmetic. Negative number is represented as

EEM336 Microprocessors I. Arithmetic and Logic Instructions

16.317: Microprocessor Systems Design I Fall 2013

Q1: Multiple choice / 20 Q2: Protected mode memory accesses

Mr. Sapan Naik 1. Babu Madhav Institute of Information Technology, UTU

16.317: Microprocessor Systems Design I Spring 2014

UNIT III MICROPROCESSORS AND MICROCONTROLLERS MATERIAL OVERVIEW: Addressing Modes of Assembler Directives. Procedures and Macros

We will first study the basic instructions for doing multiplications and divisions

Computer Architecture and System Programming Laboratory. TA Session 3

ECOM Computer Organization and Assembly Language. Computer Engineering Department CHAPTER 7. Integer Arithmetic

ASSEMBLY LANGUAGE PROGRAMMING OF THE MICROCOMPUTER

Ex: Write a piece of code that transfers a block of 256 bytes stored at locations starting at 34000H to locations starting at 36000H. Ans.

CONTENTS. 1. Display a Message Display a one Digit Number Accept a Character from keyboard and display the character 4

Chapter 7 Integer Arithmetic

Basic Pentium Instructions. October 18

8086 ASSEMBLY LANGUAGE PROGRAMMING

Computer Architecture 1 ح 303

Lecture 9. INC and DEC. INC/DEC Examples ADD. ADD Examples SUB. INC adds one to a single operand DEC decrements one from a single operand

Computer Department Chapter 7. Created By: Eng. Ahmed M. Ayash Modified and Presented by: Eng. Eihab S. El-Radie. Chapter 7

Integer Arithmetic. Shift and rotate. Overview. Shift and Rotate Instructions

Integer Arithmetic. Computer Organization and Assembly Languages Yung-Yu Chuang 2005/11/17. with slides by Kip Irvine

Lecture 9. INC and DEC. INC/DEC Examples ADD. Arithmetic Operations Overflow Multiply and Divide

Basic Assembly SYSC-3006

Babu Madhav Institute of Information Technology, UTU

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB

UNIT II 16 BIT MICROPROCESSOR INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING. The Intel 8086 Instruction Set

CS-202 Microprocessor and Assembly Language

Lecture (08) x86 programming 7

Chapter 5. Real-Mode 80386DX Microprocessor Programming 1 Part 2. The 80386, 80486, and Prentium Processors,Triebel Prof. Yan Luo, UMass Lowell 1

1-Operand instruction types 1 INC/ DEC/ NOT/NEG R/M. 2 PUSH/ POP R16/M16/SR/F 2 x ( ) = 74 opcodes 3 MUL/ IMUL/ DIV/ DIV R/M

Q1: Multiple choice / 20 Q2: Data transfers and memory addressing

Chapter Four Instructions Set

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

Code segment Stack segment

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

Hands on Experience for Faculty in Laboratories Phase I JNTUK, Kakinada

Logical and bit operations

22 Assembly Language for Intel-Based Computers, 4th Edition. 3. Each edge is a transition from one state to another, caused by some input.

Computer Architecture and Assembly Language. Practical Session 3

Chapter 12. Selected Pentium Instructions

EXPERIMENT - 1: ADDITION & SUBTRACTION


PHI Learning Private Limited

Assembly Language Each statement in an assembly language program consists of four parts or fields.

Integer Arithmetic Part2

Summer 2003 Lecture 4 06/14/03

Logical and Bit Operations. Chapter 8 S. Dandamudi

Introduction to 8086 Assembly

CS401 Assembly Language Solved MCQS From Midterm Papers

Marking Scheme. Examination Paper Department of CE. Module: Microprocessors (630313)

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

Microprocessor & Computer Architecture / Lecture

A4 Sample Solution Ch3

Assembler lecture 4 S.Šimoňák, DCI FEEI TU of Košice

Lesson 1. Fundamentals of assembly language

PESIT Bangalore South Campus

EECE.3170: Microprocessor Systems Design I Spring 2016

Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION 80x86 Instructions

Q1: Define a character string named CO_NAME containing "Internet Services" as a constant?

b) List the 16 Bit register pairs of 8085?(Any 2 pair, 1 Mark each) 2M Ans: The valid 16 bit register pair of 8085 are

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Electronics and Communication

Chapter 3: Addressing Modes

8086 INTERNAL ARCHITECTURE

Basic Intel x86 Assembly Language Programming

9/25/ Software & Hardware Architecture

db "Please enter up to 256 characters (press Enter Key to finish): ",0dh,0ah,'$'

IBM PC Hardware CPU 8088, Pentium... ALU (Arithmetic and Logic Unit) Registers. CU (Control Unit) IP.

if 2 16bit operands multiplied the result will be

ADVANCE MICROPROCESSOR & INTERFACING

L1 Remember, L2 Understand, L3 - Apply, L4 Analyze, L5 Evaluate, L6 Create

UNIT 4. Modular Programming

Scott M. Lewandowski CS295-2: Advanced Topics in Debugging September 21, 1998

Reverse Engineering II: Basics. Gergely Erdélyi Senior Antivirus Researcher

Adding Binary Integers. Part 5. Adding Base 10 Numbers. Adding 2's Complement. Adding Binary Example = 10. Arithmetic Logic Unit

CS401 Assembly Language Solved Subjective MAY 03,2012 From Midterm Papers. MC

Reverse Engineering II: The Basics

Reverse Engineering II: The Basics

Transcription:

Week 4 8088/8086 Microprocessor Programming I

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 continue 3. 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 2

Example continued AGAIN: MOV AH,0 INT 16 CMP AL,00 JZ QUIT MOV AH, 0E INT 10 JUMP AGAIN QUIT: INT 20 3

Example continued with sign-on messages MOV DX, OFFSET MES MOV AH,09 INT 21 AGAIN: MOV AH,0 INT 16 CMP AL,00 JZ QUIT MOV AH, 0E INT 10 JUMP AGAIN QUIT: INT 20 MES DB type any letter, number or punctuation key DB any F1 to F10 to end the program DB 0d,0a,0a, $ 4

Data Transfer Instructions - MOV Mnemonic Meaning Format Operation Flags Affected MOV Move MOV D, S (D) (S) None Destination Memory Accumulator Register Register Memory Register Memory Seg reg Seg reg Reg 16 Memory Source Accumulator Memory Register Memory Register Immediate Immediate Reg16 Mem16 Seg reg Seg reg Memory to memory is not allowed 5

Data Transfer Instructions - XCHG Mnemonic Meaning Format Operation Flags Affected XCHG Exchange XCHG D,S (D) (S) None Destination Accumulator Memory Register Register Source Reg16 Register Register Memory Example. XCHG [1234h], BX 6

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 Mnemonic Meaning Format Operation Flags Affected LEA Load Effective Address LEA Reg16,EA EA (Reg16) None LDS Load Register and DS LDS Reg16, MEM32 (Mem32) (Reg16) (Mem32 + 2) None (DS) LES Load Register and ES LES Reg16, MEM32 (Mem32) (Reg16) (Mem32 + 2) None (ES) Example. LDS SI, [200h] 7

Arithmetic Instructions ADD, ADC, INC, AAA, DAA Mnemonic Meaning Format Operation Flags Affected ADD Addition ADD D, S (S) + (D) (D) All Carry (CF) ADC Add with carry ADC D, S (S) + (D) + (CF) (D) All Carry (CF) INC Increment by one INC D (D) + 1 (D) All but the carry flag AAA ASCII adjust for addition AAA DAA Decimal adjust for addition DAA 8

Examples Ex. 1 ADD AX, 2 ADC AX, 2 Ex. 2 AL contains 32 (ASCII code for number 2) BL contains 34 (ASCII code for number 4) ADD AL, BL AAA ; 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, BL DAA 9

Arithmetic Instrutions SUB, SBB, DEC, AAS, DAS, NEG Mnemonic Meaning Format Operation Flags Affected SUB Subtract SUB D, S (D) - (S) (D) All Borrow (CF) SBB Subtract with borrow SBB D, S (D) - (S) - (CF) (D) All DEC Decrement by one INC D (D) - 1 (D) All but the carry flag NEG Negate NEG D DAS Decimal adjust for subtraction AAA AAS ASCII adjust for subtraction DAA 10

Example 32-bit subtraction of 2 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, 200h MOV DI, 100h MOV AX, [SI] SUB AX, [DI] MOV [SI], AX ;save the LS word of result MOV AX, [SI] +2 SBB AX, [DI]+2 MOV [SI] +2, AX Ex. 12 34 56 78 23 45 67 89 = EF EE EE EE 11

Multiplication and Division Multiplication Multiplicant Operand Result (MUL or IMUL) (Multiplier) Byte * Byte AL Register or memory AX Word * Word AX Register or memory DX :AX Dword * Dword EAX Register or Memory EDX :EAX Division Dividend Operand Quotient : Remainder (DIV or IDIV) (Divisor) Word / Byte AX Register or memory AL : AH Dword / Word DX:AX Register or memory AX : DX Qword / Dword EDX: EAX Register or Memory EAX : EDX 12

AAM, AAD, CBW, CWD AAM: Adjust AX for multiply AAD: Adjust AX for divide MOV AL,07 ; first unpacked number MUL AL, 09 ; second unpacked number AAM ;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 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 CBW CWD 13

Example Write a program that calculates the average of five temperatures and writes the result in AX DATA DB +13,-10,+19,+14,-18 ; 0d, f6,13,0e, ee MOV CX,5 ;LOAD COUNTER SUB BX,BX ;CLEAR BX, USED AS ACCUMULATOR MOV SI,OFFSET SIGN_DAT ;SET UP POINTER BACK: MOV AL,[SI] ;MOVE BYTE INTO AL CBW ;SIGN EXTEND INTO AX ADD BX,AX ;ADD TO BX INC SI DEC CX ;INCREMENT POINTER JNZ BACK ;LOOP IF NOT FINISHED MOV AL,5 ;MOVE COUNT TO AL CBW ;SIGN EXTEND INTO AX MOV CX,AX ;SAVE DENOMINATOR IN CX MOV AX,BX ;MOVE SUM TO AX CWD ;SIGN EXTEND THE SUM IDIV CX ;FIND THE AVERAGE 14

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,50h MOV CX,5 ;set up loop counter MOV BX, OFFSET DATA ;BX points to GRADE data SUB AL,AL ;AL holds highest grade found so far AGAIN: CMP AL,[BX] ;compare next grade to highest JA NEXT ;jump if AL still highest MOV AL,[BX] ;else AL holds new highest NEXT: INC BX ;point to next grade LOOP AGAIN ;continue search MOV DH, AL 15

Logical Instructions AND OR XOR 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] Used in setting certain bits xxxx xxxx OR 0000 1111 = xxxx 1111 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 1100 OR CX, 0C0H 1100 0000 XOR CX, 020H 0010 0000 16

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 bit it tests a single bit or occasionally multiple bits. Ex. TEST DL, DH ; TEST EAX, 256 TEST AL, 1 ; test right bit JNZ RIGHT ; if set TEST AL, 128 ; test left bit JNZ LEFT ; if set Bit Test Instructions (80386 thru Pentium 2) BT/BTC/BTR/BTS BT AX,4 tests bit position 4 in AX and result is loacted in the carry flag, C = 1if bit 4 is 1, 0 otherwise BTS CX, 9 ; tests and sets bit 9 BTR CX, 10 ; tests and resets bit 10 BTC CX, 12 ; tests and complements bit 12 17

Shift C Target register or memory SHL 0 SAL C 0 C SHR 0 SAR C Sign Bit 18

Examples Examples Ex. Ex. Ex. SHL AX,1 SHR BX,12 SAL DATA1, CL ; shift count is a modulo-32 count SAR EDX, 14 ; Multiply AX by 10 SHL AX, 1 MOV BX, AX SHL AX,2 ADD AX, BX What are the results of SAR CL, 1 if CL initially contains B6H? What are the results of SHL AL, CL if AL contains 75H and CL contains 3? 19

Rotate C Target register or memory RCL ROL C C RCR ROR C Ex. What is the result of ROL byte ptr [SI], 1 if memory location 3C020 contains 41H? 20

Example ;write a program that counts the number of 1 s in a byte and writes it into BL DATA1 DB 97 ; 61h SUB BL,BL ;clear BL to keep the number of 1s MOV DL,8 ;rotate total of 8 times MOV AL,DATA1 AGAIN: ROL AL,1 ;rotate it once JNC NEXT ;check for 1 INC BL ; if CF=1 then add one to count NEXT: DEC DL ;go through this 8 times JNZ AGAIN ;if not finished go back NOP 21

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 andthen rotating the MSD into the LSD position The result is then converted to ASCII by adding 30h The BIOS video service is then called 22

Program Mov bl,al And al,f0h Mov cl,4 Ror al,cl Add al,30h Mov ah,0eh Int 10h Mov al,bl And al,0fh Add al,30h Int 10h Int 20h ; return to DOS 23

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. Mov dx, buffer address Mov ah,0a Mov si,dx Mov byte ptr [si], 8 Int 21 Mov ah,0eh Mov al,0ah Int 10 ; BIOS service 0e line feed position cursor 24

Example Continued sub byte ptr[si+2], 30h sub byte ptr[si+3], 30h sub byte ptr[si+5], 30h sub byte ptr[si+6], 30h Mov cl,4 Rol byte ptr [si+3],cl Rol byte ptr [si+6],cl Ror word ptr [si+2], cl Ror word ptr [si+2], cl Mov al, [si+3] Add al, [si+6] Daa Mov bh,al Jnc display Mov al,1 Call display Mov al,bh Call display Int 20 25