-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpower_extended.s
67 lines (52 loc) · 1.03 KB
/
power_extended.s
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
# as --32 power_extended.s -o ./bin/power_extended.a && ld -m elf_i386 ./bin/power_extended.a -o ./bin/power_extended && rm ./bin/power_extended.a
.section .data
.section .text
.globl _start
_start:
#push the variables (base and power) on to the stack
pushl $3 # power
pushl $2 # base
call power # returns to eax
addl $8, %esp
pushl %eax # push the result onto the stack
pushl $0 #power
pushl $2 #base
call power
addl $8, %esp
popl %ebx
addl %eax, %ebx
movl $1, %eax
int $0x80
# %ebx - the base
# %ecx - the power
# -4(%ebp) - holds the current result - note that it's a stack pointer
# %eax is used for temp storage
.type power, @function
power:
pushl %ebp
movl %esp, %ebp
subl $4, %esp
movl 8(%ebp), %ebx
movl 12(%ebp), %ecx
movl %ebx, -4(%ebp)
# handle power 0
cmpl $0, %ecx
je power_zero
power_loop_start:
cmpl $1, %ecx
je end_power
movl -4(%ebp), %eax
imull %ebx, %eax
movl %eax, -4(%ebp)
decl %ecx
jmp power_loop_start
end_power:
movl -4(%ebp), %eax
movl %ebp, %esp
popl %ebp
ret
power_zero:
movl $1, %eax
movl %ebp, %esp
popl %ebp
ret