44
Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Embed Size (px)

Citation preview

Page 1: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Assembly Language Programming II

Week 5 – Overview of x86 assembly language programming II

Page 2: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Different Addressing modesMOV AL, BL Register addressing mode

MOV AL, 15H immediate

MOV AL, abc

MOV AL, [1234H]

direct

MOV AL, [SI] Register indirect

MOV AL, [BX+SI] Base plus index

MOV AL, [BX+4]

MOV AL, ARRAY[3]

Register relative

MOV AL, [BX+DI+4]

MOV AL, ARRAY[BX+DI]

Base relative plus index

2

Page 3: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

How to move the address of a variable to a register ?

• When using indirect addressing, such as– MOV AL, [SI]– SI is an address so how can we initialize SI?

• This is by the instruction called LEA (Load Effective Address)

• LEA is similar to x = &y in C++• Syntax of LEA:– LEA SI, ARRAY

3

Page 4: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Base-plus-index addressing mode• Move a byte or word between a register and the

memory location addressed by a base register (BP or BX) plus an index register (DI or SI)

• Physical address of the operand is obtained by adding a direct or indirect displacement to the contents of either base register BX or base pointer register BP and the current value in DS and SS, respectively.

• Eg MOV [BX+SI], AL• Move value in AL to a location (DS+BX+SI)• If BP is used then use SS instead of DS

4

Page 5: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Application of Base + index

• The base register (BX) often holds the beginning location of a memory array, while the index register (SI) holds the relative position of an element in the array

• Change the value of SI then you can access different elements in the array

5

Page 6: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Register relative addressing mode

• Move a byte or word between a register and memory location addressed by an index or base register plus a displacement

• Eg MOV AL, ARRAY[SI]• EA = value of SI + ARRAY• Physical address = EA + DS• Eg mov AX, [BX+4]• Eg mov AX, array[DI+3]• This is similar to the base-plus-index

6

Page 7: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Base relative plus index addressing mode• Transfers a byte or word between a register and

the memory location addressed by a base and an index register plus a displacement

• Combining the based addressing mode and the indexed addressing mode together

• Eg MOV AH, [BX+DI+4]• EA = value of BX + 4 + value of DI• Physical address = DS + EA • This can be used to access data stored as a 2-D

matrix• Eg mov AX, array[BX+DI]

7

Page 8: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Example• Select an instruction for each of the following

tasks:– Copy BL into CL

• Mov CL, BL– Copy DS into AX

• Mov AX, DSSuppose that DS=1100H, BX=0200H, LIST=0250H, and SI=0500H,

determine the address accessed by each of the following instructions:mov LIST[SI], DS (offset address: LIST + SI=0750H, PA = offset +DS = 11750H) mov CL, LIST[BX+SI] ( offset address: LIST+BX+SI = 0950H, PA = 11950H)mov CH, [BX+SI] (offset address: BX+SI = 0700H, PA = 11700H)

8

Page 9: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

String addressing mode

• The string instructions of the 8086 instruction set automatically use the source (SI) and destination index registers (DI) to specify the effective addresses of the source and destination operands, respectively.

• Instruction is MOVS• There is no operand after movs• Don’t need to specify the register but SI and

DI are being used during the program execution

9

Page 10: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Port addressing mode• Port addressing is used in conjunction with IN and OUT

instruction to access input and output ports. • Any of the memory addressing modes can be used for the

port address for memory-mapped ports.• For ports in the I/O address space, only the direct addressing

mode and an indirect addressing mode using DX are available• Eg IN AL, 15H ; second operand is the port number• Input data from the input port at address 1516 of the I/O

address space to register AL• Eg IN AL, DX• Load AL with port number stored in DX

10

Page 11: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Exercises1. Compute the physical address for the specified operand in each of the

following instructions:

MOV [DI], AX (destination operand)

[DI] refers to the address 0200 so PA = 0B200H

MOV DI, [SI] (source operand)

[SI] refers to 0100 so PA = 0B100H)

MOV XYZ[DI], AH (destination operand)

XYZ[DI] refers to 0400+0200, PA = 0B600H

Given CS=0A00, DS=0B00, SI=0100, DI=0200,

BX=0300, XYZ=0400

11

Page 12: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Exercises2. Express the decimal numbers that follows as unpacked and packed BCD bytes

(BCD – binary coded decimal)

a. 29 b. 88

Ans. unpacked BCD takes 1 byte per-digit so 29 becomes

00000010 00001001 as packed 00101001

3. How would the BCD numbers be stored in memory starting at address 0B000

Ans. if it is packed BCD then 29 will occupy one byte at 0B000, if as unpacked BCD then it occupies 2 bytes 00000010 at 0B001 and 00001001 at 0B000)

12

Page 13: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Instruction set• A total of 117 basic instructions• 5 groups: data transfer, arithmetic, logic, shift, and

rotate• Data transfer instructions – to move data either

between its internal registers or between an internal register and a storage location in memory

• Cannot move data from one memory location to another memory location

• When the segment register is used as an operand then the move instruction must apply to a 16-bit data

• Mov SS, data ; then data must be 16-bit

13

Page 14: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Move instruction Mnemonic Meaning Format Operation Flags affected

Mov Move mov D,S (S) to (D) none

Destination (D) Source (S)

Memory AX

AX Memory

Register Register

Register Memory

Memory Register

Register Immediate

Memory Immediate

Seg-reg Reg16

Seg-reg Mem16

Reg16 Seg-reg

Mem16 Seg-reg14

Page 15: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Special data transfer instructions• XCHG – exchange data between the source and

destination• Eg XCHG AX, DX• Then value in DX is stored in AX and value in AX

stored in DX

15

Page 16: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Special data transfer instruction• LEA – load effective address• LEA is are very important operation

because it allows you to obtain an address of a variable or an array then you can apply the relative addressing modes!!!!

• Eg LEA SI,INPUT• To load a specified register with a 16-bit

offset address• Similar to x = &y in a C++ program

16

Page 17: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Example of LEA

Dat db 11H, 22H, 33H, 44H

LEA BX, datMov AL, [BX+2] ; this is register relative addressing mode

What is being stored in AL ?????

17

Page 18: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

• LDS – load register and DS• Eg LDS SI, [200]• LDS will load a specified register (SI) and

the DS • 4 bytes of data will be fetched the first two

stored in SI and the following two stored in DS

• LES – load register and ES• LES will load a specified register and the ES

18

Page 19: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Arithmetic instructions

• Some examples of available arithmetic operations: add, substract, multiply, etc.

• Can apply to unsigned, signed, binary bytes, words, unpacked, packed decimal bytes, or ASCII numbers

• Packed decimal – two BCD digits are packed into a byte register or memory location

• Unpacked decimal numbers are stored one BCD digit per byte

• After an arithmetic operation, the flags are updated accordingly

19

Page 20: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Arithmetic instructionsADD Add byte or word

ADC Add byte or word with carry

Inc Increment byte or word by 1

AAA ASCII adjust for addition

DAA Decimal adjust for addition

Sub Subtract byte or word

Sbb Subtract byte or word with borrow

DEC Decrement byte or word by 1

NEG Negate byte or word

AAS ASCII adjust for subtraction

DAS Decimal adjust for subtraction

MUL Multiply byte or word unsigned

IMUL Integer multiply byte or word

AAM ASCII adjust for multiply

Div Divide byte or word unsigned

Idiv Integer divide byte or word

AAD ASCII adjust for division

CBW Convert byte to word

CWD Convert word to doubleword20

Page 21: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Addition related operationsMnemonic Meaning Format Operation Flags affected

Add Addition Add D, S D = S+D

CF = carry

OF, SF, ZF, AF, PF, CF

ADC Add with carry ADC D, S

D = S+D+CF

CF = carry

OF, SF, ZF, AF, PF, CF

INC Increment by 1 INC D D = D+1 OF, SF, ZF, AF, PF

AAA ASCII adjust for addition

AAA AF, CF, OF, SF, ZF, PF undefined

DAA Decimal adjust for addition

DAA SF, ZF, AF, PF, CF, OF undefined

Destination Source

Register Register

Register Memory

Memory Register

Register Immediate

Memory Immediate

AX Immediate21

Page 22: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Subtraction

Mnemonic Meaning Format Operation Flags affected

Sub Subtract Sub D,S D = D-S

CF = borrow

OF, SF, ZF, AF, PF, CF

Sbb Subtract with borrow

SBB D,S D = D-S-CF OF, SF, ZF, AF, PF, CF

Dec Decrement by 1 DEC D D=D-1 OF, SF, ZF, AF, PF

Neg Negate Neg D D=0-D

CF =1

OF, SF, ZF, AF, PF, CF

DAS Decimal adjust for subtraction

DAS OF, SF, ZF, AF, PF, CF undefined

AAS ASCII adjust for subtraction

AAS OF, SF, ZF, AF, PF, CF undefined

22

Page 23: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Subtraction operands

Destination Source

Register Register

Register Memory

Memory Register

AX Immediate

Register Immediate

Memory immediate

23

Page 24: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Example

If BX = 3A NEG BXGive FFC6

NEG – make the operand negative (2’s complement)Executing the NEG instruction causes the following 2’s complement subtraction00 - (BX) = 0000 + 2’s complement of 3A

= 0000 + FFC6 = FFC6

24

Page 25: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Multiplication• When multiplying 8-bit data, result is 16-bit and

stored in AX• When multiplying 16-bit data, result is 32-bit and

result stored in AX and DX • The AX holds the LSB and DX MSB• Only the source operand is specified and the other

operand is in AL, or AX. This is also the same in division

• In division, AH is the remainder and AL is the quotient

• For 16-bit, AX contains the quotient and DX contains the remainder

25

Page 26: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Example

If AL is –1 and CL is –2, what will be the result produced In AX forMUL CLIMUL CL

MUL CL is unsigned multiply = FD02IMUL CL is signed multiply = 2

26

Page 27: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Special functions• CBW – convert byte (8-bit) to word (16-bit)• CBW – fill AH with 0 if AL is positive; fill AH with 1s

if AL is negative then AH becomes FF• CWD – convert word (16-bit) to double word (32-

bit) and the DX register is used to store the high order word

• CWD – fill DX with 0 if AL is positive; fill DX with 1s if AX is negative then DX becomes FF

• Use CBW when doing 8-bit operation• Use CWD when doing 16-bit operation

27

Page 28: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Example

What is the result of executing the following sequence Of instructions?MOV AL, A1CBWCWD

Ans.AX = FFA1 after CBWDX = FFFF after CWD

28

Page 29: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Multiplication and division functions

Mnemonic Meaning Format Operation Flags affected

MUL Multiply

(Unsigned)

MUL S AX = AL*S8

DX,AX = AX*S16

OF, CF, SF, ZF, AF, PF undefined

DIV Division

(unsigned)

DIV S AL = Q (AX/S8)

AH = R(AX/S8)

OF, CF, SF, ZF, AF, PF undefined

IMUL Integer multiply

(signed)

IMUL S AX = AL*S8

DX,AX=AX*S16

OF, CF, SF, ZF, AF, PF undefined

IDIV Integer divide

(signed)

IDIV S AX = Q(AX/S8)

AH=R(AX/S8)

AX = Q(DX,AX)/S16

DX=R(DX,AX)/S16

If Q is positive and exceeds 7FFF or if Q is negative and becomes less than 8001 then type 0 interrupt occurs

OF, CF, SF, ZF, AF, PF undefined

29

Page 30: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Multiplication related functions

Mnemonic Meaning Format Operation Flags affected

AAM Adjust AL for multiplication

Ascii adjust after Multiplication for BCD values

AAM AH=Q(AL/10)

AL=R(AL/10)

SF, ZF, PF, OF, AF, CF undefined

AAD Adjust AX for division

Prepare 2 BCD values for division

AAD AL=(AH*10+AL)

AH =00

SF, ZF, PF, OF, AF, CF undefined

CBW Convert byte to word

CBW All bits of AH = (MSB of AL)

None

CWD Convert word to double word

CWD All bits of DX = MSB of AX

none

30

Page 31: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

A simple application

• Try to write a simple assembly program to convert from Centigrade (Celsius) to Fahrenheit

• The equation for the conversion is • f = c * 9 / 5 + 32• What operations are required?• Do you need to define variables?

31

Page 32: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Logic Instructions

• Logic operations include: AND, OR, exclusive-OR (XOR) and NOT

• Eg AND AX, BX• Result is stored in AX• Operation is applied to bits of the operands

32

Page 33: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Exercise• Describe the result of executing the

following:• MOV AL, 01010101B • AND AL, 00011111B (AL = 00010101B)• OR AL, 11000000B (AL = 11010101B)• XOR AL, 00001111B (AL = 11011010B)• NOT AL (AL = 00100101B)

Ans. AL = 00100101

NOTE: NOT != NEG

33

Page 34: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Application of logical operations• If you only want to examine a particular bit

in a 8-bit pattern you can do– AND AL, #00010000

• If you want to clear a register to 0 – XOR BL, BL

34

Page 35: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Shift Instructions• There are 4 shift operations • Can perform: logical shift and arithmetic shift• Can shift left as well as right• Logical shift – the vacated bit will be filled with ‘0’• Arithmetic shift – the vacated bit will be filled with the

original most significant bit (this only applies in right shift, in left shift vacated bit is filled with ‘0’ as well.)

• Arithmetic shift will maintain the ‘sign’ of the original data

• If shift more than 1 bit then the number of bits to be shifted is stored in CL

35

Page 36: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Shift operationsMnemonic Meaning Format Operation Flags affected

SAL/SHL Shift arithmetic left/shift logical left

SAL/SHL D, Count

Shift the (D) left by the number of bit positions equal to count and fill the vacated bits positions on the right with zeros

CF, PF, SF, ZF, OF, AF undefined

OF undefined if count not equal to 1

SHR Shift logical right SHR D, Count Shift the (D) right by the number of bit positions equal to count and fill the vacated bits positions on the left with zeros

CF, PF, SF, ZF, OF, AF undefined

OF undefined if count not equal to 1

SAR Shift arithmetic right SAR D, Count Shift the (D) right by the number of bit positions equal to count and fill the vacated bits positions on the left with the original most significant bit

CF, PF, SF, ZF, OF, AF undefined

OF undefined

36

Page 37: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Rotate Instructions

• Rotate is similar to shift • The vacate bit is filled with the bit rotated out

from the other side• Rotate left through carry (RCL) – bit rotated out is

stored in the carry flag, the vacated bit is filled by the carry flag

• Eg RCR BX, CL• If CL = 0416 and BX = 123416 what is the result of

the above operation? – 1000 0001 0010 0011 if C is 0 at the beginning

37

Page 38: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Rotate Operations

Mnemonic Meaning Format Operation Flags affected

ROL Rotate left ROL D, Count Rotate the D left by the number of bit positions equal to Count. Each bit shifted out from the leftmost bit goes back into the rightmost bit position

CF

OF undefined if count 1

ROR Rotate

right

ROR D, Count Rotate the D right by the number of bit positions equal to Count. Each bit shifted out from the rightmost bit goes back into the leftmost bit position

CF

OF undefined if count 1

38

Page 39: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Rotate operationMnemonic Meaning Format Operation Flags affected

RCL Rotate left thro’ carry

RCL D, Count Same as ROL except carry is attached to D for rotation

CF

OF undefined if count 1

RCR Rotate right thro’ carry

RCR D, Count Same as ROL except carry is attached to D for rotation

CF

OF undefined if count 1

39

Page 40: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Applications of shift/rotate• To examine bit 6 of the AL register

– RCL AL, 2• To implement a multiply

– Shift a pattern to left = x2– 00000100 = 4 after shifting to left 00001000 = 8 – Shift to right = /2– 00000100 after shifting to right 00000010 = 2

• 3x9– Binary pattern of 9 = 1001 – 3x9 = 3 x 23 + 3x20

– 3x23 = shifting pattern of 3 to left 3 times + the original value (shift 0 time)– Binary pattern of 3 00000011– 00011000 (after 3 shift) + 00000011– Result = 00011011 = ???

40

Page 41: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Exercise

If AL = 128, BL = -4After the following:

ADD AL, 3ADC BL, ALRCL BL, 1 ; this is rotate left thro’ carry, this does not change the

; sign or overflow

Q. What will be the carry, sign and overflow flag?

41

Page 42: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Exercise

AX = 0010H BX = 0020H CX = 0030H DX = 0040HSI = 0100H DI = 0200H CF = 1HDS:100 = 10H DS:101 = 00H DS:120 = FFHDS:121 = FFH DS:130 = 08H DS:131 = 00HDS:150 = 02H DS:151 = 00H DS:200 = 30HDS:201 = 00H DS:210 = 40H DS:211 = 00HDS:220 = 30H DS:221 = 00H

42

Page 43: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

ADD AX, 00FF ( AX = 010F H)

ADC SI, AX (SI = 0111H)

INC BYTE PTR [0100]; Byte ptr indicate the size of memory data addressed by the memory pointer

(the value refers to is 10H so it becomes 11H)

SUB DL, BL (DL = 40 BL =20 so result = 20H)

SBB DL, [0200] (DL = 40H, [0200] refers to 30H so result is 09H )

DEC BYTE PTR [DI+BX] (data refers to by byte ptr [DI+BX] is 30H after DEC = 29H )

NEG BYTE PTR [DI] + 0010 ( NEG 40H = 1100 0000 )

MUL DX (10x40 so AX = 0400H DX = 0000H )

IMUL BYTE PTR [BX + SI] (AX = 0FF0H , DX = 0000H)

DIV BYTE PTR[SI] + 0030 (value refers to by Byte PTR[SI]+0030 is 08H (one byte) so after the division AH = 00 AL = 02)

IDIV BYTE PTR [BX][SI]+0030 ( the value refers to is 02H so after div AH =00 AL =08)

43

Page 44: Assembly Language Programming II Week 5 – Overview of x86 assembly language programming II

Exercise1. How would the decimal number 1234 be coded in ASCII and stored in

memory starting at address 0C000H? Assume that least significant digit is stored at the lower addressed memorylocation1 = (31H in 0C003H) 2 = (32H in 0C002H) 3 = (33H in 0C001H) 4 = (34H in 0C000H)

2. If register BX contains the value 010016, register DI contains 0010 and register DS contains 1075, what physical memory location is swapped when the following instruction is executedXCHG [BX+DI], AXPA = 10750+0100+0010 = 10860H

3. Compute the physical address for the specified operand in each of the following instructions:MOV [DI], AX (destination operand) (0B000+0200 = 0B200)MOV DI, [SI] (source operand) (0B000+0100 = 0B100)MOV DI+XYZ, AH (destination operand) (0B000+0200+0400 = 0B600)

Given CS=0A00, DS=0B00, SI=0100, DI=0200, BX=0300, XYZ=0400

44