You are on page 1of 17

# Computer Organization and Assembly Language

The NEG Instruction
Ø

The NEG (Negate) instruction converts the
specified operand to its 2’s complement
equivalent and the result returned to the
operand location. This is, in effect,
reversing the sign of an integer.
Format: NEG D
Action:
D

Destination
register
MM

Arithmetic Instructions and Logic instructions

0 - [D]

Example
NEG AX
NEG byte ptr [BX]

* Property of STI
Page 1 of 17

Computer Organization and Assembly Language

Example – NEG Instruction

Ø

Determine the value of AL and the value of
the flags (assume all flags are initially 0)
following the instruction sequence:
MOV AL, 05H
NEG AL

Arithmetic Instructions and Logic instructions

* Property of STI
Page 2 of 17

Computer Organization and Assembly Language

The CMP Instruction

Ø

The CMP (Compare) instruction subtracts
the source operand from the destination
operand. It then discards the result but it
updates the values of all the status flags.
Format: CMP D, S
Action:
[D] - [S]

Destination

Source

register

register

register

MM

CMP CX, BETA

MM

register

CMP BETA, DX

register

immediate

CMP SI, ABCDH

MM

immediate

CMP byte ptr [BX], 34H

Arithmetic Instructions and Logic instructions

Example
CMP BX, CX

* Property of STI
Page 3 of 17

Computer Organization and Assembly Language

Example – CMP 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:

AX =

0015H

BP =

0002H

CS = 3000H

BX =

0019H

SP =

0035H

DS = 2000H

CX = 0012H

DI =

0017H

SS = 2000H

DX = 001BH

SI =

001EH ES = 4000H

Arithmetic Instructions and Logic instructions

* Property of STI
Page 4 of 17

CMP AX, BX
CMP [SI], DI

Computer Organization and Assembly Language

Program Tracing Example 1
Trace the following program. Assume the
following: all flags and registers (unless
specified otherwise) are initially zero, DS =
2000H, SS = 2001H, the label ALPHA =
001CH. Use the memory map (if necessary).
MOV AX, ALPHA
MOV SP, AX
POP BX
SUB BX, 9F79H
XCHG BX, BP
MOV CX, [BP]
SBB AX, CX
DAA
NEG AX
LEA SI, ALPHA
MOV DX, [SI]
Arithmetic Instructions and Logic instructions

* Property of STI
Page 5 of 17

Computer Organization and Assembly Language

The AND Instruction

Ø

The AND (Logical AND) instruction
logically ANDs the source and the
destination operands and stores the result
in the destination operand.
Format: AND D, S
Action: D
[D] ⋅ [S]

Destination

Source

register

register

register

MM

MM

register

AND BETA, CX

register

immediate

AND BX, 0015H

MM

Immediate

AND byte ptr BETA, 12H

Arithmetic Instructions and Logic instructions

Example
AND BX, CX
AND DX, [BP + SI]

* Property of STI
Page 6 of 17

Computer Organization and Assembly Language

Example – AND Instruction

Ø

Determine the value of affected registers
and the value of the flags (assume all flags
are initially 0) following the instruction
sequence:
MOV AL, 05H
MOV BL, FEH
AND AL, BL

Arithmetic Instructions and Logic instructions

* Property of STI
Page 7 of 17

Computer Organization and Assembly Language

The OR Instruction

Ø

The OR (Logical OR) instruction logically
ORs the source and the destination
operands and stores the result in the
destination operand.

Format: OR D, S
Action: D

[D] + [S]

Destination

Source

register

register

register

MM

MM

register

register
MM

Example
OR BX, CX
OR DX, [BP + SI]
OR BETA, CX

immediate OR BX, 0015H
immediate OR byte ptr BETA, 12H

Arithmetic Instructions and Logic instructions

* Property of STI
Page 8 of 17

Computer Organization and Assembly Language

Example – OR Instruction

Ø

Determine the value of affected registers
and the value of the flags (assume all flags
are initially 0) following the instruction
sequence:
MOV CL, 05H
MOV DL, 80H
OR CL, DL

Arithmetic Instructions and Logic instructions

* Property of STI
Page 9 of 17

Computer Organization and Assembly Language

The XOR Instruction

Ø

The XOR (Logical XOR) instruction
logically XORs the source and the
destination operands and stores the result
in the destination operand.

Format: XOR D, S
Action: D
[D] ⊕ [S]

Destination

Source

register

register

register

MM

MM

register

XOR BETA, CX

register

immediate

XOR BX, 0015H

MM

immediate

XOR byte ptr BETA, 12H

Arithmetic Instructions and Logic instructions

Example
XOR BX, CX
XOR DX, [BP + SI]

* Property of STI
Page 10 of 17

Computer Organization and Assembly Language

Example – XOR Instruction

Ø

Determine the value of affected registers
and the value of the flags (assume all flags
are initially 0) following the instruction
sequence:
MOV AL, 15H
MOV BL, ABH
XOR AL, BL

Arithmetic Instructions and Logic instructions

* Property of STI
Page 11 of 17

Computer Organization and Assembly Language

The NOT Instruction

Ø

The NOT (Logical NOT) instruction
performs a 1’s complement on the operand.
Format: NOT D
Action: D

[D]’

Destination
register
MM

Example
NOT AX
NOT byte ptr BETA

Arithmetic Instructions and Logic instructions

* Property of STI
Page 12 of 17

Computer Organization and Assembly Language

Example – NOT Instruction

Ø

Perform the following operation. Each
instruction is dependent on one another.
Assume that all flags are initially 0:
MOV BX, D32BH
MOV CX, 1056LH
NOT BX
NOT CH

Arithmetic Instructions and Logic instructions

* Property of STI
Page 13 of 17

Computer Organization and Assembly Language

The TEST Instruction

Ø

The TEST instruction logically ANDs the
source and the destination operands. The
result is discarded but the values of the
status flags are updated.
Format:
Action:

TEST D, S
[D] ⋅ [S]

Arithmetic Instructions and Logic instructions

* Property of STI
Page 14 of 17

Computer Organization and Assembly Language

Example – NOT Instruction

Ø

Perform the following operation. Each
instruction is dependent on one another.
Assume that all flags are initially 0:
MOV SI, 2376H
MOV DI, 1941H
TEST SI, DI

Arithmetic Instructions and Logic instructions

* Property of STI
Page 15 of 17

Computer Organization and Assembly Language

Logic Instruction Program
Tracing Example

Ø

Trace the following program. Assume the
following: all flags and registers (unless
specified otherwise) are initially zero, DS =
2000H, SS = 2001H, the label ALPHA =
001CH. Use the memory map (if
necessary).
MOV BX, ALPHA
MOV SP, BX
NEG BX
AND BX, [AX]
OR BX, 0020H
CMP BX, SP
MOV CX, [SP]
TEST BX, CX
NOT AX
XOR AL, CL

Arithmetic Instructions and Logic instructions

* Property of STI
Page 16 of 17

Computer Organization and Assembly Language

Program Tracing Exercise 1

Ø

Trace the following program. Assume the
following: all flags and registers (unless
specified otherwise) are initially zero, DS =
2000H, SS = 2001H, the label ALPHA =
001CH. Use the memory map (if
necessary).
MOV AL, 23H
MOV CH, 6BH
XOR AL, 0CH