-
Notifications
You must be signed in to change notification settings - Fork 0
/
asmmultiplier.asm
80 lines (57 loc) · 1.03 KB
/
asmmultiplier.asm
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
extern printf
section .data
num1 dq 125
num2 dq 13
string1 db "The two input numbers are %d and %d.", 10, 0
string2 db "The result of multiplying them together with the built in x86 instruction is %d.", 10, 0
string3 db "The result of multiplying them together by only shift left is %d.", 10, 0
section .bss
section .text
global main
main:
push rbp
mov rbp, rsp
mov rdi, string1
mov rsi, [num1]
mov rdx, [num2]
call printf
mov rdi, [num1]
mov rsi, [num2]
call defaultmul
mov rdi, string2
mov rsi, rax
call printf
mov rdi, [num1]
mov rsi, [num2]
xor rax, rax
xor r8, r8
xor r9, r9
call shlmul
mov rdi, string3
mov rsi, rax
call printf
leave
ret
defaultmul: ; return value in rax
push rbp
mov rbp, rsp
imul rdi, rsi
mov rax, rdi
leave
ret
shlmul:
push rbp
mov rbp, rsp
cmp rcx, 63
je exit
inc rcx
shr rsi, 1
jnc shlmul
mov r8, rdi ; put in num 1
mov r9, rcx ; put in counter
dec r9 ; counter - 1
shl r8, [r9] ; shl r9 times
add rax, r8
exit:
leave
ret