-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSubstraction.py
More file actions
52 lines (38 loc) · 1.07 KB
/
Substraction.py
File metadata and controls
52 lines (38 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
" Subtraction Instruction"""
def twos_complement(val, nbits):
"""Compute the 2's complement of int value val"""
val = int(val,2)
if val < 0:
val = (1 << nbits) + val
else:
if (val & (1 << (nbits - 1))) != 0:
# If sign bit is set.
# compute negative value.
val = val - (1 << nbits)
return val
def Subtraction(a, b, N, Status):
c1 = 0
c2 = 0
Result = ""
for num in range(N-1, -1, -1) :
ax = int(a[num],2)
bx = int(b[num],2)
S = str(c1 ^ (ax ^ bx))
Result = Result + S
c2 = c1 & (~(ax ^ bx)) | (~ax) & bx
Status["CF"] = c2
Status["OF"] = c1 ^ c2
c1 = c2
Result = Result[::-1]
# Sign Function flag Function
if Result[0] == '1':
Status["SF"] = 1
if Result[0] == '0':
Status["SF"] = 0
# Zero Function flag Function
decimalResult = int(Result, base=2)
if decimalResult == 0:
Status["ZF"] = 1
if decimalResult != 0:
Status["ZF"] = 0
return Result, Status