Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
EEE 319
Lecturer
EEE, BUBT
shams@bubt.edu.bd
Section I Arithmetic Logic Unit (ALU)
The inputs to an ALU are the data (to be operated on called operands) and a code
indicating the operation to be performed. The output of the ALU is the result of the
performed operation.
The ALU also exchanges additional information with a status/flag register, which
relates to the result of the current or previous operations.
Data
A basic ALU has three parallel data buses consisting of two input operands (A and
B) and a result output (Y). Each data bus is a group of signals that conveys one
binary integer number.
Opcode
The opcode input is a parallel bus that conveys to the ALU an operation selection
code, which specifies the desired arithmetic or logic operation to be performed by
the ALU on the operands.
Status
The status various individual signals that convey supplemental information about
the result of an ALU operation. These outputs are usually stored in registers so
they can be used in future ALU operations or for controlling conditional branching.
Operand types:
REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.
SREG: DS, ES, SS, and only as second operand: CS.
Memory: [BX], [BX+SI+7], variable, etc.
Immediate: 5, -24, 3Fh, 10001101b, etc.
REG, memory
memory, REG
copy value of one operand to another operand
REG, REG
memory, immediate
operand1 = operand2
MOV REG, immediate
SREG, memory
MOV DS, AX ; copy value of AX to DS
memory, SREG
MOV CL, 'A' ; CL = 41h (ASCII code)
REG, SREG
SREG, REG
REG, memory
operand1 = operand1 + operand2
memory, REG
ADD REG, REG
MOV AL, 5 ; AL = 5
memory, immediate
ADD AL, -3 ; AL = 2
REG, immediate
operand = operand + 1
REG
INC
Memory MOV AL, 4
INC AL ; AL = 5
operand = operand 1
REG
DEC
Memory MOV AL, 255 ; AL = 0FFh (255 or -1)
DEC AL ; AL = 0FEh (254 or -2)
REG, memory
memory, REG
MOV AL, 'a' ; AL = 01100001b
AND REG, REG
AND AL, 11011111b ; AL = 01000001b ('A')
memory, immediate
REG, immediate
REG, memory
memory, REG
MOV AL, 'A' ; AL = 01000001b
OR REG, REG
OR AL, 00100000b ; AL = 01100001b ('a')
memory, immediate
REG, immediate
1 XOR 1 = 0
REG, memory 1 XOR 0 = 1
memory, REG 0 XOR 1 = 1
XOR REG, REG 0 XOR 0 = 0
memory, immediate
REG, immediate MOV AL, 00000111b
XOR AL, 00000010b ; AL = 00000101b
Shift all bits left, the bit that goes off is set to
memory, immediate
CF and the same bit is inserted to the right
REG, immediate
ROL most position.
memory, CL
REG, CL
MOV AL, 1Ch ; AL = 00011100b
ROL AL, 1 ; AL = 00111000b, CF=0
Shift all bits right, the bit that goes off is set
memory, immediate
to CF and the same bit is inserted to the left
REG, immediate
ROR most position.
memory, CL
REG, CL
MOV AL, 1Ch ; AL = 00011100b
ROR AL, 1 ; AL = 00001110b, CF=0
Shift all bits right, the bit that goes off is set
memory, immediate to CF and previous value of CF is inserted to
REG, immediate the leftmost position.
RCR
memory, CL
REG, CL STC ; set carry (CF=1)
MOV AL, 1Ch ; AL = 00011100b
RCR AL, 1 ; AL = 10001110b, CF=0
MOV AX , 0FFFFh
MOV BX , 0FFFFh
ADD AX, BX
Solution:
Actual Result = 1 FFFEh
Result stored in AX = FFFEh
Flags:
SF = 1 because the MSB is 1
PF = 0 because there are 7 (odd number) of 1s in the low byte of the result.
ZF = 0 because nonzero result
CF = 1 because there is a carry out of the MSB on addition
OF = 0 because the sign of the stored result is the same as that of the
numbers being added (in binary addition, there is a carry into the MSB and
carry out from MSB also)
Example 2
MOV AL , 80h
MOV BL , 80h
Solution:
Actual Result = 100h
Result in AL = 00h
Flags:
SF = 0 because MSB is 0
PF = 1 because all bits in result are 0
ZF = 1 because result is 0
CF = 1 because there is a carry out from MSB
OF = 1 because the numbers being added are both negative but the MSB in
result is 0 (in binary addition, there is a no carry into the MSB but there is
carry out from MSB.
Example 3
MOV AX , 8000h
MOV BX , 0001h
SUB AX , BX
Solution:
Actual Result = 7FFFh
Result in AX = 7FFFh
Flags:
SF = 0 because MSB is 0
PF = 1 because there are 8 one bits in the low byte of the result
ZF = 0 because result is nonzero
CF = 0 because a smaller unsigned number is being subtracted from a larger
one
OF =1 because we are subtracting a positive number from a negative number
but the result is positive (wrong sign of result)
Example 4
MOV AL , 0FFh
Solution:
Actual Result = 100h
Result stored in AL = 00h
Flags:
SF = 0
PF = 1
ZF = 1
CF = 0 because CF is unaffected by INC
OF = 0 because number of unlike sign are being added (there is a carry into
the MSB and also carry out from the MSB)
Example 5
MOV AX, -5
Solution:
Result in AX = -5 = FFFBh
Flags:
None of the flags are affected by MOV
Example 6
MOV AX , 8000h
Solution:
Result in AX = 8000h (2s complement)
Flags:
SF = 1
PF = 1, in low byte of result, number of 1 bits is 0.
ZF = 0
CF = 1 because for NEG, CF is always 1 unless the result is zero
OF = 1 because there is no sign change
Practice problem
Write the status of the flags (CF, AF, ZF, PF, SF) after each instruction is
executed.