23
* Property of STI Page 1 of 23 Arithmetic Instructions Computer Organization and Assembly Language Review of Some Concepts Regarding the Binary Number System 1. Unsigned Integer. Unsigned integers may either be 8 bits or 16 bits in length. Unsigned integers are viewed as positive numbers only. 2. Signed Integer. Signed integers are viewed as either positive or negative numbers. Because of this, the most significant bit is viewed as a sign bit (0 for positive and 1 for negative). 3. Binary-Coded Decimal (BCD). In BCD, each decimal digit is represented in one nibble. 4. Packed BCD. In packed BCD, two BCD digits could be packed into one byte. 5. Unpacked BCD. In unpacked BCD, one BCD digit is represented in one byte. The BCD digit is represented in the least significant nibble, while the most significant nibble may be 0 or any value.

MELJUN CORTES Arithmetic instructions

Embed Size (px)

Citation preview

Page 1: MELJUN CORTES Arithmetic instructions

* Property of STIPage 1 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

Review of Some Concepts Regarding the Binary Number

System

1. Unsigned Integer. Unsigned integers may either be 8 bits or 16 bits in length. Unsigned integers are viewed as positive numbers only.

2. Signed Integer. Signed integers are viewed as either positive or negative numbers. Because of this, the most significant bit is viewed as a sign bit (0 for positive and 1 for negative).

3. Binary-Coded Decimal (BCD). In BCD, each decimal digit is represented in one nibble.

4. Packed BCD. In packed BCD, two BCD digits could be packed into one byte.

5. Unpacked BCD. In unpacked BCD, one BCD digit is represented in one byte. The BCD digit is represented in the least significant nibble, while the most significant nibble may be 0 or any value.

Page 2: MELJUN CORTES Arithmetic instructions

* Property of STIPage 2 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

The ADD Instruction

Ø The ADD instruction adds the source operand to the destination operand and then places the result in the destination operand.

Format: ADD D, SAction: D [D] + [S]

ADD byte ptr BETA, 12H

immediateMM

ADD BX, 0015Himmediateregister

ADD [BX + DI], CXregisterMM

ADD DX, [BP + SI]MMregister

ADD BX, CXregisterregister

ExampleSourceDestination

Page 3: MELJUN CORTES Arithmetic instructions

* Property of STIPage 3 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

Example – ADD Instruction

Ø Unless otherwise stated, determine the contents of all the affected general purpose registers and flags after executing the following program. Each instruction is dependent of one another. Whenever necessary, use the memory map (handout) for additional data. Assume the following register contents and assume that all flags are initially 0:

ADD AX, BXADD [SI], DI

4000HES =001EHSI =001BHDX =

2000HSS =0017HDI =0012HCX =

2000HDS =0035HSP =0019HBX =

3000HCS =0002HBP =0015HAX =

Page 4: MELJUN CORTES Arithmetic instructions

* Property of STIPage 4 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

Exercise 1

Ø Unless otherwise stated, determine the contents of all the affected general purpose registers and flags after executing the following program. Each instruction is dependent of one another. Whenever necessary, use the memory map (handout) for additional data. Assume the following register contents and assume that all flags are initially 0:

ADD AX, [CX]ADD [DX], SI

4000HES =001EHSI =001BHDX =

2000HSS =0017HDI =0012HCX =

2000HDS =0035HSP =0019HBX =

3000HCS =0002HBP =0015HAX =

Page 5: MELJUN CORTES Arithmetic instructions

* Property of STIPage 5 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

ADC Instruction

Ø The ADC (Add with Carry) instruction adds the source operand and the carry flag to the destination operand and places the result in the destination operand.

Format: ADC D, SAction: D [D] + [S] + [CF]

Ø One of the uses of the ADC instruction is in the implementation of 32-bit addition (adding two 32-bit numbers).

Page 6: MELJUN CORTES Arithmetic instructions

* Property of STIPage 6 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

Example – ADC Instruction

Ø Unless otherwise stated, determine the contents of all the affected general purpose registers and flags after executing the following program. Each instruction is dependent of one another. Whenever necessary, use the memory map (handout) for additional data. Assume the following register contents and assume that all flags are initially 0:

MOV AX, FAFAHMOV BX, [SP]ADD AX, BXADC SI, DI

4000HES =001EHSI =001BHDX =

2000HSS =0017HDI =0012HCX =

2000HDS =0035HSP =0019HBX =

3000HCS =0002HBP =0015HAX =

Page 7: MELJUN CORTES Arithmetic instructions

* Property of STIPage 7 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

Exercise 2

Ø Unless otherwise stated, determine the contents of all the affected general purpose registers and flags after executing the following program. Each instruction is dependent of one another. Whenever necessary, use the memory map (handout) for additional data. Assume the following register contents and assume that all flags are initially 0:

MOV CX, EBADHMOV BX, [SI]ADD CX, BXADC AX, DX

4000HES =001EHSI =001BHDX =

2000HSS =0017HDI =0012HCX =

2000HDS =0035HSP =0019HBX =

3000HCS =0002HBP =0015HAX =

Page 8: MELJUN CORTES Arithmetic instructions

* Property of STIPage 8 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

The INC Instruction

Ø The INC (Increment) instruction adds 1 to the destination operand.

Format: INC DAction: D [D] + 1

INC byte ptr [BX]MM

INC AXregister

ExampleDestination

Page 9: MELJUN CORTES Arithmetic instructions

* Property of STIPage 9 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

Example – INC Instruction

Ø Unless otherwise stated, determine the contents of all the affected general purpose registers and flags after executing the following program. Each instruction is dependent of one another. Whenever necessary, use the memory map (handout) for additional data. Assume the following register contents and assume that all flags are initially 0:

INC AXINC BXINC CXINC DX

4000HES =001EHSI =001BHDX =

2000HSS =0017HDI =0012HCX =

2000HDS =0035HSP =0019HBX =

3000HCS =0002HBP =0015HAX =

Page 10: MELJUN CORTES Arithmetic instructions

* Property of STIPage 10 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

The DAA Instruction

Ø The DAA (Decimal Adjust for Addition) instruction adjusts the result of a previous addition of two valid packed decimal operands.

Format: DAA

Example:MOV AL, 15HMOV BL, 15HADD AL, BLDAA

Page 11: MELJUN CORTES Arithmetic instructions

* Property of STIPage 11 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

Example – DAA Instruction

Ø Unless otherwise stated, determine the contents of all the affected general purpose registers and flags after executing the following program. Each instruction is dependent of one another. Whenever necessary, use the memory map (handout) for additional data. Assume that all flags are initially 0:

MOV AL, F5HMOV BL, F5HADD AL, BLDAA

4000HES =001EHSI =001BHDX =

2000HSS =0017HDI =0012HCX =

2000HDS =0035HSP =0019HBX =

3000HCS =0002HBP =0015HAX =

Page 12: MELJUN CORTES Arithmetic instructions

* Property of STIPage 12 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

The AAA Instruction

Ø The AAA (ASCII Adjust for Addition) instruction adjusts the result of a previous addition of two valid unpacked decimal operands.

Format: AAA

Example:

MOV AL, 35H; ASCII value of 5MOV BL, 34H; ASCII value of 4ADD AL, BLAAA

Page 13: MELJUN CORTES Arithmetic instructions

* Property of STIPage 13 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

Example – AAA Instruction

Ø Unless otherwise stated, determine the contents of all the affected general purpose registers and flags after executing the following program. Each instruction is dependent of one another. Whenever necessary, use the memory map (handout) for additional data. Assume that all flags are initially 0:

MOV AL, 74HMOV BL, 75HADD AL, BLAAA

4000HES =001EHSI =001BHDX =

2000HSS =0017HDI =0012HCX =

2000HDS =0035HSP =0019HBX =

3000HCS =0002HBP =0015HAX =

Page 14: MELJUN CORTES Arithmetic instructions

* Property of STIPage 14 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

The SUB Instruction

Ø The SUB instruction subtracts the source operand from the destination operand and the places the result in the destination operand.

Format: SUB D, SAction: D [D] - [S]

SUB byte ptr BETA, 12H

immediateMM

SUB BX, 0015Himmediateregister

SUB [BX + DI], CXregisterMM

SUB DX, [BP + SI]MMregister

SUB BX, CXregisterregister

ExampleSourceDestination

Page 15: MELJUN CORTES Arithmetic instructions

* Property of STIPage 15 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

Example – SUB Instruction

Ø Unless otherwise stated, determine the contents of all the affected general purpose registers and flags after executing the following program. Each instruction is dependent of one another. Whenever necessary, use the memory map (handout) for additional data. Assume the following register contents and assume that all flags are initially 0:

SUB AX, BXSUB [SI], DI

4000HES =001EHSI =001BHDX =

2000HSS =0017HDI =0012HCX =

2000HDS =0035HSP =0019HBX =

3000HCS =0002HBP =0015HAX =

Page 16: MELJUN CORTES Arithmetic instructions

* Property of STIPage 16 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

The SBB Instruction

Ø The SBB (Subtract with Borrow) instruction subtracts the source operand and the carry flag from the destination operand and then places the result in the destination operand.

Format: SBB D, SAction: D [D] - [S] - [CF]

Ø Just like in addition, subtracting a 32-bit number from another 32-bit number can be done with the combination of the SUB and the SBB instructions.

Page 17: MELJUN CORTES Arithmetic instructions

* Property of STIPage 17 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

Example – SBB Instruction

Ø Unless otherwise stated, determine the contents of all the affected general purpose registers and flags after executing the following program. Each instruction is dependent of one another. Whenever necessary, use the memory map (handout) for additional data. Assume the following register contents and assume that all flags are initially 0:

MOV AX, 7AFAHMOV BX, LISTSUB AX, BXSBB SI, DI

4000HES =001EHSI =001BHDX =

2000HSS =0017HDI =0012HCX =

2000HDS =0035HSP =0019HBX =

3000HCS =0002HBP =0015HAX =

Page 18: MELJUN CORTES Arithmetic instructions

* Property of STIPage 18 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

The DEC Instruction

Ø The DEC (Decrement) instruction subtracts 1 from the destination operand.

Format: DEC DAction: D [D] - 1

DEC word ptr [BX]MM

DEC AXregister

ExampleDestination

Page 19: MELJUN CORTES Arithmetic instructions

* Property of STIPage 19 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

Example – DEC Instruction

Ø Unless otherwise stated, determine the contents of all the affected general purpose registers and flags after executing the following program. Each instruction is dependent of one another. Whenever necessary, use the memory map (handout) for additional data. Assume the following register contents and assume that all flags are initially 0:

DEC AXDEC BXDEC CXDEC DX

4000HES =001EHSI =001BHDX =

2000HSS =0017HDI =0012HCX =

2000HDS =0035HSP =0019HBX =

3000HCS =0002HBP =0015HAX =

Page 20: MELJUN CORTES Arithmetic instructions

* Property of STIPage 20 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

The DAS Instruction

Ø The DAS (Decimal Adjust for Subtraction) instruction adjusts the result of a previous subtraction of two valid packed decimal operands.

Format: DAS

Example:

MOV AL, 34HMOV BL, 19HSUB AL, BLDAS

Page 21: MELJUN CORTES Arithmetic instructions

* Property of STIPage 21 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

Example – DAS Instruction

Ø Unless otherwise stated, determine the contents of all the affected general purpose registers and flags after executing the following program. Each instruction is dependent of one another. Whenever necessary, use the memory map (handout) for additional data. Assume that all flags are initially 0:

MOV AL, 3FHMOV BL, 15HSUB AL, BLDAS

4000HES =001EHSI =001BHDX =

2000HSS =0017HDI =0012HCX =

2000HDS =0035HSP =0019HBX =

3000HCS =0002HBP =0015HAX =

Page 22: MELJUN CORTES Arithmetic instructions

* Property of STIPage 22 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

The AAS Instruction

Ø The AAS (ASCII Adjust for Subtraction) instruction adjusts the result of a previous subtraction of two valid unpacked decimal operands.

Format: AAS

Example:

MOV AL, 39H; ASCII value of 9MOV BL, 34H; ASCII value of 4SUB AL, BLAAS

Page 23: MELJUN CORTES Arithmetic instructions

* Property of STIPage 23 of 23

Arithmetic Instructions

Computer Organization and Assembly Language

Example – AAS Instruction

Ø Unless otherwise stated, determine the contents of all the affected general purpose registers and flags after executing the following program. Each instruction is dependent of one another. Whenever necessary, use the memory map (handout) for additional data. Assume that all flags are initially 0:

MOV AL, 8EHMOV BL, 62HSUB AL, BLAAS

4000HES =001EHSI =001BHDX =

2000HSS =0017HDI =0012HCX =

2000HDS =0035HSP =0019HBX =

3000HCS =0002HBP =0015HAX =