Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 05: Assembly Language Programming (2). The 80x86 IBM PC and Compatible Computers Chapter 3 Arithmetic & Logic Instructions and Programs Chapter.

Similar presentations


Presentation on theme: "Lecture 05: Assembly Language Programming (2). The 80x86 IBM PC and Compatible Computers Chapter 3 Arithmetic & Logic Instructions and Programs Chapter."— Presentation transcript:

1 Lecture 05: Assembly Language Programming (2)

2 The 80x86 IBM PC and Compatible Computers Chapter 3 Arithmetic & Logic Instructions and Programs Chapter 6 Signed Numbers, Strings, and Tables

3 Arithmetic Instructions zAddition zSubtraction zMultiplication zDivision

4 Unsigned Addition zADD dest, src ;dest = dest + src yDest can be a register or in memory ySrc can be a register, in memory or an immediate yNo mem-to-mem operations in 80X86 yChange ZF, SF, AF, CF, OF, PF zADC dest, src ;dest = dest + src +CF yFor multi-byte numbers yIf there is a carry from last addition, adds 1 to the result

5 Addition Example of Individual Bytes Draw the layout of the data segment in memory What’s this for?

6 Addition Example of Multi-byte Nums

7 Unsigned Subtraction zSUB dest, src ;dest = dest - src yDest can be a register or in memory ySrc can be a register, in memory or an immediate yNo mem-to-mem operations in 80X86 yChange ZF, SF, AF, CF, OF, PF zSBB dest, src ;dest = dest - src – CF yFor multi-byte numbers yIf there is a borrow from last subtraction, subtracts 1 from the result

8 Subtraction Example of Individual Bytes zCPU carries out 1.take the 2’s complement of the src 2.add it to the dest 3.invert the carry After these three steps, if zCF = 0: positive result; zCF = 1: negative result, left in 2’s complement yMagnitude: NOT + INC (if a programmer wants the magnitude)

9 Subtraction Example of Multi-byte Nums

10 Unsigned Multiplication zMUL operand zbyte X byte: yOne implicit operand is AL, the other is the operand, result is stored in AX zword X word: yOne implicit operand is AX, the other is the operand, result is stored in DX & AX zword X byte: yAL hold the byte, AH = 0, the word is the operand, result is stored in DX & AX;

11 Unsigned Multiplication Example

12 Unsigned Division zDIV denominator yDenominator cannot be zero yQuotient cannot be too large for the assigned register zbyte / byte: yNumerator in AL, clear AH; quotient is in AL, remainder in AH zword / word: yNumerator in AX, clear DX; ; quotient is in AX, remainder in DX zword / byte: yNumerator in AX; quotient is in AL (max 0FFH), remainder in AH zdouble-word / word: yNumerator in DX, AX; quotient is in AX (max 0FFFFH), remainder in DX yDenominator can be in a register or in memory

13 Unsigned Division Example

14 Logic Instructions zAND zOR zXOR zNOT zLogical SHIFT zROTATE zCOMPARE

15 AND zAND dest, src yBit-wise logic ydest can be a register or in memory; src can be a register, in memory, or immediate

16 OR zOR dest, src yBit-wise logic ydest can be a register or in memory; src can be a register, in memory, or immediate

17 XOR zXOR dest, src yBit-wise logic ydest can be a register or in memory; src can be a register, in memory, or immediate

18 NOT zNOT operand yBit-wise logic yOperand can be a register or in memory XNOT X ------------------------------------------- 1 0 0 1

19 Logical SHIFT zSHR dest, times ydest can be a register or in memory y0->MSB->…->LSB->CF yTimes = 1: SHR xx, 1 yTimes >1: MOV CL, times SHR xx, CL zSHL dest, times yAll the same except in reverse direction

20 Example: BCD & ASCII Numbers Conversion zBCD: Binary Coded Decimal yDigits 0~9 in binary representation yUnpacked, packed zASCII yCode for all characters that you can read or write yDigit characters ‘0’~’9’

21 ASCII -> Unpacked BCD Conversion zSimply remove the higher 4 bits “0011” zE.g., asc DB ‘3’ unpackDB ? ------------------------- MOV AH, asc AND AH, 0Fh MOV unpack, AH

22 ASCII -> Packed BCD Conversion zFirst convert ASCII to unpacked BCD zThen, combine two unpacked into one packed  E.g., asc DB ‘23’ unpackDB ? ------------------------- MOV AH, asc MOV AL, asc+1 AND AX, 0F0Fh MOV CL, 4 SHL AH, CL OR AH, AL MOV unpack, AH

23 ROTATE zROR dest, times ydest can be a register, in memory yTimes = 1: ROR xx, 1 yTimes >1: MOV CL, times ROR xx, CL zROL dest, times yAll the same except in reverse direction

24 ROTATE Cont. zRCR dest, times ydest can be a register, in memory yTimes = 1: RCR xx, 1 yTimes >1: MCV CL, times RCR xx, CL zRCL dest, times yAll the same except in reverse direction

25 COMPARE of Unsigned Numbers zCMP dest, src yFlags affected as (dest – src) but operands remain unchanged  E.g., CMP AL, 23 JA lable1 ; jump if above, CF = ZF = 0

26

27 COMPARE of Signed Numbers zCMP dest, src y Same instruction as the unsigned case y but different understanding about the numbers and therefore different flags checked

28

29 Quiz Given the ASCII table, write an algorithm to convert lowercase letters in a string into uppercase letters and implement your algorithm using 86 assembly language.

30 Answer to Quiz

31 XLAT Instruction & Look-up Tables zSelf-learning ypp. 189 in Chapter 6


Download ppt "Lecture 05: Assembly Language Programming (2). The 80x86 IBM PC and Compatible Computers Chapter 3 Arithmetic & Logic Instructions and Programs Chapter."

Similar presentations


Ads by Google