-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2adder.asm
More file actions
99 lines (82 loc) · 1.9 KB
/
Copy path2adder.asm
File metadata and controls
99 lines (82 loc) · 1.9 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
section .data
a db 0b11 ; first number (bits: a1 a0)
b db 0b01 ; second number (bits: b1 b0)
result db 0 ; will hold sum bits (r1 r0)
carry db 0 ; final carry out
newline db 10 ; line feed
section .text
global _start
_start:
mov al, [a]
mov bl, [b]
; ----- LSB full adder -----
mov cl, al
and cl, 1 ; cl = a0
mov dl, bl
and dl, 1 ; dl = b0
mov ch, cl ; save a0
xor cl, dl ; sum0 = a0 XOR b0
mov [result], cl
and ch, dl ; carry0 = a0 AND b0
mov [carry], ch
; ----- MSB full adder -----
mov cl, al
shr cl, 1
and cl, 1 ; cl = a1
mov dl, bl
shr dl, 1
and dl, 1 ; dl = b1
mov dh, cl ; save a1
xor cl, dl ; a1 XOR b1
mov ch, [carry] ; previous carry
xor cl, ch ; sum1 = (a1 XOR b1) XOR carry
shl cl, 1
mov dl, [result]
or cl, dl
mov [result], cl
; ----- Final carry -----
mov cl, dh ; a1
mov dl, bl
shr dl, 1
and dl, 1 ; dl = b1
mov ch, [carry] ; previous carry
mov al, cl
and al, dl ; a1 & b1
mov bl, cl
and bl, ch ; a1 & prev_carry
or al, bl
mov bl, dl
and bl, ch ; b1 & prev_carry
or al, bl
mov [carry], al
; ----- Print result and carry -----
mov al, [result]
add al, '0'
mov [result], al
mov eax, 4 ; sys_write
mov ebx, 1 ; stdout
mov ecx, result
mov edx, 1
int 0x80
mov eax, 4
mov ebx, 1
mov ecx, newline
mov edx, 1
int 0x80
mov al, [carry]
add al, '0'
mov [carry], al
mov eax, 4
mov ebx, 1
mov ecx, carry
mov edx, 1
int 0x80
mov eax, 4
mov ebx, 1
mov ecx, newline
mov edx, 1
int 0x80
; Exit
mov eax, 1
xor ebx, ebx
int 0x80